我想用掩码创建UIView
的子类。我尝试使用drawRect
和layoutSubviews
,但没有成功,在这些尝试视图没有出现。
- (void)layoutSubviews {
[super layoutSubviews];
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){self.frame.origin.x + 10, self.frame.origin.y}];
[path addLineToPoint:(CGPoint){self.frame.origin.x + self.frame.size.width, self.frame.origin.y}];
[path addLineToPoint:(CGPoint){self.frame.origin.x + self.frame.size.width, self.frame.origin.y + self.frame.size.height}];
[path addLineToPoint:(CGPoint){self.frame.origin.x +10, self.frame.origin.y + self.frame.size.height}];
[path addLineToPoint:(CGPoint){self.frame.origin.x +10, self.frame.origin.y + (self.frame.size.height/2) + 5}];
[path addLineToPoint:(CGPoint){self.frame.origin.x , self.frame.origin.y + (self.frame.size.height/2)}];
[path addLineToPoint:(CGPoint){self.frame.origin.x +10, self.frame.origin.y + (self.frame.size.height/2) - 5}];
[path closePath];
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.bounds;
mask.path = path.CGPath;
self.layer.mask = mask;
}
我有什么方法可以覆盖这样做?我看到类似问题的答案,但不适合我。
答案 0 :(得分:0)
我找到了解决方案。我在我的视图中创建了另一个视图,并将掩码应用于她。
代码:
@interface BubbleView()
@property (nonatomic, strong) UIView *maskView;
@end
@implementation BubbleView
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
self.maskView = [[UIView alloc] init];
[self addSubview:self.maskView];
self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
return self;
}
- (void)drawRect:(CGRect)rect {
self.maskView.frame = self.bounds;
self.maskView.backgroundColor = [UIColor whiteColor];
[super drawRect:rect];
}
- (void)layoutSubviews {
[super layoutSubviews];
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){self.maskView.frame.origin.x + 10, self.maskView.frame.origin.y}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x + self.maskView.frame.size.width, self.maskView.frame.origin.y}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x + self.maskView.frame.size.width, self.maskView.frame.origin.y + self.maskView.frame.size.height}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x +10, self.maskView.frame.origin.y + self.maskView.frame.size.height}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x +10, self.maskView.frame.origin.y + (self.maskView.frame.size.height/2) + 5}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x , self.maskView.frame.origin.y + (self.maskView.frame.size.height/2)}];
[path addLineToPoint:(CGPoint){self.maskView.frame.origin.x +10, self.maskView.frame.origin.y + (self.maskView.frame.size.height/2) - 5}];
[path closePath];
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.maskView.bounds;
mask.path = path.CGPath;
self.maskView.layer.mask = mask;
[self layoutIfNeeded];
}