我有一个 UIView ,其图层蒙版(小于其框架)可以接收触摸。
现在,我要限制图层蒙版中的那些接触。
遮罩是渲染的形状,并不总是矩形。
我是否需要这样做:
pointInside:withEvent:
要么
hitTest:withEvent:
还是有更好的解决方案?
最佳答案
这个问题有点老了,但也许对某人有用:)
在您的.h文件中;
@interface CustomPhotoFrame : UIView {
@protected
CGPathRef borderPath;
}
@property (nonatomic, assign) CGPathRef borderPath;
并且,在您的.m文件中;
@synthesize borderPath;
- (void)setClippingPath:(CGPathRef)path
{
CGPathRelease(borderPath);
borderPath = path;
CGPathRetain(borderPath);
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return CGPathContainsPoint(borderPath, NULL, point, FALSE);
}
用你的drawrect方法; call ;
UIBezierPath *aPath = [UIBezierPath bezierPath];
// your path codes.. assume that its a circle;
aPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(centeredCircleRect)];
CGPathRef cgPath = CGPathCreateCopy(aPath.CGPath);
[self setClippingPath:cgPath];
现在,您的触摸方法仅检测您的触摸是否处于圆形蒙版视图中。
关于ios - iOS:触摸外层蒙版但内框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11540047/