pop动画

#import "ViewController.h"
#import "POP.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(log)];
//    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    return;

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
    label.backgroundColor = [UIColor redColor];
    [self.view addSubview:label];

    //核心动画
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //keypath类型要和fromValue、toValue类型匹配
//    ani.fromValue
//    ani.toValue = [NSValue valueWithCGRect:CGRectMake(200, 300, 50, 50)];
    //动画目标值
    ani.toValue = @(3);
    //动画时长
    ani.duration = 3;
    //动画结束是否移除
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;

    //自动变回去
    ani.autoreverses = YES;
    //添加动画

    [label.layer addAnimation:ani forKey:@"qwe"];

    //通过key值取到animation
//    label.layer animationForKey:<#(NSString *)#>

    //延时触发方法
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        NSLog(@"%@",NSStringFromCGRect(label.layer.frame));
//    });

//    [self performSelector:@selector(log) withObject:nil afterDelay:4];
}

- (void)log
{
    NSLog(@"delay!");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)spring:(id)sender {
    //弹性动画

    //iOS7.0后新出的弹性动画,Damping阻尼,Velocity速度
//    [UIView animateWithDuration:3 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//        [sender setFrame:CGRectMake(200, 100, 80, 40)];
//    } completion:nil];

    //frame变化
    POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    spring.toValue = [NSValue valueWithCGRect:CGRectMake(230, 40, 80, 40)];
//动画代理(代理函数包括:动画开始,动画结束,动画到达目标值、动画应用到控件上)
//    spring.delegate = self;
    //缩放
//    POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
////    spring.fromValue默认从当前位置开始
//    spring.fromValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
//    spring.toValue = [NSValue valueWithCGSize:CGSizeMake(3, 3)];

    //背景色变化
//    POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewBackgroundColor];
//    spring.toValue = [NSValue valueWithCGRect:CGRectMake(0.1, 0.2, 0.3, 1.0)];

    //设置振幅
    spring.springBounciness = 20;
    //振幅速度
    spring.springSpeed = 20;
//    spring.dynamicsMass = 100;
//    spring.dynamicsTension = 100;
//    spring.dynamicsFriction = 100;
    [sender pop_addAnimation:spring forKey:@"spring"];
}

//减速动画
- (IBAction)decay:(id)sender {
//    POPDecayAnimation *decay = [POPDecayAnimation animationWithPropertyNamed:kPOPViewCenter];
    //减速不能设置toValue
//    decay.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 100)];
//    decay.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
//    decay.velocity = [NSValue valueWithCGPoint:CGPointMake(200, 100)];

    //缩放
    POPDecayAnimation *decay = [POPDecayAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    decay.velocity = [NSValue valueWithCGSize:CGSizeMake(4, 4)];
    [sender pop_addAnimation:decay forKey:@"decay"];
}

- (IBAction)basic:(id)sender {
//    [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
    POPBasicAnimation *basic =
    [POPBasicAnimation animation];
    basic.property = [POPAnimatableProperty propertyWithName:kPOPViewFrame];

    basic.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 80, 40)];
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(200, 200, 80, 40)];
    //动画时长
    basic.duration = 2;
    [sender pop_addAnimation:basic forKey:@"basic"];
}

