xcode - Swift UIView.animateWithDuration 有时会忽略动画代码

标签 xcode swift animation uiview

我正在尝试为 UIButton 制作动画以缩小并移动到 View 的中心。该动画在正确运行和不运行之间交替。当它无法正确运行时,它会忽略将 UIButton 发送到中心的调用,而仅将 View 缩小到位。

动画代码:

println("before send box center \(self.box1.center) to middle \(self.view.center)")

UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
     println("1 send box center \(self.box1.center) to middle \(self.view.center)")

     self.box1.center = self.view.center                
     self.box1.transform = CGAffineTransformMakeScale(0.0001, 0.0001)

     println("2 send box center \(self.box1.center) to middle \(self.view.center)")
}, completion: { finished in
     println("final box center \(self.box1.center) to middle \(self.view.center)")
})

当动画正确运行时,前两个 println 在动画之前显示框的中心 CGPoint,最后两个 println 显示 View 的中心 CGPoint,现在也是框的中心点。

当动画困惑时,前两个 println 在动画之前显示框的中心 CGPoint,第三个 println(在动画 block 内)显示框的中心已设置为 View 的中心,但是当我们到达 println 时在完成 block 中,盒子已返回到其原始中心点(动画也执行相同的操作,将盒子缩小到位,而不将其移动到中心)

什么会导致相同的动画代码在遵循其代码和忽略其代码之间翻转?

什么会导致动画 block 中设置框中心点的代码在我们到达完成 block 时被忽略?

没有其他代码可以设置动画或更改我尝试设置动画的框的框架或位置。

我尝试添加 UIViewAnimationOptions.OverrideInheritedCurve 和 . AllowAnimatedContent、.BeginFromCurrentState 和 .AllowUserInteraction 希望找到导致问题的故障,但这些都没有对代码执行方式或动画显示方式产生任何影响。

最佳答案

我敢打赌您已经在 Storyboard/XIB 中选中了“使用自动布局”。使用自动布局时,您必须以不同的方式制作动画。您应该为这些设置添加约束并将 IBOutlet 连接到这些约束,而不是更改 View 的框架或中心。

然后在动画代码中更改约束的常量值。然后在 self.view 上调用 layoutIfNeeded()

关于xcode - Swift UIView.animateWithDuration 有时会忽略动画代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30967654/

相关文章:

objective-c - 发送通知到山狮通知中心

ios - 在 Swift 的属性声明期间引用 self

reactjs - 使用 Velocity.js 从 URL 渲染 SVG 进行动画

CSS 动画 chrome 滞后

ios - 设置输出视频设置时出错

ios - NSSortDescriptor 与关系

objective-c - 在 Mac 应用程序中使用钥匙串(keychain)中的证书

ios - SwiftUI - 如何在图像上添加前景线性渐变

ios - UIScrollView 及其子项 : why are they placed on top of each other? (AutoLayout)

jquery - 单击时动画链接下划线而不是悬停时下划线?