UIView bottom to top animation issue

◇◆丶佛笑我妖孽 提交于 2019-12-11 13:08:51

问题


This is a simple function I am using for animating a view from top to bottom and vice versa (if is top to bottom animation and else is bottom to top animation) :

@objc func openMenu(sender: UIButton) {
        if sender.tag == 1 {
            self.buttonView.tag = 2
             self.moduleView = ModulesCollectionView(frame: CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0), collectionViewLayout: UICollectionViewLayout())
            self.window?.addSubview(self.moduleView)
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - self.frame.size.height - 22)
            }, completion: { _ in

            })

        } else {
            self.buttonView.tag = 1
            UIView.animate(withDuration: 3, animations: {
                self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0)
            }, completion: { _ in
                self.moduleView.removeFromSuperview()
            })
        }
    }  

Top animation works fine and the view is animated from top to bottom pleasantly in 0.7 seconds. However, bottom to top animation does not happen. The view is removed instantly. This is the result I am getting :

But I want the animation to be clean while going from bottom to top as well. Just like here.

Secondary : What I finally plan to achieve is PullUpController with the exact reverse animation. So if anyone knows a similar library (pull down drag) can share there inputs.

Update : The issue is coming only with UICollectionView. I replaced collectionView with a simple UIView and it worked perfect.


回答1:


You should try to use layoutSubViews() method after at the end of animation block. Change the animation block like this.

For if block:

self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - self.frame.size.height - 22)
self.moduleView.layoutSubviews()

For else block:

self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0)
self.moduleView.layoutSubviews()



回答2:


Here is example code, hope it will help.

Show view:

self.containerView = ModulesCollectionView(frame: UIScreen.main.bounds)
self.containerView.center = CGPoint(x: self.view.center.x,
        y: self.view.center.y + self.view.frame.size.height)
self.window?.addSubview(self.moduleView)
self.window?.bringSubview(toFront: self.moduleView)
self.window?.endEditing(true)
UIView.animate(withDuration: 0.3, delay: 0.0,
    usingSpringWithDamping: 0.7, initialSpringVelocity: 3.0,
    options: .allowAnimatedContent, animations: {
    self.moduleView.center = self.view.center
}) { (isFinished) in
    self.view.layoutIfNeeded()
}

hide view:

UIView.animate(withDuration: 0.7, delay: 0.0,
    usingSpringWithDamping: 1, initialSpringVelocity: 1.0,
    options: .allowAnimatedContent, animations: {
    self.moduleView.center = CGPoint(x: self.view.center.x,
    y: self.view.center.y + self.view.frame.size.height)
}) { (isFinished) in
    self.moduleView.removeFromSuperview
}


来源:https://stackoverflow.com/questions/50741078/uiview-bottom-to-top-animation-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!