2011-07-05 78 views
8

如何檢測Xcode中的圖像是否被觸摸?我已經看過蘋果的文檔,它真的是彆扭...... 我看到:如何檢測圖像是否被觸摸

if (CGRectContainsPoint([imageView frame], location)) 

,但我的形象仍不會移動。我嘗試使用的touchesBegan + touchesMoved,並設置圖像的userInteractionIsEnabled爲YES,但它仍然不會檢測到它:(


編輯:謝謝大家這麼多的偉大的建議最終,我真的想讓它儘可能簡單,我知道我的代碼應該工作,所以我一直用它擺弄,和一個良好的夜間睡眠後,我意識到這是一個相當簡單的解決方案:

在我的動作移動:

UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    CGRect shapeRect = [imageView frame]; 
    CGRect dropSquareRect = [dropSquare frame]; 

    CGPoint touchLocation = CGPointMake(location.x, location.y); 

    if (CGRectContainsPoint(shapeRect, touchLocation)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [imageView setCenter:touchLocation]; 
     [UIView commitAnimations]; 
    } 

    if (CGRectIntersectsRect(shapeRect, dropSquareRect)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     self.imageView.alpha = 0; 
     self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y); 
     self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8); 
     [UIView commitAnimations]; 

回答

3

您可以將UIPanGesureRecognizer添加到保存UIImage的UIImageView中。這將允許您檢測用戶何時平移圖像並相應地移動圖像翻譯。這裏是對文檔的引用。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html

它使用手勢識別,因爲它保持與操作系統的其餘部分的一致性,儘量淘洗去很好。

希望這會有所幫助。

+0

這是應該做的......上帝的答案 –

3

您需要touchesBegan方法,例如

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
CGPoint myPoint = [touch locationInView:self]; 
    if (CGRectContainsPoint([imageView frame], location)){ 
    // code here    
    } 
} 

我剛剛看了你嘗試了,雖然我不知道爲什麼它不工作。請按照以下建議嘗試使用輕按手勢。

2

您可以使用此:

做繼視圖設置並加載:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 

然後使用以下方法:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     //Do moving 
    } 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     // do moving 
    } 
+0

非常無關的答案...... –

13

你可以考慮使用UITapGestureRecognizerUIImageView檢測觸動。

也不要忘記將userInteractionIsEnabled設置爲YES

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(imageTapped:)]; 
myImageView.userInteractionIsEnabled = YES; 
[myImageView addGestureRecognizer:tap]; 
[tap release]; 

執行imageTapped:方法。

- (void)imageTapped:(UITapGestureRecognizer *) gestureRecognizer 
    { 

    } 
3

你可以試試這個 假設你有一個

IBOutlet UIImageView *touchImageVIew; 
Let touchImageVIew height and width are 50,25; 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 

    CGPoint pt = [[touches anyObject] locationInView:touchImageVIew]; 

    if (pt.x>=0 && pt.x<=50 && pt.y>=0 && pt.y<=25) { 
     NSLog(@"image touched"); 
    } 

    else 
{ 
NSLog(@"image not touched"); 
} 
} 

調整高度,寬度和名稱根據您的要求。

+1

這是一個很好的解決方案。 – Siriss

2

如果你不是特別喜歡使用UIImageView,請嘗試使用自定義按鈕以及其中的圖像,並使用動作方法touchDown和touchDraggedOut移動圖像。

相關問題