タッチを受け取ることができるレイヤーマスク (フレームより小さい) を持つ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メソッドで; 電話;
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];
これで、タッチ メソッドは、タッチが円マスク ビューにあるかどうかのみを検出します。
レイヤーの特定の位置にソリッドまたは透明なピクセルがあるかどうかを確認するメソッドを作成することで解決しました。
- (BOOL)isSolidPixel:(CGImageRef)image withXPosition:(int)xPos andYPosition:(int)yPos {
if(xPos > CGImageGetWidth(image) || yPos > CGImageGetHeight(image))
return NO;
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image));
const UInt8* data = CFDataGetBytePtr(pixelData);
int pixelInfo = yPos * CGImageGetBytesPerRow(image) + xPos * 4;
UInt8 alpha = data[pixelInfo];
CFRelease(pixelData);
if (alpha)
return YES;
return NO;
}