首页 > 解决方案 > swift - 从左侧为 SlideIn 和 SlideOut 设置动画

问题描述

如何快速从左侧滑入和滑出菜单?下面的代码显示了左侧的菜单,但是当 slideIn 快速发生并且它实际上并没有显示菜单来自左侧。此外,slideOut 从右侧而不是左侧关闭菜单。

这是我的代码:

UIView.animate(withDuration: 0.5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
            self.blackView.alpha = 1

            self.collectionView.frame = CGRect(x: 0, y: 0, width: 
            self.collectionView.frame.width - 10, height: 
            self.collectionView.frame.height)
        }, completion: nil)

也适用于幻灯片

UIView.animate(withDuration: 0.5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.blackView.alpha = 0
        if let window = UIApplication.shared.keyWindow {
            self.collectionView.frame = CGRect(x: window.frame.width, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
        }
    }) { (completed: Bool)

任何建议表示赞赏。

标签: swift

解决方案


//Slide in
self.collectionView.frame = CGRect(x: -(window.frame.width), y: 0, width: window.frame.width-50, height: window.frame.height)
UIView.animate(withDuration: 0.5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
    self.blackView.alpha = 1

    self.collectionView.frame = CGRect(x: 0, y: 0, width: window.frame.width-50, height: window.frame.height)
}, completion: nil)

//Slide out
UIView.animate(withDuration: 0.5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
    self.blackView.alpha = 0
    if let window = UIApplication.shared.keyWindow {
        self.collectionView.frame = CGRect(x: -(window.frame.width), y: 0, width: window.frame.width-50, height: window.frame.height)
    }
}, completion: nil)

推荐阅读