如何检测图像是否被触摸

时间:2011-07-05 03:20:51

标签: iphone objective-c xcode image touch

如何检测Xcode中是否触摸了图像?我查看了Apple文档,这真令人困惑...... 我看到了:

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

但我的形象仍然不会移动。我尝试使用touchesBegan + touchesMoved,并将图像的userInteractionIsEnabled设置为YES,但它仍然无法检测到它:(


编辑:非常感谢您的好建议!最后,我真的想让它变得尽可能简单,我知道我的代码应该工作,所以我一直在摆弄它,经过一夜好眠,我意识到它是一个相当简单的解决方案:

在我的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];

6 个答案:

答案 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 
   {

   }

答案 1 :(得分:3)

您可以将UIPanGesureRecognizer添加到持有UIImage的UIImageView中。这将允许您检测用户何时在图像上平移并相应地移动图像转换。这里是对文档的引用。

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

使用手势识别器很好,因为它可以保持与操作系统其余部分的一致性,就像平移一样。

希望这有帮助。

答案 2 :(得分:3)

你需要touchesBegan方法,例如

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

我刚读过你尝试过的,虽然我不确定它为什么不起作用。请尝试使用下面建议的点按手势。

答案 3 :(得分: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");
}
}

根据您的要求调整高度,宽度和名称。

答案 4 :(得分: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
  }

答案 5 :(得分:2)

如果你不是特别喜欢使用UIImageView,请尝试使用带有图像的自定义按钮,并使用动作方法touchDown和touchDraggedOut来移动图像。