iOS10新特性之UIViewPropertyAnimator的使用

来源:互联网 发布:中科软件 编辑:程序博客网 时间:2024/05/02 05:00

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

OC :

#import "ViewController.h"@interface ViewController ()@property(nonatomic) UIView *views;@property(nonatomic) UIViewPropertyAnimator *animator;@end@implementation ViewController- (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 UIKitclass ViewController: 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    }    @IBAction func startAnimate(_ sender: Any) {        self.animator.startAnimation()    }    @IBAction func pauseAnimate(_ sender: Any) {        self.animator.pauseAnimation()    }    @IBAction func continueAnimate(_ sender: Any) {        //参数 dampingRatio,阻尼系数越小弹性越大(0-1)        let param = UISpringTimingParameters(dampingRatio:0.1)        self.animator.continueAnimation(withTimingParameters: param, durationFactor: 1)    }    @IBAction func stopAnimate(_ sender: Any) {        //是否要在动画执行完毕后再停止         self.animator.stopAnimation(false)    }}

拖动放大图的demo:

import UIKitclass 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        }}}

效果图:
这里写图片描述

0 0
原创粉丝点击