美文网首页
iOS10新特性之UIViewPropertyAnimator的

iOS10新特性之UIViewPropertyAnimator的

作者: 咖啡绿茶1991 | 来源:发表于2018-05-17 17:25 被阅读0次

    UIViewPropertyAnimator是iOS的新特性,通过UIViewPropertyAnimator你可以细粒度控制自己制作的动画,易于抹除、逆向、暂停和重启动画,并重构动画帧使之平滑流畅。这些功能也可以用于控制器的转场动画。

    OC :

    #import"ViewController.h"

    @interfaceViewController()

    @property(nonatomic) UIView *views;

    @property(nonatomic) UIViewPropertyAnimator *animator;

    @end

    @implementationViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

        view.backgroundColor = [UIColor redColor];

        [self.view addSubview:view];

        self.views = view;

        //初始化动画器    UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc]initWithDuration:4 curve:UIViewAnimationCurveLinear animations:^{

            self.views.frame = CGRectMake(200, 200, 200, 200);

        }];

        //添加动画结束    [animator addCompletion:^(UIViewAnimatingPosition finalPosition) {

            if(finalPosition == UIViewAnimatingPositionEnd){

                self.views.frame = CGRectMake(100, 100, 100, 100);

            }

        }];

        self.animator = animator;

    }

    - (IBAction)start:(UIButton *)sender {

        [self.animator startAnimation];

    }

    - (IBAction)pause:(id)sender {

        [self.animator pauseAnimation];

    }

    - (IBAction)continue:(id)sender {

        //参数 dampingRatio,阻尼系数越小弹性越大(0-1)    UISpringTimingParameters *params = [[UISpringTimingParameters alloc]initWithDampingRatio:0.6];

        [self.animator continueAnimationWithTimingParameters:params durationFactor:1];

    }

    - (IBAction)stop:(id)sender {

        [self.animator stopAnimation:false];

    }

    swift:

    import UIKit

    classViewController:UIViewController

     var v:UIView! 

     var animator:UIViewPropertyAnimator! 

     override func viewDidLoad() { 

     super.viewDidLoad()

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

     v.backgroundColor =UIColor.red 

     view.addSubview(v)

    self.v = v 

     //初始化动画器

     let animator =UIViewPropertyAnimator(duration: 4,curve:UIViewAnimationCurve.linear){ v.frame =CGRect(x:200,y:200,width:200,height:200)

     animator.addCompletion {(UIViewAnimatingPosition)in v.frame =CGRect(x:100,y:100,width:100,height:100)

     self.animator = animator 

     } 

     @IBActionfunc startAnimate(_sender:Any) {

     self.animator.startAnimation()

     @IBActionfunc pauseAnimate(_sender:Any) {

     self.animator.pauseAnimation()

    }

     @IBActionfunc continueAnimate(_sender:Any) {

     //参数 dampingRatio,阻尼系数越小弹性越大(0-1)

    let param =UISpringTimingParameters(dampingRatio:0.1)

    self.animator.continueAnimation(withTimingParameters:param,durationFactor: 1)

     @IBActionfunc stopAnimate(_sender:Any) {

     //是否要在动画执行完毕后再停止 

     self.animator.stopAnimation(false)

    }

    }

    拖动放大图的demo:

    import UIKit

    class ViewController: UIViewController {

        // 记录拖动时的圆形视图 center

        var circleCenter: CGPoint!

        // 我们将在拖拽响应事件上附加不同的动画

        var circleAnimator: UIViewPropertyAnimator!

        let animationDuration = 4.0    override func viewDidLoad() {

            super.viewDidLoad()

            // 添加可拖动视图

            let circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))

            circle.center = self.view.center        circle.layer.cornerRadius = 50.0        circle.backgroundColor = UIColor.green        // 添加拖动手势

            circle.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragCircle)))        // 可选动画参数

            circleAnimator = UIViewPropertyAnimator(duration: animationDuration, curve: .easeOut, animations: {

                // 放大凉别

                circle.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)

            })

            self.view.addSubview(circle)

        }

        //拖动事件

        func dragCircle(gesture: UIPanGestureRecognizer) {

            let target = gesture.view!

            //methond 1//        switch gesture.state {

    //        case .began:

    //            circleCenter = target.center//        case .changed:

    //            let translation = gesture.translation(in: self.view)

    //            target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)

    //            //传入一个 0.0-1.0 的浮点数,使其在对应位置暂停

    //            circleAnimator?.fractionComplete = target.center.y / self.view.frame.height//        default: break//        }

            //methond 2        switch gesture.state {

            case .began, .ended:

                circleCenter = target.center            if circleAnimator.state == .active {

                    // 使animator为inactive状态

                    circleAnimator.stopAnimation(true)

                }

                if (gesture.state == .began) {

                    circleAnimator.addAnimations({

                        target.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)

                    })

                }

                else {

                    circleAnimator.addAnimations({

                        target.transform = CGAffineTransform.identity                })

                }

                circleAnimator.startAnimation()

            case .changed:

                let translation = gesture.translation(in: self.view)

                target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)

            default: break        }

    }

    }

    效果图: 

    转载自:https://blog.csdn.net/zww1984774346/article/details/58089754

    相关文章

      网友评论

          本文标题:iOS10新特性之UIViewPropertyAnimator的

          本文链接:https://www.haomeiwen.com/subject/lcqidftx.html