2011-07-05 10 views
8

画像がXcodeでタッチされたかどうかをどのように検出できますか?画像がタッチされたかどうかを検出する方法

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

が、私のイメージはまだ動きません:私が見た ...私はアップル社のドキュメントを見ていると、それは本当に混乱しています。私はtouchesBegan + touchesMovedを使用してみました、とYESに画像のuserInteractionIsEnabledを設定するが、それはまだそれを検出しません:(


EDIT:!は最終的にはあなたの偉大な提案をとてもありがとうございました、私は実際にそれをできるだけシンプルにしたかった、と私は私のコード仕事をする必要があることを知っていたので、私はそれをいじる保ち、良い夜の眠りの後、私はそれはかなり単純な解決策だった実現:

で私のtouchesMoved:

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

UIImageを保持しているUIImageViewにUIPanGesureRecognizerを追加することができます。これにより、ユーザーが画像をパンニングしているときを検出し、それに応じて画像の移動を移動することができます。ドキュメントへの参照はここにあります。

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

それはそれは限りパンが行くようにOSの残りの部分との整合性を保つため、ジェスチャー認識装置を使用してうれしいです。

これが役に立ちます。

+0

これは、これは素晴らしいソリューションです...神 –

3

あなたはタッチが必要です。

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

は、私はちょうどあなたが、私はそれが動作しない理由はわからないが、それをしようとしたことを読みました。下記のようにタップジェスチャーを試してみてください。

2

あなたがこれを使用することができます:

は、ビューに設定次の操作を行うには、負荷をした:

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

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

非常に無関係な答えを答える...どうすべきか –

13

あなたが検出するUIImageViewUITapGestureRecognizerの使用を検討できます。

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]; 

は、その後、次のメソッドを使用しますタッチ。

また、userInteractionIsEnabledYESに設定することを忘れないでください。

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を使用してその特定されていない場合は、それにあなたのイメージにカスタムボタンを使用して試してみて、画像を移動するためのアクションメソッド、タッチダウンとtouchDraggedOutを使用しています。

関連する問題