因为项目需要,就去查找画圆角的资料了,刚开始找到的比较麻烦,是要利用上下文的,后面找到了利用CAShapeLayer来做,比较简便。我也是直接从网络上扒下来的。但是后来想想,如果是圆角应该怎么做呢,有去查资料了,刚巧在简书上看见有人放了矩形虚线的代码,下面有人问圆角怎么弄,我竟然自己突然想到了:“改变贝塞尔曲线”。当时真的自己都震惊了=。=,本来想去查资料复制粘贴的,没想到灵机一动,自己找到了办法,有点兴奋。好,废话不多说,开始正题。
下面代码是对UIImageView做处理的,不过对于UIView应该都是一样的。
定义变量:
@property(nonatomic,strong) CAShapeLayer *shapeLayer;
@property(nonatomic,strong) UIImageView *imageV_IDCard;
写好懒加载:
-(UIImageView *)imageV_IDCard{
if (!_imageV_IDCard) {
_imageV_IDCard = [[UIImageView alloc] initWithFrame:CGRectMake(22, 66, SCREEN_WIDTH / 2.0 , SCREEN_WIDTH / 2.0)];
_imageV_IDCard.layer.cornerRadius = 25;
_imageV_IDCard.backgroundColor = [UIColor lightGrayColor];
}
return _imageV_IDCard;
}
-(CAShapeLayer *)shapeLayer{
if (!_shapeLayer) {
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
_shapeLayer.fillColor = [UIColor greenColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.imageV_IDCard.bounds cornerRadius:25];
_shapeLayer.path = path.CGPath;
_shapeLayer.frame = self.imageV_IDCard.bounds;
_shapeLayer.lineWidth = 2;
_shapeLayer.lineCap = @"square";
_shapeLayer.lineDashPattern = @[@5, @5];
}
return _shapeLayer;
}
注意的就是imageview和shapelayer要设置一样他圆角。
然后一添加,就成功了:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.imageV_IDCard];
[self.imageV_IDCard.layer addSublayer:self.shapeLayer];
}
不过,我对于_shapeLayer.lineDashPattern这个属性还是很有疑惑,我查到的资料是说,前一个值是实线长度,后一个值是空白长度,但是我运行出来的画面,和设置的长度不一样。有懂的朋友,在下面留言哦~,谢谢~
除非注明,文章均为seanward原创,转载请注明本文地址:https://www.jianshu.com/p/852581a165dd
网友评论