How do you create a wiggle animation similar to iphone deletion animation

前端 未结 8 2330
-上瘾入骨i
-上瘾入骨i 2020-12-12 16:57

We are currently developing an application that contains a series of icons. We want the icons to wiggle like the app deletion animations when pressed. What would be the be

8条回答
  •  猫巷女王i
    2020-12-12 17:26

        func startWiggling() {
            deleteButton.isHidden = false
            guard contentView.layer.animation(forKey: "wiggle") == nil else { return }
            guard contentView.layer.animation(forKey: "bounce") == nil else { return }
    
            let angle = 0.04
    
            let wiggle = CAKeyframeAnimation(keyPath: "transform.rotation.z")
            wiggle.values = [-angle, angle]
            wiggle.autoreverses = true
            wiggle.duration = randomInterval(0.1, variance: 0.025)
            wiggle.repeatCount = Float.infinity
            contentView.layer.add(wiggle, forKey: "wiggle")
    
            let bounce = CAKeyframeAnimation(keyPath: "transform.translation.y")
            bounce.values = [4.0, 0.0]
            bounce.autoreverses = true
            bounce.duration = randomInterval(0.12, variance: 0.025)
            bounce.repeatCount = Float.infinity
            contentView.layer.add(bounce, forKey: "bounce")
        }
    
        func stopWiggling() {
            deleteButton.isHidden = true
            contentView.layer.removeAllAnimations()
            }
    
        func randomInterval(_ interval: TimeInterval, variance: Double) -> TimeInterval {
            return interval + variance * Double((Double(arc4random_uniform(1000)) - 500.0) / 500.0)
        }
    

    Look at this iOS SpingBoard example

    springboard

提交回复
热议问题