我希望能够在每次按下按钮时将一个对象(在我的情况下,是一张图片“puppy”)向上移动1像素。我已经找到了一些旧的Objective-C解决方案以及类似的Swift代码,但都不适用于我的具体问题。我希望知道一个简单的方法来移动我的图片。这是我目前从收集的代码中得到的(我希望它不必要地长,并且可以缩短到一两行):
@IBAction func tapButton() {
UIView.animateWithDuration(0.75, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.puppy.alpha = 1
self.puppy.center.y = 0
}, completion: nil)
var toPoint: CGPoint = CGPointMake(0.0, 1.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = NSValue(CGPoint: fromPoint)
movement.toValue = NSValue(CGPoint: toPoint)
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}