• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

命测试 UIView 子类的填充区域

用户头像
it1352
帮助1

问题说明

我有一些 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?

谢谢,詹姆斯

正确答案

#1

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
系列文章
更多 icon
同类精品
更多 icon
继续加载