在Obj-C(iPad)中绘制一个形状,看看我是否点了它

时间:2011-05-19 17:27:31

标签: ipad uigesturerecognizer tap cgpath

我有两个与内部奇怪形状的png重叠的uiviews。我想在每个uiview的顶部绘制一个不可见的形状,并检测我是否点击了这些形状。否则,重叠可以看到水龙头。我认为下面的代码会允许这种情况发生,但根本没有任何触摸显示。

-(void)drawRect:(CGRect)rect {
    quadrantOnePath = CGPathCreateMutable();
    CGPathMoveToPoint(quadrantOnePath, NULL, 30, 214);
    CGPathAddLineToPoint(quadrantOnePath, NULL, 281, 47);
    CGPathAddLineToPoint(quadrantOnePath, NULL, 493, 51);
    CGPathAddLineToPoint(quadrantOnePath, NULL, 306, 376);
    CGPathCloseSubpath(quadrantOnePath);
}

//in viewdidload

UITapGestureRecognizer *tapBlueRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewBlueFloorplans:)];
[tapBlueRecognizer setNumberOfTapsRequired:1];
[tapBlueRecognizer setDelegate:self];
[floorBlueHolderview addGestureRecognizer:tapBlueRecognizer];


// viewbluefunction
-(void)viewBlueFloorplans:(id)sender meso:(UIGestureRecognizer *)recognizer {

   CGPoint factor = [recognizer locationOfTouch:0 inView:self.view];
   bool processTouch = CGPathContainsPoint(quadrantOnePath, NULL, factor, true);

   if(processTouch) {
    NSLog(@"tap_viewBLU");
   }
}

1 个答案:

答案 0 :(得分:1)

1,您实际上不必绘制形状以便能够在其上使用CGPathContainsPoint。如果你知道形状是正确的那么就把

quadrantOnePath = CGPathCreateMutable();
CGPathMoveToPoint(quadrantOnePath, NULL, 30, 214);
CGPathAddLineToPoint(quadrantOnePath, NULL, 281, 47);
CGPathAddLineToPoint(quadrantOnePath, NULL, 493, 51);
CGPathAddLineToPoint(quadrantOnePath, NULL, 306, 376);
CGPathCloseSubpath(quadrantOnePath);

如果形状没有改变,则进入“viewdidload”。 (为防止内存泄漏,请不要忘记发布quadrantOnePath) 如果您不想调试形状,请绘制它。

2,@selector(viewBlueFloorplans:)不会调用-(void)viewBlueFloorplans:(id)sender meso:(UIGestureRecognizer *)recognizer

@selector(viewBlueFloorplans:)将致电-(void)viewBlueFloorplans:(UIGestureRecognizer *)recognizer

3,如果你实际上没有实现委托方法,那么就不应该设置[tapBlueRecognizer setDelegate:self];(为了节省一些计算)。