iOS 动画 - View 应该在 super View 之外

标签 ios iphone swift cocoa-touch animation

我创建了一个 UIBezierPath 对象,我希望根据从加速度计接收到的值在 super View 中移动它。我正在尝试使用 UIView 动画,但我不太确定我是否特别了解这段代码。

UIBezierPath 对象的代码如下:

 override func drawRect(rect: CGRect) {
    let path = UIBezierPath(arcCenter: pathCenter, radius: pathRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)
    UIColor.blueColor().setFill()
    UIColor.greenColor().setStroke()
    path.lineWidth = 2.0
    path.fill()
    path.stroke()
}

在 ViewController 中,我使用以下代码执行动画:

override func viewDidLoad() {
    super.viewDidLoad()
    UIView.animateWithDuration(0.5) { () -> Void in
        //self.ballView.center.x += self.view.frame.width
    }
    print(self.ballView.center.x)
    print(self.view.frame.width)
}

我面临的困惑是关于 ballView 和 superview(ViewController 的 View )的尺寸

这一行 self.ballView.center.x 返回 187.5 而 self.view.frame.width 返回 375.0,所以如果我执行注释行的代码,self.ballView.center.x += self.view.frame.width ballView 的 center.x 值不应该是 555.75,因此在 ViewControllers View 之外。相反,ballView 位于 self.view 的中心。请让我知道到底发生了什么。

最佳答案

改为将动画移动到 viewDidAppear 方法中。像这样:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    UIView.animateWithDuration(0.5) {
        self.ballView.center.x += self.view.frame.width
    }
}

关于iOS 动画 - View 应该在 super View 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35907591/

相关文章:

ios - 将 NSString 转换为 double - Objective-C

iOS Autolayout 常量问题导致 UIView 在 iPad 中不占据整个宽度

ios - 如何在 viewDidLoad() 方法上更新 Swift 3 中的 UIProgressView?

objective-c - 什么是 Objective-C 等同于 Swift 的 inout 参数?

ios - 在不使用 PrepareForSegue 的情况下在 segue 之间传递数据

iphone - 接受的方式让用户 "reset"应用程序设置(NSUserDefaults)?

ios - 与 watch 套件扩展和 iPhone 应用程序通信

ios - 具有 1 个数据源的 tableview 单元格内的 Collectionview

swift - 导航到其他页面后,如何在 SwiftUI 中恢复已发布的计时器?

iphone - 应用程序不改变方向