//自定义
- (IBAction)Custom:(id)sender {

    //block返回值为yes代表动画一直执行(block会继续执行),返回值no代表动画结束(block不会继续被执行)。
//    POPCustomAnimation *cus = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation *animation) {
//
//        //修改alpha值
//        CGFloat alpha = [target alpha];
//        alpha -= 0.005;
//        [target setAlpha:alpha];
//
//        //每次向右移动1
//        CGRect frame = [target frame];
//        frame.origin.x += 1;
//        [target setFrame:frame];
//        //如果x坐标大于等于200,则结束动画,否则继续动画
//        if (frame.origin.x >= 200) {
//            return NO;
//        }
//        return YES;
//    }];
//    [sender pop_addAnimation:cus forKey:@"custom"];

    //贝塞尔曲线创建圆路径(参数顺序:圆心、半径、起始角度、结束角度、是否顺时针绘制)
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:80 startAngle:M_PI endAngle:M_PI*(7/2) clockwise:YES];
    //移动到某个点
    [path moveToPoint:CGPointMake(120, 200)];
    //添加到另外一个点之间的线
    [path addLineToPoint:CGPointMake(180, 250)];
    //再次移动到某个点
    [path moveToPoint:CGPointMake(180, 250)];
    [path addLineToPoint:CGPointMake(250, 140)];

    //渲染路径的特殊layer
    CAShapeLayer *layer = [CAShapeLayer layer];
    //设置渲染路径
    layer.path = path.CGPath;
    //线条颜色
    layer.strokeColor = [[UIColor redColor]CGColor];
    //填充颜色
    layer.fillColor = [[UIColor whiteColor]CGColor];
    //线条宽度
    layer.lineWidth = 5;
    //添加到当前页面的layer上
    [self.view.layer addSublayer:layer];

    //核心动画
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//    animation.toValue = @(layer.strokeEnd);
//    animation.fromValue = @(layer.strokeStart);
//    animation.duration = 2;
//    [layer addAnimation:animation forKey:@"coreAnimation"];
//    return;

    //popcustom动画
    POPCustomAnimation *layCus = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation *animation) {

        CAShapeLayer *shapeLayer = target;
        shapeLayer.strokeEnd += 0.01;
        if (shapeLayer.strokeEnd >= 1) {
            return NO;
        }
        return YES;
    }];
    layer.strokeEnd = 0;
    [layer pop_addAnimation:layCus forKey:@"custom"];
}

- (IBAction)property:(id)sender {

    //自定义property
    POPBasicAnimation *animation = [POPBasicAnimation animation];
    //设置动画规则
    animation.property = [POPAnimatableProperty propertyWithName:@"customProperty" initializer:^(POPMutableAnimatableProperty *prop) {
        //从values数组中取值显示到target上
        [prop setWriteBlock:^(id target, const CGFloat *values) {
            [target setAlpha:values[0]];
        }];

        //从target取值赋值到values数组中
        [prop setReadBlock:^(id target, CGFloat *values) {
            values[0] = [target alpha];
        }];
    }];
//    //让button变成透明
//    animation.toValue = @(0);
//    animation.property = [POPAnimatableProperty propertyWithName:@"increase" initializer:^(POPMutableAnimatableProperty *prop) {
//
//        [prop setWriteBlock:^(id target, const CGFloat *values) {
//            [target setTitle:[NSString stringWithFormat:@"%.f",values[0]] forState:UIControlStateNormal];
//        }];
//
//        [prop setReadBlock:^(id target, CGFloat *values) {
//            values[0] = [target titleLabel].text.floatValue;
//        }];
//    }];
//    animation.toValue = @(1000);

//    animation.property = [POPAnimatableProperty propertyWithName:@"size" initializer:^(POPMutableAnimatableProperty *prop) {
//       //writeblock和readblock正好是相反的两个过程
//        [prop setWriteBlock:^(id target, const CGFloat *values) {
//
//            CGRect frame = [target frame];
//            frame.size.width = values[0];
//            frame.size.height = values[1];
//            [target setFrame:frame];
//
//        }];
//
//        [prop setReadBlock:^(id target, CGFloat *values) {
//            CGRect frame = [target frame];
//            values[0] = frame.size.width;
//            values[1] = frame.size.height;
//        }];
//    }];
//    animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(80, 40)];
//    animation.toValue = [NSValue valueWithCGSize:CGSizeMake(200, 200)];

//    animation.property = [POPAnimatableProperty propertyWithName:@"qwer" initializer:^(POPMutableAnimatableProperty *prop) {
//       [prop setReadBlock:^(id target, CGFloat *values) {
//           CGRect frame = [target frame];
//           values[0] = frame.origin.x;
//           values[1] = frame.origin.y;
//           values[2] = [target titleLabel].text.floatValue;
//           values[3] = 0;
//       }];
//
//        [prop setWriteBlock:^(id target, const CGFloat *values) {
//            CGRect frame = [target frame];
//            NSLog(@"%f-%f-%f-%f",values[0],values[1],values[2],values[3]);
//            frame.origin.x = values[0];
//            frame.origin.y = values[1];
//            [target setFrame:frame];
//            [target setTitle:[NSString stringWithFormat:@"%.f",values[2]] forState:UIControlStateNormal];
//        }];
//    }];
//    animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
//    animation.toValue = [NSValue valueWithCGRect:CGRectMake(300, 300, 1000, 0)];
    //设置动画时长
    animation.duration = 2;
    [sender pop_addAnimation:animation forKey:@"animation"];
}

