objective-c – 命中测试UIView子类的填充区域
发布时间:2020-12-16 07:31:56 所属栏目:百科 来源:网络整理
导读:我有一些UIView子类,我在drawRect中绘制UIBezierPaths.在添加这些视图的viewController中,我需要进行命中测试以查看bezier路径内是否发生了点击.我尝试在视图子类中创建一个UIBezierPath变量,然后对其进行测试.但是当然,偏移是完全错误的 – 我会在屏幕的上
我有一些UIView子类,我在drawRect中绘制UIBezierPaths.在添加这些视图的viewController中,我需要进行命中测试以查看bezier路径内是否发生了点击.我尝试在视图子类中创建一个UIBezierPath变量,然后对其进行测试.但是当然,偏移是完全错误的 – 我会在屏幕的上角点击,而不是在形状上.
谁能建议最好的方法呢?这有意义吗,还是应该添加一些代码? 谢谢, 解决方法
这是我的自定义三角视图.它比bezier路径简单得多,但我相信它应该工作相同.我还有一个基于alpha级别的测试类别,基于像素每像素,我用于带有alpha图层的UI
Images. (在这篇文章中
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,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); CGPathAddLineToPoint(trianglePath,self.frame.size.width,self.frame.size.height); CGPathCloseSubpath(trianglePath); if (CGPathContainsPoint(trianglePath,nil,point,YES)) { CGPathRelease(trianglePath); return self; } else { CGPathRelease(trianglePath); return nil; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |