CGFloat squareSize = MIN(imageSize.width, imageSize.height); UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (squareSize / 2), (squareSize / 4))]; [path addLineToPoint:CGPointMake((squareSize / 2), 0)]; [path addLineToPoint:CGPointMake(squareSize - ((sin(M_1_PI / 180 * 60)) * (squareSize / 2)), (squareSize / 4))]; [path addLineToPoint:CGPointMake(squareSize - ((sin(M_1_PI / 180 * 60)) * (squareSize / 2)), (squareSize / 2) + (squareSize / 4))]; [path addLineToPoint:CGPointMake((squareSize / 2), squareSize)]; [path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (squareSize / 2), (squareSize / 2) + (squareSize / 4))]; [path closePath];
CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame = CGRectMake(0, 5, self.bounds.size.width, self.bounds.size.height); pathLayer.path = path.CGPath; pathLayer.strokeColor = [UIColor blackColor].CGColor; pathLayer.fillColor = [UIColor clearColor].CGColor; pathLayer.lineWidth = 5.0; pathLayer.lineJoin = kCALineJoinRound;
// 创建渐变图层 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = self.bounds; gradientLayer.type = kCAGradientLayerConic; // 定义颜色数组,中间白色,两边透明 NSArray *colors = @[(id)[UIColor colorWithWhite:1 alpha:1].CGColor, (id)[UIColor colorWithWhite:0.8 alpha:0.8].CGColor, (id)[UIColor colorWithWhite:0.6 alpha:0.6].CGColor, (id)[UIColor colorWithWhite:0.5 alpha:0].CGColor, (id)[UIColor colorWithWhite:0 alpha:0].CGColor, (id)[UIColor colorWithWhite:0.5 alpha:0].CGColor, (id)[UIColor colorWithWhite:0.6 alpha:0.6].CGColor, (id)[UIColor colorWithWhite:0.8 alpha:0.8].CGColor, (id)[UIColor colorWithWhite:1 alpha:1].CGColor]; gradientLayer.colors = colors; gradientLayer.startPoint = CGPointMake(0.5, 0.5); gradientLayer.endPoint = CGPointMake(0, 0.5); // 设置颜色位置 gradientLayer.locations = @[@0.0, @0.05, @0.1, @0.15, @0.5, @0.85, @0.9, @0.95, @1.0];
[self.layer addSublayer:self.gradientLayer]; self.gradientLayer.mask = self.pathLayer;
- (void)createTimer { gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0)); dispatch_source_set_timer(gcdTimer, DISPATCH_TIME_NOW, 0.02 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC); dispatch_source_set_event_handler(gcdTimer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ [self handleTimerEvent]; }); }); dispatch_resume(gcdTimer); } - (void)handleTimerEvent { CGFloat speed = 0.04; CGPoint endPoint = self.gradientLayer.endPoint; if (endPoint.x <= 0 && endPoint.y > 0 && !self.leftMove) { self.bottomMove = YES; self.topMove = NO; self.gradientLayer.endPoint = CGPointMake(endPoint.x, endPoint.y-speed); } else if (endPoint.x < 1 && endPoint.y <= 0 && !self.topMove) { self.leftMove = YES; self.rightMove = NO; self.gradientLayer.endPoint = CGPointMake(endPoint.x+speed, endPoint.y); } else if (endPoint.x >= 1 && endPoint.y < 1 && !self.rightMove) { self.topMove = YES; self.bottomMove = NO; self.gradientLayer.endPoint = CGPointMake(endPoint.x, endPoint.y+speed); } else if (endPoint.x >= 0 && endPoint.y >= 1 && !self.bottomMove) { self.rightMove = YES; self.leftMove = NO; self.gradientLayer.endPoint = CGPointMake(endPoint.x-speed, endPoint.y); } }