繁体   English   中英

Swift 3 UIView 动画

[英]Swift 3 UIView animation

自从将我的项目升级到 swift 3 以来,我的自动布局约束动画不起作用; 更具体地说,它们是捕捉到新位置而不是动画。

UIView.animate(withDuration: 0.1,
               delay: 0.1,
               options: UIViewAnimationOptions.curveEaseIn,
               animations: { () -> Void in
                   constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
                   self.layoutIfNeeded()
    }, completion: { (finished) -> Void in
    // ....
})

我知道他们添加了UIViewPropertyAnimator类,但我还没有尝试。

我在 swift 3 的最新更新中也遇到了这个问题。

确切地说,无论何时您想要为视图设置动画,您实际上都在该视图的超级视图上调用 layoutIfNeeded。

试试这个:

UIView.animate(withDuration: 0.1,
           delay: 0.1,
           options: UIViewAnimationOptions.curveEaseIn,
           animations: { () -> Void in
               constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
               self.superview?.layoutIfNeeded()
}, completion: { (finished) -> Void in
// ....
})

在过去,他们似乎对能够重新布局您想要动画的视图很宽容。 但是在文档中,您确实应该在超级视图中调用 layoutIfNeeded 。

升级到 swift 3.0

查看从右到左的动画,如苹果默认推送动画

//intially set x = SCREEN_WIDTH
view.frame = CGRect(x: ScreenSize.SCREEN_WIDTH, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)

UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
 //Set x position what ever you want
                        view.frame = CGRect(x: 0, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)

                    }, completion: nil)

首先为您的约束设置新值,然后调用 animate。

self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
    self.view.layoutIfNeeded()
}

斯威夫特 3.1 , Xcode 8.3.2

这段代码对我很有效。 您视图上的任何更改都将以慢动作动画。

UIView.animate(withDuration: 3.0, animations: { // 3.0 are the seconds

// Write your code here for e.g. Increasing any Subviews height.

self.view.layoutIfNeeded()

})

斯威夫特 4,Xcode 10

用于搜索的从右到左动画

//初始设置 x = Your_View_Width

    viewOfSearch.frame = CGRect(x: viewOfSearch.frame.size.width, y: 0 , width: viewOfSearch.frame.size.width, height: 50)

    UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
        //Set x position what ever you want
        self.viewOfSearch.frame = CGRect(x: 0, y: 0 , width: self.viewOfSearch.frame.size.width, height: 50)

    }, completion: nil)
func setView(view: UIView) {
        UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
        })
    }
  • 更改选项部分以在该特定 UIView 上应用新动画。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM