美文网首页Swift编程
任意形状的ImageView

任意形状的ImageView

作者: 我是一个TableView | 来源:发表于2016-08-19 15:22 被阅读0次

    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类型,可以传入任意想要的形状。

    相关文章

      网友评论

        本文标题:任意形状的ImageView

        本文链接:https://www.haomeiwen.com/subject/eaybsttx.html