用UIBezierPath數組對UIView進行鏤空處理,uibezierpathuiview

來源:互聯網
上載者:User

用UIBezierPath數組對UIView進行鏤空處理,uibezierpathuiview

用UIBezierPath數組對UIView進行鏤空處理

 

效果

 

源碼

////  CutOutClearView.h//  CutOutMaskView////  Created by YouXianMing on 16/7/8.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import <UIKit/UIKit.h>@interface CutOutClearView : UIView@property (nonatomic, strong) UIColor  *fillColor;@property (nonatomic, strong) NSArray  <UIBezierPath *>  *paths;@end
////  CutOutClearView.m//  CutOutMaskView////  Created by YouXianMing on 16/7/8.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "CutOutClearView.h"@implementation CutOutClearView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {            self.fillColor       = [UIColor whiteColor];        self.backgroundColor = [UIColor clearColor];        self.opaque          = NO;    }        return self;}- (void)drawRect:(CGRect)rect {    [super drawRect:rect];        [self.fillColor setFill];    UIRectFill(rect);        CGContextRef context = UIGraphicsGetCurrentContext();        for (UIBezierPath *path in self.paths) {                CGContextAddPath(context, path.CGPath);        CGContextSetBlendMode(context, kCGBlendModeClear);        CGContextFillPath(context);    }}@end
////  ViewController.m//  CutOutClearView////  Created by YouXianMing on 16/7/8.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "CutOutClearView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];    imageView.image        = [UIImage imageNamed:@"bg.png"];    imageView.contentMode  = UIViewContentModeScaleAspectFill;    [self.view addSubview:imageView];        NSMutableArray *paths = [NSMutableArray array];        {        UIBezierPath* bezierPath = [UIBezierPath bezierPath];        [bezierPath moveToPoint: CGPointMake(57.04, 31.19)];        [bezierPath addLineToPoint: CGPointMake(125.55, 12.5)];        [bezierPath addLineToPoint: CGPointMake(185.5, 91)];        [bezierPath addLineToPoint: CGPointMake(57.04, 169.5)];        [bezierPath addLineToPoint: CGPointMake(18.5, 91)];        [bezierPath addLineToPoint: CGPointMake(57.04, 31.19)];        [bezierPath closePath];        [paths addObject:bezierPath];    }        {        UIBezierPath* bezierPath = [UIBezierPath bezierPath];        [bezierPath moveToPoint: CGPointMake(46.5, 245.5)];        [bezierPath addLineToPoint: CGPointMake(137.5, 272.5)];        [bezierPath addLineToPoint: CGPointMake(137.5, 211.5)];        [bezierPath addLineToPoint: CGPointMake(90.5, 196.5)];        [bezierPath addLineToPoint: CGPointMake(46.5, 211.5)];        [bezierPath addLineToPoint: CGPointMake(46.5, 245.5)];        [bezierPath closePath];        [paths addObject:bezierPath];    }        CutOutClearView *cutOutView = [[CutOutClearView alloc] initWithFrame:self.view.bounds];    cutOutView.fillColor        = [UIColor redColor];    cutOutView.paths            = paths;//    [self.view addSubview:cutOutView];    imageView.maskView = cutOutView;}@end

 

細節

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.