Swift代码
override func viewDidLoad() {
super.viewDidLoad()
let showView = UIImageView(frame: CGRectMake(100,100,100))
showView.image = UIImage(named: "Snip20160422_3")
showView.layer.mask = test(showView)
self.view.addSubview(showView)
let blankView = UIView(frame: CGRectMake(250,100))
blankView.backgroundColor = UIColor.greenColor()
blankView.layer.mask = test(blankView)
self.view.addSubview(blankView)
}
func test(showView: UIView) -> CAShapeLayer {
let viewWidth = CGFloat(CGRectGetWidth(showView.frame))
let viewHeight = CGFloat(CGRectGetHeight(showView.frame))
let rightSpace: CGFloat = 10
let topSpace: CGFloat = 30
let point1 = CGPointMake(0,0)
let point2 = CGPointMake(viewWidth - rightSpace,0)
let point3 = CGPointMake(viewWidth - rightSpace,topSpace)
let point4 = CGPointMake(viewWidth,topSpace)
let point5 = CGPointMake(viewWidth - rightSpace,topSpace + 10)
let point6 = CGPointMake(viewWidth - rightSpace,viewHeight)
let point7 = CGPointMake(0,viewHeight)
let path = UIBezierPath()
path.moveToPoint(point1)
path.addLineToPoint(point2)
path.addLineToPoint(point3)
path.addLineToPoint(point4)
path.addLineToPoint(point5)
path.addLineToPoint(point6)
path.addLineToPoint(point7)
let layer = CAShapeLayer()
layer.path = path.CGPath
return layer
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(100,100)];
blankView.backgroundColor = [UIColor redColor];
blankView.layer.mask = [self testWithView:blankView];
[self.view addSubview:blankView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(250,100)];
imageView.image = [UIImage imageNamed:@"Snip20160422_3"];
imageView.layer.mask = [self testWithView:blankView];
[self.view addSubview:imageView];
}
- (CAShapeLayer *)testWithView:(UIView *)view
{
CGFloat viewWidth = CGRectGetWidth(view.frame);
CGFloat viewHeight = CGRectGetHeight(view.frame);
CGFloat rightSpace = 10;
CGFloat topSpace = 15;
CGPoint point1 = CGPointMake(0,0);
CGPoint point2 = CGPointMake(viewWidth-rightSpace,0);
CGPoint point3 = CGPointMake(viewWidth-rightSpace,topSpace);
CGPoint point4 = CGPointMake(viewWidth,topSpace);
CGPoint point5 = CGPointMake(viewWidth-rightSpace,topSpace+10.);
CGPoint point6 = CGPointMake(viewWidth-rightSpace,viewHeight);
CGPoint point7 = CGPointMake(0,viewHeight);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:point1];
[path addLineToPoint:point2];
[path addLineToPoint:point3];
[path addLineToPoint:point4];
[path addLineToPoint:point5];
[path addLineToPoint:point6];
[path addLineToPoint:point7];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
return layer;
}
原文链接:https://www.f2er.com/swift/323961.html