Swift 3 UIView动画

Ale*_*own 18 uiview uiviewanimation ios swift swift3

由于将我的项目升级到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
    // ....
})
Run Code Online (Sandbox Code Playgroud)

我知道他们增加了UIViewPropertyAnimator课程,但还没有尝试.

小智 27

我对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
// ....
})
Run Code Online (Sandbox Code Playgroud)

在过去,他们似乎对于能够重新布置您想要制作动画的视图感到宽容.但是在文档中你应该在superview中调用layoutIfNeeded.

  • 请尝试这个代码,它的工作对我来说.UIView.animate(withDuration:1,delay:0,options:.curveEaseInOut,animations:{self.bottomViewTopConstraint.constant = 0.0 self.bottomView.superview?.layoutIfNeeded()},completion:nil) (3认同)

Sau*_*hah 9

升级到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)
Run Code Online (Sandbox Code Playgroud)


abd*_*lek 6

首先为约束设置新值,然后调用animate.

self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
    self.view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*wad 5

斯威夫特 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()

})
Run Code Online (Sandbox Code Playgroud)