2013-10-16 70 views
1

我使用UIScrollView与PagingEnabled,在UIScrollView内我添加了三个UIImage。它工作正常。UIScrollView检测用户水龙头

我想知道如何检测用户是否在UIImage中的两个方块之间轻击,例如:在附加图像中,如何检测用户是否在方块1和方块2之间轻击或用户是否在方块2和3?

任何想法?

谢谢。

enter image description here

+0

添加手势? ;) –

+0

当然我会添加手势,但是如果它位于方形5和6之间,我该如何检测触摸位置。@TotumusMaximus – Mariam

+0

您可以在5和6之下创建一个视图,该视图具有框架,5和6的最小x,最小y为5和6,最大x的宽度为5和6,最大y的高度为5和6.然后在手势处理程序中检测哪个立方体更接近(因为您将拥有许多这些不可见的视图)或使某种视图层次结构,以便将具有更多区域的视图放置在屏幕的较低层。 –

回答

1

添加手势图像视图

imageView.userInteractionEnabled = YES; 
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handlePinch:)]; 
pgr.delegate = self; 
[imageView addGestureRecognizer:pgr]; 
[pgr release]; 
: 
: 
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer 
{ 
    //handle pinch... 
} 
+0

也检查UIImageView的userInteractionEnabled是YES – nivritgupta

+0

我不需要用户移动两个手指对方,所以我不需要使用UIPinchGestureRecognizer。 @nivritgupta – Mariam

+0

不明白为什么这个答案得到一个+1,因为它显然不是他想要的答案。 –

0

为了检测单个或多个抽头使用UITapGestureRecognizer,其UIGestureRecognizer一个子类。您不应该忘记将userInteractionEnabled属性设置为YES,因为UIImageView - 类将默认值更改为NO

self.imageView.userInteractionEnabled = YES; 
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; 
// Set the number of taps, if needed 
[tapRecognizer setNumberOfTouchesRequired:1]; 
// and add the recognizer to our imageView 
[imageView addGestureRecognizer:tapRecognizer]; 

- (void)handleTap:(UITapGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     // if you want to know, if user tapped between two objects 
     // you need to get the coordinates of the tap 
     CGPoint point = [sender locationInView:self.imageView]; 
     // use the point 
     NSLog(@"Tap detected, point: x = %f y = %f", point.x, point.y); 
     // then you can do something like 
     // assuming first square's coordinates: x: 20.f y: 20.f width = 10.f height: 10.f 
     // Construct the frames manually 
     CGRect firstSquareRect = CGRectMake(20.f, 20.f, 10.f, 10.f); 
     CGRect secondSquareRect = CGRectMake(60.f, 10.f, 10.f, 10.f); 
     if(CGRectContainsPoint(firstSquareRect, point) == NO && 
      CGRectContainsPoint(secondSquareRect, point) == NO && 
      point.y < (firstSquareRect.origin.y + firstSquareRect.size.height) /* the tap-position is above the second square */) { 
     // User tapped between the two objects 
     } 
    } 
} 
+0

好吧然后得到协调员后,你认为我会使用if - else取决于协调员? @falsecrypt – Mariam

+0

@Mariam我编辑了我的答案 – falsecrypt

+0

我不能使用[firstSquare frame],因为正方形在图像内,它不是图像本身。 – Mariam