任意形状的ImageView

demo地址
运行效果:

各个形状的imageview

ZWImageView.h中代码:

#import <UIKit/UIKit.h>

@interface ZWImageView : UIView
/**
 *  path:形状
 */
@property (nonatomic, assign) CGPathRef path;
/**
 *  image:图片
 */
@property (nonatomic, strong) UIImage *image;

- (instancetype)init;
- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame path:(CGPathRef)path image:(UIImage *)image;
@end

ZWImageView.m中代码:

#import "ZWImageView.h"

@implementation ZWImageView
{
    CAShapeLayer *maskLayer;
    CALayer *contentLayer;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpLayers];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpLayers];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame path:(CGPathRef)path image:(UIImage *)image{
    self = [super initWithFrame:frame];
    if (self) {
        _path = path;
        _image = image;
        [self setUpLayers];
    }
    return self;
}

- (void)setUpLayers{
    maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.fillColor = [UIColor blackColor].CGColor;
    maskLayer.strokeColor = [UIColor redColor].CGColor;
    //拉伸范围
//    maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    maskLayer.contentsScale = [UIScreen mainScreen].scale;
    maskLayer.path = _path;
    
    contentLayer = [CALayer layer];
    contentLayer.frame = self.bounds;
    //居中裁剪
    contentLayer.contentsGravity = kCAGravityResizeAspectFill;
    contentLayer.mask = maskLayer;
    contentLayer.contents = (__bridge id _Nullable)(_image.CGImage);
    [self.layer addSublayer:contentLayer];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    maskLayer.frame = self.bounds;
    contentLayer.frame = self.bounds;
}

- (void)setPath:(CGPathRef)path{
    _path = path;
    maskLayer.path = path;
}

- (void)setImage:(UIImage *)image{
    _image = image;
    contentLayer.contents = (__bridge id _Nullable)(image.CGImage);
}

@end

使用方法:

//三角形
    UIBezierPath *path1 = [[UIBezierPath alloc] init];
    path1.lineCapStyle = kCGLineCapRound;
    path1.lineJoinStyle = kCGLineJoinRound;
    [path1 moveToPoint:CGPointMake(25, 0)];
    [path1 addLineToPoint:CGPointMake(0, 50)];
    [path1 addLineToPoint:CGPointMake(50, 50)];
    [path1 fill];
    
    ZWImageView *imageView1 = [[ZWImageView alloc] initWithFrame:CGRectMake(20, 100, 50, 50) path:path1.CGPath image:[UIImage imageNamed:@"示例图"]];
    [self.view addSubview:imageView1];
    
    //波浪
    UIBezierPath *path2 = [[UIBezierPath alloc] init];
    path2.lineCapStyle = kCGLineCapRound;
    path2.lineJoinStyle = kCGLineJoinRound;
    [path2 moveToPoint:CGPointMake(0, 50)];
    [path2 addLineToPoint:CGPointMake(0, 25)];
    //    [path addArcWithCenter:CGPointMake(50, 50) radius:50 startAngle:M_PI endAngle:M_PI_2 clockwise:YES];
    [path2 addCurveToPoint:CGPointMake(50, 25) controlPoint1:CGPointMake(15, 15) controlPoint2:CGPointMake(35, 35)];
    [path2 addLineToPoint:CGPointMake(50, 50)];
    [path2 fill];
    
    ZWImageView *imageView2 = [[ZWImageView alloc] initWithFrame:CGRectMake(120, 100, 50, 50) path:path2.CGPath image:[UIImage imageNamed:@"示例图"]];
    [self.view addSubview:imageView2];
    
    //椭圆形
    UIBezierPath *path3 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 5, 50, 100)];
    [path3 fill];
    
    ZWImageView *imageView3 = [[ZWImageView alloc] initWithFrame:CGRectMake(220, 100, 50, 120) path:path3.CGPath image:[UIImage imageNamed:@"示例图"]];
    [self.view addSubview:imageView3];
    
    
    //扇形
    UIBezierPath *path4 = [[UIBezierPath alloc] init];
    [path4 moveToPoint:CGPointMake(50, 100)];
    [path4 addLineToPoint:CGPointMake(0, 50)];
    [path4 addArcWithCenter:CGPointMake(50, 100) radius:70.7 startAngle:M_PI_4 * 5 endAngle:M_PI_4 * 7 clockwise:YES];
    [path4 fill];
    
    ZWImageView *imageView4 = [[ZWImageView alloc] initWithFrame:CGRectMake(20, 180, 100, 100) path:path4.CGPath image:[UIImage imageNamed:@"示例图"]];
    [self.view addSubview:imageView4];
    
    //四分之三圆
    UIBezierPath *path5 = [[UIBezierPath alloc] init];
    [path5 moveToPoint:CGPointMake(0, 100)];
    [path5 addLineToPoint:CGPointMake(0, 50)];
    [path5 addArcWithCenter:CGPointMake(50, 50) radius:50 startAngle:M_PI endAngle:M_PI_2 clockwise:YES];
    [path5 fill];
    
    ZWImageView *imageView5 = [[ZWImageView alloc] initWithFrame:CGRectMake(20, 300, 100, 100) path:path5.CGPath image:[UIImage imageNamed:@"示例图"]];
    [self.view addSubview:imageView5];

ZWImageviewpath属性为CGPathRef类型,可以传入任意想要的形状。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,795评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,749评论 19 139
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 9,004评论 7 11
  • 在很长一段时间里,我对冬天的印象都是祖奶奶的小暖炉。那个暖炉是竹编的,形状像极了古时候的食盒,不同之处在于暖炉顶部...
    阿青mmmmmay阅读 2,325评论 0 0