POP动画[1]

POP动画[1]

pop动画是facebook扩展CoreAnimation的,使用及其方便:)

 

1:Spring系列的弹簧效果(两个动画kPOPLayerBounds与kPOPLayerCornerRadius同时运行)

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化View
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    showView.center  = self.view.center;
    showView.layer.cornerRadius = 25;
    showView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:showView];

    // 延时7s后执行的代码
    [[GCDQueue mainQueue] execute:^{

        // 尺寸
        POPSpringAnimation *bounds = \
        [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];

        // 圆角
        POPSpringAnimation *cornerRadius = \
        [POPSpringAnimation animationWithPropertyNamed:kPOPLayerCornerRadius];

        bounds.toValue     = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
        bounds.springSpeed = 0;

        cornerRadius.toValue     = @(100);
        cornerRadius.springSpeed = 0;

        // 添加动画
        [showView.layer pop_addAnimation:bounds
                                  forKey:@"size"];
        [showView.layer pop_addAnimation:cornerRadius
                                  forKey:@"cornerRadius"];

    } afterDelay:NSEC_PER_SEC * 7];
}

@end

2:一个动画结束后开始另外一个动画

//
//  RootViewController.m
//  Animation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"

@interface RootViewController ()<POPAnimationDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化View
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    showView.center  = self.view.center;
    showView.layer.cornerRadius = 25;
    showView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:showView];

    // 延时7s后执行的代码
    [[GCDQueue mainQueue] execute:^{

        // 位置
        POPSpringAnimation *position = \
            [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];

        // 设置速度
        position.springSpeed = 0.f;

        // 赋值
        position.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];

        // 添加动画
        [showView.layer pop_addAnimation:position forKey:nil];

        // 结束后
        [position setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
            // 颜色
            POPSpringAnimation *backgroundColor = \
                [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];

            // 速度
            backgroundColor.springSpeed = 0.f;

            // 赋值
            backgroundColor.toValue = (id)[UIColor redColor].CGColor;

            // 添加动画
            [showView.layer pop_addAnimation:backgroundColor forKey:nil];
        }];
    } afterDelay:NSEC_PER_SEC * 7];
}

@end

注意动画类型的不同导致toValue的值也不一样,这个始于CALayer的动画保持一致的:

 

3:动画中的代理

//
//  RootViewController.m
//  Animation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"

@interface RootViewController ()<POPAnimationDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化View
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    showView.center  = self.view.center;
    showView.layer.cornerRadius = 25;
    showView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:showView];

    // 延时7s后执行的代码
    [[GCDQueue mainQueue] execute:^{

        // 尺寸
        POPSpringAnimation *bounds = \
            [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];

        // 设置代理
        bounds.delegate = self;

        bounds.toValue     = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
        bounds.springSpeed = 0;

        // 添加动画
        [showView.layer pop_addAnimation:bounds
                                  forKey:@"size"];

    } afterDelay:NSEC_PER_SEC * 7];
}

// 动画开始
- (void)pop_animationDidStart:(POPAnimation *)anim
{
    NSLog(@"pop_animationDidStart %@", anim);
}

// 动画值动态改变
- (void)pop_animationDidApply:(POPAnimation *)anim
{
    NSLog(@"pop_animationDidApply %@", anim);
}

// 动画到达终点值
- (void)pop_animationDidReachToValue:(POPAnimation *)anim
{
    NSLog(@"pop_animationDidReachToValue %@", anim);
}

// 动画结束
- (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished
{
    NSLog(@"pop_animationDidStop %@", anim);
}

@end

动画代理方法能够完整的表示出这个动画执行的过程,从开始到结束到中间值的改变我们都能获取到的.

 

4:按钮的动画效果

//
//  RootViewController.m
//  Animation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"

@interface RootViewController ()<POPAnimationDelegate>

@property (nonatomic, strong) UIButton    *button;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 完整显示按住按钮后的动画效果
    _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
    _button.layer.cornerRadius = 5.f;
    _button.backgroundColor = [UIColor cyanColor];
    _button.center = self.view.center;
    [self.view addSubview:_button];

    // 按住按钮后没有松手的动画
    [_button addTarget:self
                action:@selector(scaleToSmall)
      forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];

    // 按住按钮松手后的动画
    [_button addTarget:self
                action:@selector(scaleAnimation)
      forControlEvents:UIControlEventTouchUpInside];

    // 按住按钮后拖拽出去的动画
    [_button addTarget:self
                action:@selector(scaleToDefault)
      forControlEvents:UIControlEventTouchDragExit];
}

- (void)scaleToSmall
{
    NSLog(@"scaleToSmall");

    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.75f, 0.75f)];
    [_button.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSmallAnimation"];
}

