How to make “shaking” animation

后端 未结 4 1035
栀梦
栀梦 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

    From Corona answer

    Give shake animation in multiple buttons and also in views SWIFT3

    for view animation just change extension UIButton to extension UIView

    extension UIButton
    {
        func shakeAnim ()
        {
            let shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
            shake.duration = 0.1
            shake.repeatCount = 2
            shake.autoreverses = true
    
            let from_point:CGPoint = CGPoint(x:self.center.x - 5,y: self.center.y)
            let from_value:NSValue = NSValue(cgPoint: from_point)
    
            let to_point:CGPoint = CGPoint(x:self.center.x + 5,y: self.center.y)
            let to_value:NSValue = NSValue(cgPoint: to_point)
    
            shake.fromValue = from_value
            shake.toValue = to_value
            self.layer.add(shake, forKey: "position")
       }
    }
    

提交回复
热议问题