@property (nonatomic, strong) UIView *dowView;
@property (nonatomic, strong) UIButton *dowLoadButton;
/// 创建控件
self.dowView = [[UIView alloc] initWithFrame:CGRectMake(200, 100, 100, 100)];
[self.view addSubview:self.dowView];
// self.dowView.backgroundColor = [UIColor lightGrayColor];
///
self.dowLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
[self.dowView addSubview:self.dowLoadButton];
self.dowLoadButton.layer.cornerRadius = 25;
self.dowLoadButton.layer.masksToBounds = YES;
self.dowLoadButton.backgroundColor = [UIColor orangeColor];
[self.dowLoadButton addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
//// 执行方法 添加防止变态点击的方法
- (void)downloadAction{
//先将未到时间执行前的任务取消
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(allTaskBeginDownload)object:nil];
[self performSelector:@selector(allTaskBeginDownload)withObject:nil afterDelay:0.2f];
}
-(void) allTaskBeginDownload{
// button的点击事件写在这里
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
anim.fromValue = @(0.6);/// 调整光线的起始位置 当是0时 光线从中心开始
anim.toValue = @(1);
anim.beginTime = CACurrentMediaTime();
anim.repeatCount = 1;/// 重复次数
anim.duration = 0.5; /// 动画时间
anim.fillMode = kCAFillModeForwards;
[anim isRemovedOnCompletion];
/// 循环添加12条光线
for (NSInteger i = 0; i<12; i++) {
CGMutablePathRef path=CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.dowView.frame.size.width/2, self.dowView.frame.size.height/2);
CGPathAddLineToPoint(path, NULL, self.dowView.frame.size.width/2+40(调整光线起始位置和长度)*cos(2*(M_PI)*i/12), self.dowView.frame.size.width/2+40*sin(2*M_PI*i/12));
CAShapeLayer *trackLayer = [CAShapeLayer layer];
//// 设置随机颜色
trackLayer.strokeColor = [UIColor colorWithRed:(arc4random()%226)/225.0 green:(arc4random()%226)/225.0 blue:(arc4random()%226)/225.0 alpha:1].CGColor;
/// 创建光线宽
trackLayer.lineWidth = 1;
trackLayer.path = path;
trackLayer.fillColor = [UIColor clearColor].CGColor;
trackLayer.strokeStart = 1;
[self.dowView.layer addSublayer:trackLayer];
[trackLayer addAnimation:anim forKey:@"strokeStart"];
}
}