我有一个看起来应该很简单的问题。基本上,我试图将一个对象(一个UIImageView)从点A(在故事板中设置)移动到我程序定义的点B。
我的第一次尝试导致了这段代码:
UIView.animateWithDuration(0.75, delay: 0.5, options: UIViewAnimationOptions.CurveLinear, animations: {
self.signInButton.alpha = 1
self.signInButton.center.y = 10
}, completion: nil)
然而,这段代码的作用基本上是将按钮移动到偏离中心的位置,然后再移回原来的位置。
接着我研究了QuartzCore来帮助我,但所有内容都是Objective-C。我有这个方法:
func moveImage(view: UIImageView){
var toPoint: CGPoint = CGPointMake(0.0, -10.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = fromPoint
movement.toValue = toPoint
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
然而,这里的问题是
movement.fromValue
无法接受CGPoint
。我知道在Objective C中有一个函数可以将CGPoint
转换为NSValue
,但是这个函数似乎已经过时了,在Swift中找不到其他方法来解决这个问题。因此,我的问题要么是,如何将
CGPoint
转换为NSValue
使我的moveImage()
函数工作,要么是有更好的方法将对象从A点移动到B点?谢谢!
我正在查看这个问题Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop