iOS渐变和自定义圆角的实现

来源:互联网 发布:刷新快捷键 mac 编辑:程序博客网 时间:2024/06/16 19:38
软件工程师在传统观念里可能就是写代码,完成项目需求。但对我而言,作为一名互联网从业者,不应该只是掌握一门技能,在平时的工作中也让自己充当产品经理,UI设计等角色,毕竟牛逼的用户体验是每个人喜欢的,不妨放开思路,尝试着做些不一样的东西。

在一些项目中,可能希望有这样的效果,某个view显示的是渐变风格,并且可能不是规则的矩形或者圆形,如何实现这种特效,下面小编给大家介绍一下这个方法,希望能帮到大家。

1.实现自定义圆角

-(void)setCustomCorners:(UIView *)view{<span style="white-space:pre"></span>UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];<span style="white-space:pre"></span>CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];<span style="white-space:pre"></span>maskLayer.frame = view.bounds;<span style="white-space:pre"></span>maskLayer.path = maskPath.CGPath;<span style="white-space:pre"></span>view.layer.mask = maskLayer;}

其中,UIRectCornerTopRight | UIRectCornerBottomRight表示右上右下为圆角,CGSizeMake(10,10)表示圆角度数为10,可以根据项目需求更改这两个值。

2.实现渐变


-(void)setCustomGradientLayer:(UIView *)view{<span style="white-space:pre"></span>CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];<span style="white-space:pre"></span>gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:73.0f/255.0f green:148.0f/255.0f blue:230.0f/255.0f alpha:0.5].CGColor,(__bridge id)[UIColor blueColor].CGColor];<span style="white-space:pre"></span>gradientLayer.startPoint = CGPointMake(0.5, 0);<span style="white-space:pre"></span>gradientLayer.endPoint = CGPointMake(0.5, 1);<span style="white-space:pre"></span>gradientLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.colorBackgroundView.frame), CGRectGetHeight(self.colorBackgroundView.frame));<span style="white-space:pre"></span>[view.layer addSublayer:gradientLayer];}

其中,gradientLayer.colors表示设定渐变的颜色数组,可以自定义;gradientLayer.startPoint ,gradientLayer.endPoint分别设定渐变的起始和结束位置,范围为0到1。

      如果您对我的文章感兴趣,请关注我的公众号,希望在互联网的道路上我们并肩前行。


1 0
原创粉丝点击