Swift中UIImageView的无限摇动动画

时间:2015-04-16 19:55:30

标签: ios swift

我试图让UIImageView无限期地动摇。

        var coffeeImageView = UIImageView(image: UIImage(named: "coffee.png"))
        coffeeImageView.frame = CGRectMake(100, self.view.frame.size.height - 100, 50, 50)
        self.view.addSubview(coffeeImageView)

        let coffeeShakeAnimation = CABasicAnimation(keyPath: "transform")
        coffeeShakeAnimation.duration = 0.07
        coffeeShakeAnimation.repeatCount = 20
        coffeeShakeAnimation.autoreverses = true
        coffeeShakeAnimation.fromValue = NSValue(CGPoint: CGPointMake(coffeeImageView.center.x - 10, coffeeImageView.center.y))
        coffeeShakeAnimation.toValue = NSValue(CGPoint: CGPointMake(coffeeImageView.center.x + 10, coffeeImageView.center.y))
        coffeeImageView.layer.addAnimation(coffeeShakeAnimation, forKey: "position")

1 个答案:

答案 0 :(得分:1)

替换

let coffeeShakeAnimation = CABasicAnimation(keyPath: "transform")

通过

let coffeeShakeAnimation = CABasicAnimation(keyPath: "position")

它会动摇