命测试 UIView 子类的填充区域
问题说明
我有一些 UIView 子类,我在 drawRect 中绘制了 UIBezierPaths.在添加这些视图的 viewController 中,我需要进行命中测试以查看是否在贝塞尔曲线路径内发生了点击.我尝试在视图子类中创建一个 UIBezierPath 变量,然后对其进行测试.但当然,偏移量是完全错误的 - 我会在屏幕的顶角而不是形状上得到点击.
I have some UIView subclasses where I draw UIBezierPaths in drawRect. In the viewController that adds these views, I need to do a hit test to see if a tap happened inside the bezier path. I tried creating a UIBezierPath variable in the view subclass, then testing against that. But of course, the offset was completely wrong - I would get hits in the top corner of the screen, rather than over the shape.
谁能建议最好的方法来做到这一点?这是否有意义,还是我应该添加一些代码?
Can anyone suggest the best way to do this? Does this make sense, or should I add some code?
谢谢,詹姆斯
正确答案
This is a custom triangle view I have. Its much simpler than a bezier path but I believe it should work about the same. I also have a category that hittests based on alpha level on a pixel-per-pixel basis that I use for UIImages with alpha layers. (Its in this post Retrieving a pixel alpha value for a UIImage)
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 0.0, 0.0);
CGContextAddLineToPoint(context, rect.size.width, 0.0);
CGContextAddLineToPoint(context, 0.0, rect.size.height);
CGContextClosePath(context);
CGContextSetFillColorWithColor(context, triangleColor.CGColor);
CGContextFillPath(context);
CGContextSaveGState(context);
[self.layer setShouldRasterize:YES];
[self.layer setRasterizationScale:[UIScreen mainScreen].scale];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGMutablePathRef trianglePath = CGPathCreateMutable();
CGPathMoveToPoint(trianglePath, NULL, 0.0, 0.0);
CGPathAddLineToPoint(trianglePath, NULL, self.frame.size.width, 0.0);
CGPathAddLineToPoint(trianglePath, NULL, 0.0, self.frame.size.height);
CGPathCloseSubpath(trianglePath);
if (CGPathContainsPoint(trianglePath, nil, point, YES)) {
CGPathRelease(trianglePath);
return self;
} else {
CGPathRelease(trianglePath);
return nil;
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfighg
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20