How to make “shaking” animation

后端 未结 4 1040
栀梦
栀梦 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:24

    func shakeView(_ view: UIView?) {
        let shake = CABasicAnimation(keyPath: "position")
        shake.duration = 0.1
        shake.repeatCount = 2
        shake.autoreverses = true
        shake.fromValue = NSValue(cgPoint: CGPoint(x: (view?.center.x)! - 5, y: view?.center.y ?? 0.0))
        shake.toValue = NSValue(cgPoint: CGPoint(x: (view?.center.x)! + 5, y: view?.center.y ?? 0.0))
        view?.layer.add(shake, forKey: "position")
    }
    

提交回复
热议问题