- (void)scaleAnimation
{
    NSLog(@"scaleAnimation");

    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.springBounciness = 18.0f;
    [_button.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
}

- (void)scaleToDefault
{
    NSLog(@"scaleToDefault");

    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    [_button.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
}

@end

POP的动画真心强大呢:)

 

5:Stroke动画效果

//
//  RootViewController.m
//  Animation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"
#import "CAShapeLayer+Circle.h"

@interface RootViewController ()<POPAnimationDelegate>

@property (nonatomic, strong) GCDTimer  *timer;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 圆
    CAShapeLayer *layer = [CAShapeLayer LayerWithCircleCenter:self.view.center
                                                       radius:50.f
                                                   startAngle:DEGREES(180)
                                                     endAngle:DEGREES(180 + 360)
                                                    clockwise:YES
                                              lineDashPattern:nil];

    layer.strokeColor   = [UIColor cyanColor].CGColor;    // 边缘线的颜色
    layer.lineCap       = kCALineCapRound;                // 边缘线的类型
    layer.lineWidth     = 5.0f;                           // 线条宽度
    layer.strokeStart   = 0.0f;
    layer.strokeEnd     = 1.0f;

    [self.view.layer addSublayer:layer];

    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{

        CGFloat value1 = arc4random()%100/100.f;
        CGFloat value2 = arc4random()%100/100.f;

        POPSpringAnimation *strokeAnimationEnd = \
            [POPSpringAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
        strokeAnimationEnd.toValue = @(value1 > value2 ? value1 : value2);
        strokeAnimationEnd.springBounciness = 12.f;

        POPSpringAnimation *strokeAnimationStart = \
            [POPSpringAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeStart];
        strokeAnimationStart.toValue = @(value1 < value2 ? value1 : value2);
        strokeAnimationStart.springBounciness = 12.f;

        [layer pop_addAnimation:strokeAnimationEnd forKey:@"layerStrokeAnimation"];
        [layer pop_addAnimation:strokeAnimationStart forKey:@"layerStrokeAnimation1"];

    } timeInterval:1*NSEC_PER_SEC];
    [_timer start];
}

@end

6:减速动画

//
//  RootViewController.m
//  Animation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXEasing.h"
#import "POP.h"
#import "YXGCD.h"

@interface RootViewController ()<POPAnimationDelegate>

@property(nonatomic) UIControl *dragView;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *recognizer = \
        [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handlePan:)];

    self.dragView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.dragView.center = self.view.center;
    self.dragView.layer.cornerRadius = CGRectGetWidth(self.dragView.bounds)/2;
    self.dragView.backgroundColor = [UIColor cyanColor];
    [self.dragView addGestureRecognizer:recognizer];

    // 当触目的时候移除动画
    [self.dragView addTarget:self
                      action:@selector(touchDown:)
            forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:self.dragView];
}

- (void)touchDown:(UIControl *)sender
{
    [sender.layer pop_removeAllAnimations];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    // 拖拽
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    NSLog(@"center %@", NSStringFromCGPoint(recognizer.view.center));

    // 拖拽动作结束
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        // 计算出移动的速度
        CGPoint velocity = [recognizer velocityInView:self.view];
        NSLog(@"velocity %@", NSStringFromCGPoint(velocity));

        // 衰退减速动画
        POPDecayAnimation *positionAnimation = \
            [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPosition];

        // 设置代理
        positionAnimation.delegate = self;

        // 设置速度动画
        positionAnimation.velocity = [NSValue valueWithCGPoint:velocity];

        // 添加动画
        [recognizer.view.layer pop_addAnimation:positionAnimation
                                         forKey:@"layerPositionAnimation"];
    }
}

@end

计算出pan手势的在拖拽结束的时候的速度值.

 

 

时间: 2024-10-22 22:04:53

POP动画[1]的相关文章

ios-使用CAKeyframeAnimation导致导航栏的pop动画卡顿

问题描述 使用CAKeyframeAnimation导致导航栏的pop动画卡顿 我的一个提示框消失的时候,使用自定义的动画,用了CAKeyframeAnimation,此提示框消失的同时,导航栏执行pop viewcontroller操作,这时候,就出现了pop特效有卡顿的现象,如果我用UIView 的animateWithDuration 方法执行那个自定义的动画或者用uialertview代替我自己的提示框,pop特效就很流畅,这是什么原因呢? 代码如下: -(void)hideOnPopA

用POP动画编写带富文本的自定义动画效果

用POP动画编写带富文本的自定义动画效果 [源码] https://github.com/YouXianMing/UI-Component-Collection   [效果]   [特点] * 支持富文本 * 可定制型强(继承父类重写父类的startAnimation方法即可) * 支持动画的中断与持续 * 支持CAMediaTimingFunction * 数据与UI隔离,便于你封装属于你的类   [核心] // // POPNumberCount.h // POP // // Created

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 // // RootViewCont

POP动画[3]

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

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

用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 <

ios7 push/pop转场动画

前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见!!! 请注意:如果要求支持iOS 7以下版本,则不可使用此效果. 我们本篇文章目标效果: 视图切换种类 如下效果图,这是有两大类视图切换动画的,一种是交互式的,另一种就是自定义的. 本篇只讲其中的UIViewControllerAnimatedTransitioning协议,来实现push.pop

POP按钮动画

POP按钮动画   效果   源码 https://github.com/YouXianMing/Animations // // ButtonPressViewController.m // Facebook-POP-Animation // // Created by YouXianMing on 15/11/16. // Copyright 2015年 ZiPeiYi. All rights reserved. // #import "ButtonPressViewController.h

Swift:超炫的View Controller切换动画

匿名社交应用Secret的开发者开发了一款叫做Ping的应用,用户可以他们感兴趣的话题的推送. Ping有一个很炫的东西,就是主界面和之间切换的动画做的非常的好.每次看到一个非常炫的动画,都不由得会想:"这个东西我要不要自己实现以下".哈哈~~~ 这个教程里,你会学到如何用Swift实现这样的很酷的动画.你会学到如何使用shape layer,遮罩和使用UIViewControllerAnimnatedTransitioning协议和UIPercentDrivenInteractive

控制器转场动画详解

控制器转场动画详解   效果   说明 1. 控制器转场动画包括了普通控制器的present与dismiss转场动画,还有NavigationController的push与pop转场动画.其中,NavigationController的pop动画包含了回退百分比显示 2. 对转场动画对象进行行为抽象,让使用更加简单 3. 即使简化了使用,控制器转场动画也是属于比较难掌握的   源码 https://github.com/YouXianMing/ViewControllersTransition