/*
view的layer的实现
通过view的layer直接设置的方式
*/
UIImageView *redImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
redImgView.contentMode = UIViewContentModeScaleToFill;
redImgView.image = [UIImage imageNamed:@"11"];
redImgView.layer.cornerRadius = redImgView.frame.size.width / 2;
redImgView.layer.masksToBounds = YES;
[self.view addSubview:redImgView];
/*
使用贝塞尔曲线UIBezierPath和Core Graphics实现
BezierPath的实现方式继承UIView,自己实现一个customview
*/
UIImageView *blueImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
blueImgView.contentMode = UIViewContentModeScaleAspectFit;
blueImgView.image = [UIImage imageNamed:@"22"];
//开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(blueImgView.bounds.size, NO, 1.0);
//使用贝塞尔曲线画出一个圆形图
[[UIBezierPath bezierPathWithRoundedRect:blueImgView.bounds cornerRadius:blueImgView.frame.size.width] addClip];
[blueImgView drawRect:blueImgView.bounds];
blueImgView.image = UIGraphicsGetImageFromCurrentImageContext();
//结束画图
UIGraphicsEndImageContext();
[self.view addSubview:blueImgView];
/*
使用CAShapeLayer和UIBezierPath实现
通过bezizerpath设置一个路径,加到目标视图的layer上。
*/
UIImageView *grayImgView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 100, 100)];
grayImgView.contentMode = UIViewContentModeScaleAspectFill;
grayImgView.image = [UIImage imageNamed:@"33"];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:grayImgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:grayImgView.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//设置大小
maskLayer.frame = grayImgView.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
grayImgView.layer.mask = maskLayer;
[self.view addSubview:grayImgView];
//设置view左侧上UIRectCornerTopLeft 和 左侧下UIRectCornerBottomLeft为5的圆角
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer * layer = [[CAShapeLayer alloc]init];
layer.frame = view.bounds;
layer.path = path.CGPath;
view.layer.mask = layer;
//设置view 全部角为圆角
viewT.layer.cornerRadius = 10;//设置那个圆角大小
viewT.layer.masksToBounds = YES;//设置YES是保证添加的图片覆盖视图的效果
iOS11 layer新增了maskedCorners属性
typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
kCALayerMinXMinYCorner = 1U << 0,
kCALayerMaxXMinYCorner = 1U << 1,
kCALayerMinXMaxYCorner = 1U << 2,
kCALayerMaxXMaxYCorner = 1U << 3,
};
//对左下角和右下角进行圆角处理
if (@available(iOS 11.0, *)) {
view2.layer.maskedCorners = kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner;
} else {
// Fallback on earlier versions
}
UIViewContentMode
视图内容的填充方式
@property(nonatomic) UIViewContentMode contentMode;
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, //填充到整个视图区域,不等比例拉伸。
UIViewContentModeScaleAspectFit, //长宽等比填充视图区域,当某一个边到达视图边界的时候就不再拉伸,保证内容的长宽比是不变的同时尽可能的填充视图区域。
UIViewContentModeScaleAspectFill, //长宽等比填充视图区域,当某一个边到达视图边界的时候还继续拉伸,直到另一个方向达到视图边界。内容的长宽比不变的同时填满整个视图区域,不显示超过的部分。
UIViewContentModeRedraw, //重绘视图边界
UIViewContentModeCenter, //视图居中
UIViewContentModeTop, //视图顶部对齐
UIViewContentModeBottom, //视图底部对齐
UIViewContentModeLeft, //视图左侧对齐
UIViewContentModeRight, //视图右侧对齐
UIViewContentModeTopLeft, //视图左上角对齐
UIViewContentModeTopRight, //视图右上角对齐
UIViewContentModeBottomLeft, //视图左下角对齐
UIViewContentModeBottomRight, //视图右下角对齐
};