我创建了一个 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/