UIView 子类中的 CAShapeLayer [英] CAShapeLayer in UIView Subclass
本文介绍了UIView 子类中的 CAShapeLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用掩码创建 UIView
的子类.我尝试使用 drawRect
和 layoutSubviews
但没有成功,在这些尝试中没有出现视图.
I want create the subclass of UIView
with a mask. I tried used drawRect
and layoutSubviews
but not had success, in theses attempts the view not appear.
- (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;
}
我要重写的方法是什么?我看到类似问题的很多答案,但对我不起作用.
What is the method I have override for do this? I see very answers for similar questions but not work for me.
推荐答案
我找到了解决方案.我在我的视图中创建另一个视图并在她身上应用蒙版.
I find a solution for this. I create another view inside the my view and apply the mask in her.
代码:
@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];
}
这篇关于UIView 子类中的 CAShapeLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文