zoukankan      html  css  js  c++  java
  • 使用 Facebook开源动画库 POP 实现真实衰减动画

    1. POP动画基于底层刷新原理。是基于CADisplayLink,1秒钟运行60秒,接近于游戏开发引擎


    @interface ViewController ()

    @property (nonatomic,strong)CADisplayLink *displayLink;

    @property (nonatomic)      NSInteger     count;

    @end


    - (void)viewDidLoad {

        [superviewDidLoad];

        

        self.displayLink = [CADisplayLinkdisplay LinkWithTarget:self

                                                       selector:@selector(displayLinkEvent:)];

        

        [self performSelector:@selector(eventOne)

                   withObject:nil

                   afterDelay:1];

        

        [self performSelector:@selector(eventTwo)

                   withObject:nil

                   afterDelay:2];

    }


    - (void)displayLinkEvent:(id)obj

    {

        self.count ++;

        NSLog(@"count = %ld",(long)self.count);

    }



    - (void)eventOne{

        [self.displayLink addToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];

    }


    - (void)eventTwo{

        [self.displayLink invalidate];

    }

    二、POP动画引擎中 Layer CALayer的联系与差别

    1.使用pop动画与使用CALayer动画很相似

    2.POP动画的运行没有中间状态

    3.POP动画是对CALayer的动画的扩充,但不能实现全部的CALayer的动画效果

    4.POP动画能够作用在不论什么对象上,不不过CALayer



    - (void)accessNormalLayer{

        self.normalLayer = [CALayerlayer];

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

        self.normalLayer.backgroundColor = [UIColorredColor].CGColor;

        [self.view.layeraddSublayer:self.normalLayer];

        

        //

        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

        basicAnimation.fromValue    = [NSValue valueWithCGPoint:self.normalLayer.position];

        basicAnimation.toValue      = [NSValue valueWithCGPoint:CGPointMake(100 +50,400)];

        basicAnimation.duration     = 4.0;

        basicAnimation.timingFunction =

        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

        // layerpostion相当于viewcenter

        self.normalLayer.position =CGPointMake(100 +50,400);

        

        [self.normalLayer addAnimation:basicAnimationforKey:nil];

        

        //1.4秒移除后,动画直接到终点

        [self performSelector:@selector(remoVeNormalAnimation)withObject:nilafterDelay:1.4];

    }


    - (void)remoVeNormalAnimation{

        CALayer *layer = self.normalLayer.presentationLayer;

        NSLog(@"%@",NSStringFromCGRect(layer.frame));

        NSLog(@"%@",NSStringFromCGRect(self.normalLayer.frame));

        

        [self.normalLayer removeAllAnimations];

    }

    三、用 POP动画引擎实现衰减动画

    1.衰减动画由POPDecayAnimaiton来实现

    2.须要精确计算停止运动瞬间的加速度才干用衰减动画做出真实的效果

    - (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer{

        //获取定位点

        CGPoint translation = [recognizer translationInView:self.view];

        

        //recognizer.view.center 指的是button

        recognizer.view.center =CGPointMake(recognizer.view.center.x + translation.x,

                                            recognizer.view.center.y +translation.y);

        

        //让他恢复坐标系

        [recognizer setTranslation:CGPointMake(0,0)inView:self.view];

        

        if (recognizer.state ==UIGestureRecognizerStateChanged) {

            NSLog(@"手势停止时运行这一句话");

            //获取加速度

            CGPoint velocity = [recognizer velocityInView:self.view];

            //初始化POPdeacy(衰减)动画

            POPDecayAnimaton *decayAnimation =

            [POPDecayAnimation animationWithPropertyName:kPOPLayerPosition];

            decayAnimation.velocity = [NSValue valueWithCGPoint:velocity];

            [recognizer.view.layer pop_addAnimation:decayAnimation forKey:nil];

        }

    }


    - (void)buttonEvent:(UIButton *)button

    {

        //[button.layer pop_removeAllAnimations];

    }


    - (void)viewDidLoad {

        [superviewDidLoad];

        self.button = [[UIButton alloc]initWithFrame:CGRectMake(100,100,100,100)];

        self.button .backgroundColor = [UIColorredColor];

        self.button.layer.cornerRadius =50;

        self.button.layer.masksToBounds =YES;

        self.button.center =self.view.center;

        [self.viewaddSubview:self.button];

        [self.buttonaddTarget:self

                       action:@selector(buttonEvent:)

             forControlEvents:UIControlEventTouchDragInside];

        

        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePanGesture:)];

        [self.buttonaddGestureRecognizer:panGesture];

        

       

        Facebook官方的pop动画:附链接https://github.com/schneiderandre/popping





  • 相关阅读:
    马云:员工的离职原因--转载
    zookeeper源码分析之五服务端(集群leader)处理请求流程
    技术高手如何炼成?--转自知乎
    一次上线事故经验
    zookeeper源码分析之四服务端(单机)处理请求流程
    AngularJS2.0 quick start——其和typescript结合需要额外依赖
    typescript 入门例子 Hello world——ts就是一个宿主机语言
    Kubernetes——自动扩展容器!假设你突然需要增加你的应用;你只需要告诉deployment一个新的 pod 副本总数即可
    Kubernetes——基于容器技术的分布式架构领先方案,它的目标是管理跨多个主机的容器,提供基本的部署,维护以及运用伸缩
    华为FusionSphere概述——计算资源、存储资源、网络资源的虚拟化,同时对这些虚拟资源进行集中调度和管理
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7010929.html
Copyright © 2011-2022 走看看