如圖:
這是一個(gè)xib搭建的界面,里面的button 是通過(guò)純代碼自定義創(chuàng)建的,包含動(dòng)畫,button的代碼如下:
#import <UIKit/UIKit.h>
typedef void(^finishBlock)();
@interface LYButton : UIView
@property (nonatomic,copy) finishBlock translateBlock;
@end
#import "LYButton.h"
@interface LYButton()
//渲染層
@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 LYButton
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
// NSLog(@"LYButton----initWithCoder");
// NSLog(@"%@",NSStringFromCGRect(self.frame));
[self starInitWithFrame:self.frame];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
NSLog(@"-----");
if(self = [super initWithFrame:frame]){
[self starInitWithFrame: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:@"SIGN IN" 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)starInitWithFrame:(CGRect)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:@"SIGN IN" 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];
}
-(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];
}
//點(diǎn)擊出現(xiàn)白色圓形
-(void)clickAnimation{
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;
//執(zhí)行下一個(gè)動(dòng)畫
[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;
//變透明
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"];
[self performSelector:@selector(startMaskAnimation) withObject:self afterDelay:animationGroup.duration];
}
//半透明的登錄按鈕的背景
-(void)startMaskAnimation{
_maskLayer.opacity = 0.5;
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
basicAnimation.duration = 0.25;
basicAnimation.toValue = (__bridge id _Nullable)([self drawBezierPath:self.frame.size.height/2].CGPath);
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[_maskLayer addAnimation:basicAnimation forKey:@"maskAnimation"];
[self performSelector:@selector(dismissAnimation) withObject:self afterDelay:basicAnimation.duration+0.2];
}
//登錄按鈕合攏并消失(透明)
-(void)dismissAnimation{
[self removeSubViews];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
basicAnimation.duration = 0.2;
basicAnimation.toValue = (__bridge id _Nullable)([self drawBezierPath:self.frame.size.width/2].CGPath);
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
basicAnimation1.beginTime = 0.10;
basicAnimation1.duration = 0.2;
basicAnimation1.toValue = @0;
basicAnimation1.removedOnCompletion = NO;
basicAnimation1.fillMode = kCAFillModeForwards;
animationGroup.animations = @[basicAnimation,basicAnimation1];
animationGroup.duration = basicAnimation1.beginTime+basicAnimation1.duration;
animationGroup.removedOnCompletion = NO;
animationGroup.fillMode = kCAFillModeForwards;
[_shapeLayer addAnimation:animationGroup forKey:@"dismissAnimation"];
[self performSelector:@selector(loadingAnimation) withObject:self afterDelay:animationGroup.duration];
}
//菊花
-(void)loadingAnimation{
_loadingLayer = [CAShapeLayer layer];
_loadingLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
_loadingLayer.fillColor = [UIColor clearColor].CGColor;
_loadingLayer.strokeColor = [UIColor whiteColor].CGColor;
_loadingLayer.lineWidth = 2;
_loadingLayer.path = [self drawLoadingBezierPath].CGPath;
[self.layer addSublayer:_loadingLayer];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
basicAnimation.fromValue = @(0);
basicAnimation.toValue = @(M_PI*2);
basicAnimation.duration = 0.5;
basicAnimation.repeatCount = LONG_MAX;
[_loadingLayer addAnimation:basicAnimation forKey:@"loadingAnimation"];
[self performSelector:@selector(removeAllAnimation) withObject:self afterDelay:2];
}
-(void)removeSubViews{
[_button removeFromSuperview];
[_maskLayer removeFromSuperlayer];
[_loadingLayer removeFromSuperlayer];
[_clickCicrleLayer removeFromSuperlayer];
}
-(void)removeAllAnimation{
[self removeSubViews];
if(self.translateBlock){
self.translateBlock();
}
}
-(CAShapeLayer *)drawMask:(CGFloat)x{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.bounds;
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 *)drawLoadingBezierPath{
CGFloat radius = self.bounds.size.height/2 - 3;
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:M_PI/2 endAngle:M_PI/2+M_PI/2 clockwise:YES];
return bezierPath;
}
//畫圓
-(UIBezierPath *)drawclickCircleBezierPath:(CGFloat)radius{
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
/**
* center: 弧線中心點(diǎn)的坐標(biāo)
radius: 弧線所在圓的半徑
startAngle: 弧線開始的角度值
endAngle: 弧線結(jié)束的角度值
clockwise: 是否順時(shí)針畫弧線
*
*/
[bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
return bezierPath;
}
@end
這個(gè)自定義的button的代碼,大家可以直接復(fù)制過(guò)去使用;
xib中的設(shè)置如下:
其實(shí)操作就是拖一個(gè)View(什么自定義的button,我是view創(chuàng)建的),然后設(shè)置它的class為 LYButton
;
這個(gè)時(shí)候運(yùn)行代碼,發(fā)現(xiàn)一個(gè)問(wèn)題,我們并沒有把這個(gè)自定義的控件加載出來(lái);
打斷點(diǎn)發(fā)現(xiàn),-(instancetype)initWithFrame:(CGRect)frame
,這個(gè)自定義控件的初始化方法并不會(huì)走;
原因:這個(gè)方法是只有純代碼創(chuàng)建,我們調(diào)用了
initWithFrame
,才會(huì)走,通過(guò)xib是不走這個(gè)方法的,我們需要添加一個(gè)- (instancetype)initWithCoder:(NSCoder *)coder
方法,這里面其實(shí)創(chuàng)建了一個(gè)LYButton
的對(duì)象了,我們需要通過(guò)initWithCoder
去對(duì)這個(gè)對(duì)象進(jìn)行一些其他 的初始化操作;
上面的代碼,我已經(jīng)添加了initWithCoder,并重新調(diào)用了初始化的操作,問(wèn)題解決.