iOS動(dòng)畫之CAShapeLayer實(shí)戰(zhàn)-對鉤動(dòng)畫

1. 先讓view從橢圓變成圓形.修改view的layer的bounds和cornerRadius。

 //一邊切圓角一邊修改bouse
    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation1.toValue = @(btnWidth/2.0);
    
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation2.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, btnWidth, btnWidth)];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.delegate = self;
    group.duration = 2.0;
    group.animations = @[animation1,animation2];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self.layer addAnimation:group forKey:@"hehe"];

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    CAAnimation *ani = [self.layer animationForKey:@"hehe"];
    
    if ([ani isEqual:anim]) {
       
        self.layer.cornerRadius = btnWidth/2.0;
        self.bounds = CGRectMake(0, 0, btnWidth, btnWidth);
        
        [self checkAnimaltion];
    }
    
    
}

2. 制作對鉤動(dòng)畫.利用CAShapeLayer和貝塞爾曲線做。

- (void)checkAnimaltion
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth = 10;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    /**
     lineCap
     
     kCALineCapButt: 默認(rèn)格式,不附加任何形狀;
     kCALineCapRound: 在線段頭尾添加半徑為線段 lineWidth 一半的半圓;
     kCALineCapSquare: 在線段頭尾添加半徑為線段 lineWidth 一半的矩形”
     
     */
    shapeLayer.lineCap = kCALineCapRound;
    

    //kCALineJoinMiter,    // 尖角
    //kCALineJoinRound,    // 圓角
    //kCALineJoinBevel     // 缺角
    shapeLayer.lineJoin = kCALineJoinRound;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.frame.size.width/4, self.frame.size.height/2)];
    [path addLineToPoint:CGPointMake(self.frame.size.width/2, self.frame.size.height/4*3)];
    [path addLineToPoint:CGPointMake(self.frame.size.width/4*3, self.frame.size.height/3)];
    
    shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:shapeLayer];
    
    CABasicAnimation *checkani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkani.duration = 1.0;
    checkani.fromValue = @(0);
    checkani.toValue = @(1.0);
    [shapeLayer addAnimation:checkani forKey:@"checkani"];
    
    
}

3 因?yàn)間if在線制作只能加5張圖片,所以看起來不太好看,項(xiàng)目里看著順暢。

101554786.gif

4 完整代碼

#import <UIKit/UIKit.h>

@interface CheckBtn : UIView

@end

============================================


#import "CheckBtn.h"

#define kWidth self.bounds.size.width

#define kHeight self.bounds.size.height

static CGFloat btnWidth = 100;

@interface CheckBtn()<CAAnimationDelegate>

@end

@implementation CheckBtn

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    self.backgroundColor = [UIColor greenColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tap];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.layer.cornerRadius = kHeight/2.0;
    
}

- (void)tapAction
{
  

    
    //一邊切圓角一邊修改bouse
    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation1.toValue = @(btnWidth/2.0);
    
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation2.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, btnWidth, btnWidth)];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.delegate = self;
    group.duration = 10.0;
    group.animations = @[animation1,animation2];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self.layer addAnimation:group forKey:@"hehe"];
    

    
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    CAAnimation *ani = [self.layer animationForKey:@"hehe"];
    
    if ([ani isEqual:anim]) {
       
        self.layer.cornerRadius = btnWidth/2.0;
        self.bounds = CGRectMake(0, 0, btnWidth, btnWidth);
        
        [self checkAnimaltion];
    }
    
    
}

- (void)checkAnimaltion
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth = 10;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    /**
     lineCap
     
     kCALineCapButt: 默認(rèn)格式,不附加任何形狀;
     kCALineCapRound: 在線段頭尾添加半徑為線段 lineWidth 一半的半圓;
     kCALineCapSquare: 在線段頭尾添加半徑為線段 lineWidth 一半的矩形”
     
     */
    shapeLayer.lineCap = kCALineCapRound;
    

    //kCALineJoinMiter,    // 尖角
    //kCALineJoinRound,    // 圓角
    //kCALineJoinBevel     // 缺角
    shapeLayer.lineJoin = kCALineJoinRound;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.frame.size.width/4, self.frame.size.height/2)];
    [path addLineToPoint:CGPointMake(self.frame.size.width/2, self.frame.size.height/4*3)];
    [path addLineToPoint:CGPointMake(self.frame.size.width/4*3, self.frame.size.height/3)];
    
    shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:shapeLayer];
    
    CABasicAnimation *checkani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkani.duration = 10.0;
    checkani.fromValue = @(0);
    checkani.toValue = @(1.0);
    [shapeLayer addAnimation:checkani forKey:@"checkani"];
    
    
}

@end

=================================

#import "ViewController.h"
#import "CheckBtn.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CheckBtn *cheakbtn = [[CheckBtn alloc]init];
    cheakbtn.frame = CGRectMake(100, 100, 130, 50);
    cheakbtn.center = self.view.center;
    [self.view addSubview:cheakbtn];
    
}


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


@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,698評論 6 30
  • 轉(zhuǎn)載:http://m.itdecent.cn/p/32fcadd12108 每個(gè)UIView有一個(gè)伙伴稱為l...
    F麥子閱讀 6,595評論 0 13
  • 每個(gè)UIView有一個(gè)伙伴稱為layer,一個(gè)CALayer。UIView實(shí)際上并沒有把自己畫到屏幕上;它繪制本身...
    shenzhenboy閱讀 3,265評論 0 17
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,273評論 5 13
  • 花·色 深信造物安排自有神跡,世間萬物也各有其獨(dú)特的性格與氣質(zhì)。曾寫過一篇關(guān)于廚房的文字,那時(shí)候看到的是山藥雪樣肌...
    如風(fēng)格閱讀 590評論 0 2

友情鏈接更多精彩內(nèi)容