How to make UIView animation sequence repeat and autoreverse

后端 未结 3 1753
走了就别回头了
走了就别回头了 2020-12-02 08:29

How to make this complex animation repeat and autoreverse? Is there any way to add options UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat to this animation s

3条回答
  •  独厮守ぢ
    2020-12-02 09:09

    Swift 4 for simple wiggle sequence:

    func animateWiggle() {
        // Set animation props
        let scaleDelta = CGFloat(0.10)
    
        // Set transforms
        let wiggleOutHorizontally = CGAffineTransform(scaleX: 1.0 + scaleDelta, y: 1.0)
        let wiggleOutVertically = CGAffineTransform(scaleX: 1.0, y: 1.0 + scaleDelta)
    
        // Run animation sequence
        UIView.animateKeyframes(withDuration: 1.0, delay: 0.0, options: [.autoreverse, .repeat], animations: {
            // Animate wiggle horizontally
            UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: {
                self.transform = wiggleOutHorizontally
            })
    
            // Animate wiggle vertically
            UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: {
                self.transform = wiggleOutVertically
            })
        },
        completion: nil)
    }
    

提交回复
热议问题