首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS hitTest withEvent

iOS hitTest withEvent
EN

Stack Overflow用户
提问于 2013-09-27 01:16:03
回答 1查看 2.1K关注 0票数 1

我正在尝试使用UImageView withEvent获取对蒙面UIImageView下面的一个hitTest的引用。以下是我所拥有的不起作用的东西:

UIView A,包含3 UIImageViews作为子视图: panelOne、panelTwo和panelThree。panelThree占据了整个框架,但被蒙蔽成一个三角形,显示了面板一和二的部分。因此,我需要检测用户何时点击该矩形外部并将触摸发送到适当的UIImageView。

代码:(CollagePanel是UIImageView的子类)

代码语言:javascript
复制
-(void)triangleInASquare
{
    CGSize size = self.frame.size;
    CollagePanel *panelOne = [[CollagePanel alloc] initWithFrame:CGRectMake(0,0, size.width/2, size.height)];
    panelOne.panelScale = panelOne.frame.size.width/self.frame.size.width;
    panelOne.backgroundColor = [UIColor greenColor];

    CollagePanel *panelTwo = [[CollagePanel alloc] initWithFrame:CGRectMake(size.width/2,0, size.width/2, size.height)];
    panelTwo.panelScale = panelOne.frame.size.width/self.frame.size.width;
    panelTwo.backgroundColor = [UIColor purpleColor];

    CollagePanel *panelThree = [[CollagePanel alloc] initWithFrame:CGRectMake(0,0, size.width, size.height)];
    panelThree.backgroundColor = [UIColor orangeColor];

    UIBezierPath* trianglePath = [UIBezierPath bezierPath];
    [trianglePath moveToPoint:CGPointMake(0, panelThree.frame.size.height)];
    [trianglePath addLineToPoint:CGPointMake(panelThree.frame.size.width/2,0)];
    [trianglePath addLineToPoint:CGPointMake(panelThree.frame.size.width, panelTwo.frame.size.height)];
    [trianglePath closePath];

    // Mask the panels's layer to triangle.
    CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
    [triangleMaskLayer setPath:trianglePath.CGPath];
    triangleMaskLayer.strokeColor = [[UIColor whiteColor] CGColor];
    panelThree.layer.mask = triangleMaskLayer;

    //Add border
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.strokeColor = [[UIColor whiteColor] CGColor];
    borderLayer.fillColor = [[UIColor clearColor] CGColor];
    borderLayer.lineWidth = 6;
    [borderLayer setPath:trianglePath.CGPath];

    [panelThree.layer addSublayer:borderLayer];

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:panelOne];
    [tempArray addObject:panelTwo];
    [tempArray addObject:panelThree];

    [self addGestureRecognizersToPanelsInArray:tempArray];
    [self addPanelsFromArray:tempArray];
    self.panelArray = tempArray;
}

-(void)handleTap: (UITapGestureRecognizer*) recognizer //coming from panel.imageView
{
    CGPoint tapPoint = [recognizer locationInView:self];
    NSLog(@"Location in self: %@", NSStringFromCGPoint(tapPoint));
    NSLog(@"self.subviews: %@", self.subviews);
    UIView *bottomView = [self hitTest:tapPoint withEvent:nil];
    NSLog(@"Bottom View: %@", bottomView);
}

NSLog of bottomView总是panelThree (最上面的面板)。据我所知,命中测试应该返回“最底层”的子视图。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-27 01:34:27

你搞错了。它将返回一个视图,它会识别自己是被触摸的,并且离你的手指最近,离顶端更近。

如果视图在某一点上不承认自己是触摸,则需要覆盖。

代码语言:javascript
复制
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

为了那景色。

我认为奥勒·贝格曼( Ole Begemann)型按钮是如何做到这一点的一个很好的例子。

在您的项目中,此方法可以确定路径中是否存在一个点:CGPathContainsPoint

您的pointInside:withEvent:可能如下所示:

代码语言:javascript
复制
#import "CollagePanel.h"
#import <QuartzCore/QuartzCore.h>

@implementation CollagePanel
//
// ...
//
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint p = [self convertPoint:point toView:[self superview]];
    if(self.layer.mask){
        if (CGPathContainsPoint([(CAShapeLayer *)self.layer.mask path], NULL, p, YES) )
            return YES;
    }else {
        if(CGRectContainsPoint(self.layer.frame, p))
            return YES;
    }
    return NO;
}

@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19041233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档