@end
时间: 2024-10-23 02:06:15

pop动画的相关文章

POP动画引擎中Layer与CALayer的一点区别

POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步: https://github.com/facebook/pop 下面简单的讲一下POP动画引擎中Layer与CALayer的区别: 这里, 代码做的都是同一个效果: 执行位移动画3秒, 然后在1秒后移除所有动画 使用POP效果图: 使用CALayer效果图: 可以看到, POP执行的动画当动画被移除之后, 被执行对象保留了被停止时的状态 而CALayer执行的动画当动画被移

POP动画[2]

POP动画[2] 1:定制控制器间的转场动画. 源码有点多-_-!! // // RootViewController.h // Animation // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end RootViewController.h // // RootViewContro

自定义UINavigationController push和pop动画

http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界面切换的动画效果复杂一点,大概有这么几个: 更快的左右推出,默认是0.3秒,我需要快一倍 带抖动的左右推出 水平翻转 纸张翻页效果 但是这些切换都要放在NavigationController里做管理,怎么实现,求个思路 ios 链接 评论 更多 默认排序时间排序 3 个回答 答案对人有帮助,有参考

POP动画[1]

POP动画[1] pop动画是facebook扩展CoreAnimation的,使用及其方便:) 1:Spring系列的弹簧效果(两个动画kPOPLayerBounds与kPOPLayerCornerRadius同时运行) #import "RootViewController.h" #import "YXEasing.h" #import "POP.h" #import "YXGCD.h" @interface RootVi

swift详解之二十七------------自定义UINavigationController的push和pop动画

自定义UINavigationController的push和pop动画 我们这里先创建一个简单的工程 , 在storyboard 中拖一个导航控制器 , rootViewController 改成我们的ViewController . 为了实现自定义动画切换 , 我们需要实现两个协议 . UIViewControllerAnimatedTransitioning,UINavigationControllerDelegate UIViewControllerAnimatedTransitioni

Facebook POP动画简单使用

简单实用POP动画 发现POP比较好的一点是保留了动画结束后的状态,通过block回调.使用POPAnimatableProperty 可以快速添加基本动画,也可以自定义属性动画. 弹性动画 - (void)spring{ POPSpringAnimation* framePOP = [POPSpringAnimation animationWithPropertyNamed:kPOPViewBackgroundColor]; framePOP.springSpeed = 10.f; frame

POP动画[3]

这一节主要讲解POP动画的自定义动画属性. POP动画中有一个参数,叫timingFunction,与CoreAnimation中的一个参数CAMediaTimingFunction基本一样,下图表示的是kCAMediaTimingFunctionEaseInEaseOut的曲线图. 下图是Spring动画效果: 我们可以使用自定义的属性来实现POP的库中没有提供的动画. 实现的效果: 源码: // // RootViewController.m // YXPOP // // Copyright

用POP动画引擎实现弹簧动画(POPSpringAnimation)

效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @property (nonatomic, weak) UIView *testView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor

用POP动画模拟真实秒钟摆动效果

静态图: 动画图: 此处用到了POP中的Spring系列动画,现提供源码如下: SecondClockView.h 与 SecondClockView.m // // SecondClockView.h // YouXianMingClock // // Created by YouXianMing on 14-10-12. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @