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"
#import "POP.h"
#import "UIColor+CustomColors.h"
#import "StrokeCircleLayerConfigure.h"
#import "FillCircleLayerConfigure.h"
#import "GCD.h"

@interface ButtonPressViewController ()

@property (nonatomic, strong) UIButton          *button;
@property (nonatomic, strong) CAShapeLayer      *circleShape1;
@property (nonatomic, strong) CAShapeLayer      *circleShape2;

@property (nonatomic, strong) UILabel           *label;

@end

@implementation ButtonPressViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setup];
}

#pragma mark - setup
- (void)setup {

    self.view.backgroundColor = [UIColor whiteColor];

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

    self.label               = [[UILabel alloc] initWithFrame:_button.bounds];
    self.label.font          = Font_HYQiHei(30);
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.text          = @"0%";
    [self.button addSubview:self.label];

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

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

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

    // 圆环1
    {
        self.circleShape1 = [CAShapeLayer layer];
        self.circleShape1.strokeEnd = 0.f;
        StrokeCircleLayerConfigure *config = [StrokeCircleLayerConfigure new];
        config.lineWith     = 0.5f;
        config.startAngle   = 0;
        config.endAngle     = M_PI * 2;
        config.radius       = 55.f;
        config.circleCenter = self.view.center;
        [config configCAShapeLayer:self.circleShape1];
        [self.view.layer addSublayer:self.circleShape1];
    }

    // 圆环2
    {
        self.circleShape2 = [CAShapeLayer layer];
        self.circleShape2.strokeEnd = 0.f;
        StrokeCircleLayerConfigure *config = [StrokeCircleLayerConfigure new];
        config.lineWith     = 0.5f;
        config.startAngle   = 0;
        config.endAngle     = M_PI * 2;
        config.radius       = 60.f;
        config.clockWise    = YES;
        config.circleCenter = self.view.center;
        [config configCAShapeLayer:self.circleShape2];
        [self.view.layer addSublayer:self.circleShape2];
    }
}

#pragma mark - Button events
- (void)scaleToSmall {

    [_button.layer pop_removeAllAnimations];

    // 变小尺寸
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue            = [NSValue valueWithCGSize:CGSizeMake(0.7f, 0.7f)];
    scaleAnimation.delegate           = self;
    [_button.layer pop_addAnimation:scaleAnimation forKey:nil];

    // 颜色
    POPSpringAnimation *backgroundColor = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
    backgroundColor.toValue             = (id)[UIColor magentaColor].CGColor;
    [_button.layer pop_addAnimation:backgroundColor forKey:@"magentaColor"];
}

- (void)scaleAnimations {

    [_button.layer pop_removeAllAnimations];

    // 恢复尺寸
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue            = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.delegate           = self;
    [_button.layer pop_addAnimation:scaleAnimation forKey:nil];

    // 颜色
    POPSpringAnimation *backgroundColor = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
    backgroundColor.toValue             = (id)[UIColor cyanColor].CGColor;
    [_button.layer pop_addAnimation:backgroundColor forKey:nil];
}

- (void)scaleToDefault{

    [_button.layer pop_removeAllAnimations];

    // 恢复尺寸
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue            = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.delegate           = self;
    [_button.layer pop_addAnimation:scaleAnimation forKey:nil];

    // 颜色
    POPSpringAnimation *backgroundColor = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];
    backgroundColor.toValue             = (id)[UIColor cyanColor].CGColor;
    [_button.layer pop_addAnimation:backgroundColor forKey:nil];
}

#pragma mark - POP delegate
- (void)pop_animationDidStart:(POPAnimation *)anim {

    NSLog(@"pop_animationDidStart %@", anim);
}

- (void)pop_animationDidReachToValue:(POPAnimation *)anim {

    NSLog(@"pop_animationDidReachToValue %@", anim);
}

- (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished {

    NSLog(@"pop_animationDidStop %@", anim);

}

- (void)pop_animationDidApply:(POPAnimation *)anim {

    NSLog(@"pop_animationDidApply %@", anim);

    NSValue *toValue = (NSValue *)[anim valueForKeyPath:@"currentValue"];
    CGSize size      = [toValue CGSizeValue];

    CGFloat percent         = (size.height - calculateConstant(0, 1, 1, 0.7))/calculateSlope(0, 1, 1, 0.7);
    _circleShape1.strokeEnd = percent;
    _circleShape2.strokeEnd = percent;

    double showValue = fabs(percent * 100);
    self.label.text  = [NSString stringWithFormat:@"%.f%%", showValue];
}

#pragma mark - Y = kX + b
CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) {

    return (y2 - y1) / (x2 - x1);
}

CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) {

    return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
}

@end

细节

处理好POP动画的代理即可.

时间: 2024-12-22 03:01:54

POP按钮动画的相关文章

Flash制作插按钮动画教程

   Flash是制作二维动画的一个常用软件,小北的这个教程详细的讲述了非常实用的插按钮方法~很实用的哦,希望能对大家有所帮助! 工具/原料 Flash CS3或以上 方法/步骤 1.打开Flash,新建一个文档,或者打开自己做的动画文件.(小北是打开自己做好的FL哦~)如图所示: 2.在场景中绘制按钮,并且储存为按钮元件,并双击进入按钮设置界面,设置好弹起,指针经过,按下,点击,如图所示: 3.返回场景,新建按钮图层,在需要插入按钮的那一帧插入停止动作,具体做法如下:(1) 将鼠标指针放在需要

POP数值动画

POP数值动画   效果     源码 https://github.com/YouXianMing/Animations // // PopNumberController.m // Animations // // Created by YouXianMing on 15/11/18. // Copyright 2015年 YouXianMing. All rights reserved. // #import "PopNumberController.h" #import &qu

css3 动画:CSS3实现漂亮的按钮动画

Chrome下效果理想,Firefox,IE9下没有动画效果.CSS代码片段.button, .button:visited{background:#222 url(overlay.png) repeat-x;display:inline-block;padding:5px 10px 6px;color:#fff;text-decoration:none;/*border-radius*/-webkit-border-radius:6px;-moz-border-radius:6px;borde

CSS3实现漂亮的按钮动画

 Chrome下效果理想,Firefox,IE9下没有动画效果. CSS代码片段.button, .button:visited{background:#222 url(overlay.png) repeat-x;display:inline-block;padding:5px 10px 6px;color:#fff;text-decoration:none;/*border-radius*/-webkit-border-radius:6px;-moz-border-radius:6px;bor

excel利用“宏”建立窗体命令按钮动画教程

<Excel2003入门动画教程57.利用"宏"建立窗体命令按钮>. 演示动画 操作步骤 通过运行"宏"对话框调用宏不太方便,通过快捷键调用宏,如果宏多了又记不住,那我们不如在Excel表格中添加一个按钮吧. 如果宏仅对当前工作簿文档有效,我建议大家在工作簿文档中添加一个"窗体"命令按钮,用来调用宏: 执行"视图工具栏窗体"命令,展开"窗体"工具栏. 点击工具栏上的"命令按钮"

Excel中制作“宏”按钮动画教程

<Excel2003入门动画教程56.Excel中制作"宏"按钮>. 演示动画 操作步骤 如果宏对所有工作簿文档有效,建议大家在Excel工具栏上添加一个命令按钮,用来调用宏. 执行"工具→自定义"命令,打开"自定义"对话框. 切换到"命令"标签下,在"类别"下面选中"宏",然后将右边"命令"区域中的"自定义按钮"选项拖到工具栏合适位置

Android实现定制返回按钮动画效果的方法_Android

今天我们来讲一讲Andorid中如何定制返回按钮的动画效果.我将结合实际应用来阐述如何使用. 首先来看一个效果截图,有一个搜索按钮在一个页面的顶部: 我之前实现的方式是和百度/Google首页搜索的效果一样的,类似web开发中的ajax请求,结果直接在当前页面显示出来(下拉效果).后来参考了众多APP之后发现都是进入到一个新的页面,所以我也就改过来试试..废话说多了.. 那我们点击了搜索框之后呢,会进入到一个新的activity,这里的动画效果很简单,直接使用overridePendingTra

iOS 按照UIDemo写的点击录音按钮动画,[[EMCDDeviceManager sharedInstance] emPeekRecorderVoiceMeter]返回值总是0

问题描述 SDK是用CocoaPods导入的,在写点击录音按钮播放动画时,需要根据音量大小改变动画图片,但是[[EMCDDeviceManager sharedInstance] emPeekRecorderVoiceMeter]方法返回值总是0,请大婶们帮忙看下 解决方案 已解决,加上下面代码就OK了

Android实现定制返回按钮动画效果的方法

今天我们来讲一讲Andorid中如何定制返回按钮的动画效果.我将结合实际应用来阐述如何使用. 首先来看一个效果截图,有一个搜索按钮在一个页面的顶部: 我之前实现的方式是和百度/Google首页搜索的效果一样的,类似web开发中的ajax请求,结果直接在当前页面显示出来(下拉效果).后来参考了众多APP之后发现都是进入到一个新的页面,所以我也就改过来试试..废话说多了.. 那我们点击了搜索框之后呢,会进入到一个新的activity,这里的动画效果很简单,直接使用overridePendingTra