將其中的動畫拆開,由簡單到難展示,第二部分,先放大,光環後隱藏:
#import "WaveCircleButton.h"
@interface WaveCircleButton ()
//渲染層
@property (nonatomic,strong) CAShapeLayer *maskLayer;
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong) CAShapeLayer *loadingLayer;
@property (nonatomic,strong) CAShapeLayer *clickCicrleLayer;
@property (nonatomic,strong) UIButton *button;
@end
@implementation WaveCircleButton
- (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];
[self.layer addSublayer:self.maskLayer];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = self.bounds;
[_button setTitle:@"Wave Circle" 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;
}
- (CAShapeLayer *)maskLayer
{
if(!_maskLayer){
_maskLayer = [CAShapeLayer layer];
_maskLayer.opacity = 0;
_maskLayer.fillColor = [UIColor whiteColor].CGColor;
_maskLayer.path = [self drawBezierPath:self.frame.size.width/2].CGPath;
}
return _maskLayer;
}
- (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:(self.bounds.size.height - 10*2)/2].CGPath;
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;
//
// //執行下一個動畫
[self performSelector:@selector(clickNextAnimation) withObject:self afterDelay:basicAnimation.duration];
}
- (void)clickNextAnimation
{
//圓形變圓弧
_clickCicrleLayer.fillColor = [UIColor clearColor].CGColor;
_clickCicrleLayer.strokeColor = [UIColor whiteColor].CGColor;
_clickCicrleLayer.lineWidth = 10;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//圓弧變大
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
basicAnimation.duration = 0.5;
basicAnimation.toValue = (__bridge id _Nullable)([self drawclickCircleBezierPath:(self.bounds.size.height - 10*2)].CGPath);
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
//變透明
/* 如果一個animation是在一個animation group中,則beginTime就是其parent object——animation group 開始的一個偏移 */
/* 如果一個animation 的 beginTime爲5,則此動畫在group aniamtion開始之後的5s在開始動畫。 */
CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
basicAnimation1.beginTime = 0.10;
basicAnimation1.duration = 0.5;
basicAnimation1.toValue = @0;
basicAnimation1.removedOnCompletion = NO;
basicAnimation1.fillMode = kCAFillModeForwards;
animationGroup.duration = basicAnimation1.beginTime + basicAnimation1.duration;
animationGroup.removedOnCompletion = NO;
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.animations = @[basicAnimation,basicAnimation1];
[_clickCicrleLayer addAnimation:animationGroup forKey:@"clickCicrleAnimation1"];
}
- (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
第二部分是通過CAAnimationGroup來實現