基础回弹动画

1. 基础回弹动画

使用 usingSpringWithDamping

swift

// 基础回弹动画

UIView.animate(

    withDuration: 0.6,

    delay:0.0,

    usingSpringWithDamping: 0.6,  // 阻尼系数 (0.0-1.0)

    initialSpringVelocity: 0.8,  // 初始速度

    options: [],

    animations: {

        // 动画属性变化

        self.animatedView.center.y+=100

        self.animatedView.transform=CGAffineTransform(scaleX:1.2,y:1.2)

    },

    completion: { finishedin

        print("回弹动画完成")

    }

)



不同类型的回弹效果

swift

class BounceAnimationDemo: UIViewController {


    letanimatedView=UIView(frame:CGRect(x:100,y:100,width:100,height:100))


    override func viewDidLoad() {

        super.viewDidLoad()

        setupView()

    }


    private func setupView() {

        animatedView.backgroundColor = .systemBlue

        animatedView.layer.cornerRadius = 12

        view.addSubview(animatedView)


        // 添加按钮测试不同效果

        addTestButtons()

    }


    // MARK: - 不同类型的回弹效果


    /// 轻微回弹效果

    func gentleBounce() {

        UIView.animate(

            withDuration:0.8,

            delay:0.0,

            usingSpringWithDamping: 0.7,  // 较高的阻尼 = 较少回弹

            initialSpringVelocity: 0.5,

            options: [],

            animations: {

                self.animatedView.center.y+=150

            }

        )

    }


    /// 强烈回弹效果

    func strongBounce() {

        UIView.animate(

            withDuration:1.2,

            delay:0.0,

            usingSpringWithDamping: 0.3,  // 较低的阻尼 = 更多回弹

            initialSpringVelocity: 1.0,

            options: [],

            animations: {

                self.animatedView.center.y+=150

            }

        )

    }


    /// 缩放回弹效果

    func scaleBounce() {

        UIView.animate(

            withDuration:0.6,

            delay:0.0,

            usingSpringWithDamping: 0.5,

            initialSpringVelocity: 0.8,

            options: [],

            animations: {

                self.animatedView.transform=CGAffineTransform(scaleX:1.5,y:1.5)

            }

        )

    }


    /// 旋转回弹效果

    func rotateBounce() {

        UIView.animate(

            withDuration:0.8,

            delay:0.0,

            usingSpringWithDamping: 0.6,

            initialSpringVelocity: 0.5,

            options: [],

            animations: {

                self.animatedView.transform = CGAffineTransform(rotationAngle: .pi / 4)

            }

        )

    }


    /// 组合动画:移动 + 缩放 + 旋转

    func complexBounce() {

        UIView.animate(

            withDuration:1.0,

            delay:0.0,

            usingSpringWithDamping: 0.5,

            initialSpringVelocity: 0.7,

            options: [],

            animations: {

                self.animatedView.center.y+=100

                self.animatedView.center.x+=50

                self.animatedView.transform=CGAffineTransform(scaleX:1.3,y:1.3)

                    .rotated(by: .pi/6)

            }

        )

    }

}




高级回弹动画类

swift

class BounceAnimator {


    enum BounceType {

        casegentle      // 轻微回弹

        casemedium      // 中等回弹

        casestrong      // 强烈回弹

        casecustom(damping:CGFloat, velocity:CGFloat)

    }


    /// 执行回弹动画

    static func animate(view: UIView,

                       type:BounceType= .medium,

                       duration:TimeInterval=0.6,

                       delay:TimeInterval=0.0,

                       animations:@escaping() ->Void,

                       completion: ((Bool) ->Void)? =nil) {


        let(damping, velocity) =getParameters(for: type)


        UIView.animate(

            withDuration: duration,

            delay: delay,

            usingSpringWithDamping: damping,

            initialSpringVelocity: velocity,

            options: [],

            animations: animations,

            completion: completion

        )

    }


    /// 获取动画参数

    privatestaticfuncgetParameters(fortype:BounceType) -> (damping:CGFloat, velocity:CGFloat) {

        switchtype {

        case.gentle:

            return(0.7,0.5)

        case.medium:

            return(0.5,0.7)

        case.strong:

            return(0.3,1.0)

        case.custom(letdamping,letvelocity):

            return(damping, velocity)

        }

    }


    // MARK: - 预设动画效果


    /// 弹入动画(从屏幕外弹入)

    static func bounceIn(view: UIView,

                        fromdirection:Direction= .bottom,

                        type:BounceType= .medium) {


        letoriginalTransform = view.transform

        letoriginalCenter = view.center


        // 根据方向设置初始位置

        switchdirection {

        case.top:

            view.center.y-=UIScreen.main.bounds.height

        case.bottom:

            view.center.y+=UIScreen.main.bounds.height

        case.left:

            view.center.x-=UIScreen.main.bounds.width

        case.right:

            view.center.x+=UIScreen.main.bounds.width

        }


        view.alpha=0

        view.transform= view.transform.scaledBy(x:0.5,y:0.5)


        animate(view: view, type: type, duration: 0.8, delay: 0.0, animations: {

            view.center= originalCenter

            view.transform= originalTransform

            view.alpha=1

        })

    }


    /// 弹出动画(弹到屏幕外)

    static func bounceOut(view: UIView,

                         todirection:Direction= .bottom,

                         type:BounceType= .medium,

                         completion: ((Bool) ->Void)? =nil) {


        letscreenBounds =UIScreen.main.bounds


        animate(view: view, type: type, duration: 0.6, delay: 0.0, animations: {

            view.alpha=0

            view.transform= view.transform.scaledBy(x:0.5,y:0.5)


            switchdirection {

            case.top:

                view.center.y-= screenBounds.height

            case.bottom:

                view.center.y+= screenBounds.height

            case.left:

                view.center.x-= screenBounds.width

            case.right:

                view.center.x+= screenBounds.width

            }

        },completion: completion)

    }


    ///心跳效果

    static func heartbeat(view: UIView) {

        animate(view: view, type: .strong, duration: 0.3, animations: {

            view.transform=CGAffineTransform(scaleX:1.3,y:1.3)

        },completion: { _in

            animate(view: view,type: .medium,duration:0.3,animations: {

                view.transform=CGAffineTransform.identity

            })

        })

    }


    ///震动效果

    staticfuncshake(view:UIView) {

        letshakeDistance:CGFloat=10


        animate(view: view, type: .strong, duration: 0.1, animations: {

            view.center.x+= shakeDistance

        },completion: { _in

            animate(view: view,type: .strong,duration:0.1,animations: {

                view.center.x-= shakeDistance *2

            },completion: { _in

                animate(view: view,type: .strong,duration:0.1,animations: {

                    view.center.x+= shakeDistance

                })

            })

        })

    }


    enum Direction {

        case top, bottom, left, right

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容