How to make “shaking” animation

后端 未结 4 1037
栀梦
栀梦 2021-02-01 07:41

In my xcode project I have an UIImage. I want to make it shaking (animation) from left to right a little bit. I wrote this code, however, its not working.

-(void         


        
4条回答
  •  野的像风
    2021-02-01 08:20

    brynbodayles answer also works under iOS8 for a shake animation in swift.

    With Import of QuartzCore:

    func shakeView(){
        var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
        shake.duration = 0.1
        shake.repeatCount = 2
        shake.autoreverses = true
    
        var from_point:CGPoint = CGPointMake(LoginField.center.x - 5, LoginField.center.y)
        var from_value:NSValue = NSValue(CGPoint: from_point)
    
        var to_point:CGPoint = CGPointMake(LoginField.center.x + 5, LoginField.center.y)
        var to_value:NSValue = NSValue(CGPoint: to_point)
    
        shake.fromValue = from_value
        shake.toValue = to_value
        LoginField.layer.addAnimation(shake, forKey: "position")
    }
    

提交回复
热议问题