2013-10-16 108 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