iosAnimationDemo - 1

https://github.com/a130785/iosAnimationDemo


將其中的動畫拆開,由簡單到難展示:

#import "ScaleButton.h"

@interface ScaleButton ()

//渲染層
@property (nonatomic,strong) CAShapeLayer *shapeLayer;

@property (nonatomic,strong) CAShapeLayer *clickCicrleLayer;

@property (nonatomic,strong) UIButton *button;

@end

@implementation ScaleButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        _shapeLayer = [self drawMask:frame.size.height/2];
        _shapeLayer.fillColor   = [UIColor clearColor].CGColor;
        _shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
        _shapeLayer.lineWidth   = 2;
        [self.layer addSublayer:_shapeLayer];
        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = self.bounds;
        [_button setTitle:@"Enlarge" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:13.f];
        [self addSubview:_button];
        [_button addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


- (void)clickBtn
{
    [self clickAnimation];
}


//點擊出現白色圓形
- (void)clickAnimation
{
    /*
     
     解釋:爲什麼動畫結束後返回原狀態?首先我們需要搞明白一點的是,layer動畫運行的過程是怎樣的?
     
     其實在我們給一個視圖添加layer動畫時,真正移動並不是我們的視圖本身,而是 presentation layer 的一個緩存。
     動畫開始時 presentation layer開始移動,原始layer隱藏,動畫結束時,presentation layer從屏幕上移除,原始layer顯示。
     這就解釋了爲什麼我們的視圖在動畫結束後又回到了原來的狀態,因爲它根本就沒動過。
     
     這個例子就是建立一個面積爲0的CAShapeLayer視圖
     利用CABasicAnimation來將其擴大,產生放大效果
     最後將其移除
     
     
     對於presentationLayer,這個屬性不一定總會返回一個實體對象,只有當進行動畫或者其他渲染的操作時,這個屬性會返回一個在當前屏幕上的layer
     不且每一次執行,這個對象都會不同,它是原layer的一個副本presentationLayer的modelLayer就是其實體layer層。
     
     */
    
    CAShapeLayer *clickCicrleLayer = [CAShapeLayer layer];
    clickCicrleLayer.frame = CGRectMake(self.bounds.size.width/2, self.bounds.size.height/2, 5, 5);
    
    clickCicrleLayer.fillColor = [UIColor whiteColor].CGColor;
    
    clickCicrleLayer.path = [self drawclickCircleBezierPath:0].CGPath;
    
    [self.layer addSublayer:clickCicrleLayer];
    
    //放大變色圓形
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.5;
    basicAnimation.toValue = (__bridge id _Nullable)([self drawclickCircleBezierPath:(self.bounds.size.height - 10*2)/2].CGPath);
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    
    [clickCicrleLayer addAnimation:basicAnimation forKey:@"clickCicrleAnimation"];
    
    _clickCicrleLayer = clickCicrleLayer;
}


- (CAShapeLayer *)drawMask:(CGFloat)x
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    
    // 所以說CAShapeLayer 經常和 UIBezierPath相使用
    shapeLayer.path = [self drawBezierPath:x].CGPath;
    return shapeLayer;
}


- (UIBezierPath *)drawBezierPath:(CGFloat)x
{
    CGFloat radius = self.bounds.size.height/2 - 3;
    CGFloat right = self.bounds.size.width-x;
    CGFloat left = x;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    bezierPath.lineJoinStyle = kCGLineJoinRound;
    bezierPath.lineCapStyle = kCGLineCapRound;
    
    //右邊圓弧
    [bezierPath addArcWithCenter:CGPointMake(right, self.bounds.size.height/2) radius:radius startAngle:-M_PI/2 endAngle:M_PI/2 clockwise:YES];
    //左邊圓弧
    [bezierPath addArcWithCenter:CGPointMake(left, self.bounds.size.height/2) radius:radius startAngle:M_PI/2 endAngle:-M_PI/2 clockwise:YES];
    //閉合弧線
    [bezierPath closePath];
    
    return bezierPath;
}



//畫圓
- (UIBezierPath *)drawclickCircleBezierPath:(CGFloat)radius
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    /**
     *  center: 弧線中心點的座標
     radius: 弧線所在圓的半徑
     startAngle: 弧線開始的角度值
     endAngle: 弧線結束的角度值
     clockwise: 是否順時針畫弧線
     *
     */
    [bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    return bezierPath;
}

@end



調用:

#import "ScaleButton.h"

@interface CAAnimationGroupViewController ()

@end

@implementation CAAnimationGroupViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view.layer addSublayer: [self backgroundLayer]];
    
    ScaleButton *scale = [[ScaleButton alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    scale.center = CGPointMake(self.view.center.x, 100);
    [self.view addSubview:scale];
    
}


- (CAGradientLayer *)backgroundLayer
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.colors = @[(__bridge id)[UIColor purpleColor].CGColor,(__bridge id)[UIColor redColor].CGColor];
    gradientLayer.startPoint = CGPointMake(0.5, 0);
    gradientLayer.endPoint = CGPointMake(0.5, 1);
    gradientLayer.locations = @[@0.65,@1];
    return gradientLayer;
}

@end


在這裏測試可知,如果不將背景設置一個CALayer值,會導致效果無法實現,這個問題還在研究


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章