gpt4 book ai didi

objective-c - 点击测试 UIView 子类的填充区域

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:29 25 4
gpt4 key购买 nike

我有一些 UIView 子类,我在 drawRect 中绘制 UIBezierPaths。在添加这些 View 的 viewController 中,我需要进行 HitTest 以查看贝塞尔曲线路径内是否发生了点击。我尝试在 View 子类中创建一个 UIBezierPath 变量,然后对其进行测试。但当然,偏移是完全错误的——我会在屏幕的上角得到点击,而不是在形状上。

有谁能建议最好的方法吗?这是否有意义,或者我应该添加一些代码?

谢谢,詹姆斯

最佳答案

这是我的自定义三角形 View 。它比贝塞尔路径简单得多,但我相信它的工作原理应该差不多。我还有一个基于 alpha 级别的类别,以像素为单位进行 HitTest ,我将其用于具有 alpha 层的 UIImage。 (在这篇文章中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;
}
}

关于objective-c - 点击测试 UIView 子类的填充区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com