IOS:触摸外层图层,但在框架内部

Ori*_*itm 1 mask uiview ios

我有一个UIView,它有一个可以接收触摸的图层掩码(小于它的帧).现在的问题是我想限制图层蒙版中的那些触摸.

蒙版是渲染的形状,并不总是矩形.

我必须这样做:

pointInside:withEvent:
Run Code Online (Sandbox Code Playgroud)

要么

hitTest:withEvent:
Run Code Online (Sandbox Code Playgroud)

或者有更好的解决方案吗?

Qua*_*aso 8

它有点老问题,但也许对某人有用:)

在您的视图.h文件;

@interface CustomPhotoFrame : UIView {
  @protected
    CGPathRef borderPath;
}
@property (nonatomic, assign) CGPathRef borderPath;
Run Code Online (Sandbox Code Playgroud)

并且,在您的.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);
}
Run Code Online (Sandbox Code Playgroud)

在你用粗略的方法; 呼叫;

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];
Run Code Online (Sandbox Code Playgroud)

现在,您的触摸方法仅检测您的触摸是否处于圆圈蒙版视图中.