总结iOS实现渐变颜色的三种方法

2020-01-18 16:31:19刘景俊

     3、将CAShapeLayer对象赋值给imageView的mask属性


- (void)viewDidLoad
{
 [super viewDidLoad];

 [self.view addSubview:self.firstCircle];
 _firstCircle.frame = CGRectMake(0, 0, 200, 200);
 _firstCircle.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2.0, CGRectGetHeight(self.view.bounds) / 2.0);
 CGFloat firsCircleWidth = 5;
 self.firstCircleShapeLayer = [self generateShapeLayerWithLineWidth:firsCircleWidth];
 _firstCircleShapeLayer.path = [self generateBezierPathWithCenter:CGPointMake(100, 100) radius:100].CGPath;
 _firstCircle.layer.mask = _firstCircleShapeLayer;
} 

- (CAShapeLayer *)generateShapeLayerWithLineWidth:(CGFloat)lineWidth
{
 CAShapeLayer *waveline = [CAShapeLayer layer];
 waveline.lineCap = kCALineCapButt;
 waveline.lineJoin = kCALineJoinRound;
 waveline.strokeColor = [UIColor redColor].CGColor;
 waveline.fillColor = [[UIColor clearColor] CGColor];
 waveline.lineWidth = lineWidth;
 waveline.backgroundColor = [UIColor clearColor].CGColor;

 return waveline;
}

- (UIBezierPath *)generateBezierPathWithCenter:(CGPoint)center radius:(CGFloat)radius
{
 UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];

 return circlePath;
}

- (UIImageView *)firstCircle
{
 if (!_firstCircle) {
 self.firstCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circleBackground"]];
 _firstCircle.layer.masksToBounds = YES;
 _firstCircle.alpha = 1.0;
 }

 return _firstCircle;
}

ios,开发实现颜色渐变,ios渐变色代码实现,颜色渐变

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能有所帮助,如果有疑问大家可以留言交流。


注:相关教程知识阅读请移步到IOS开发频道。