如何检测图像是否被触摸

use*_*370 9 iphone xcode image objective-c touch

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

if (CGRectContainsPoint([imageView frame], location))
Run Code Online (Sandbox Code Playgroud)

但我的形象仍然不会动.我尝试使用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];
Run Code Online (Sandbox Code Playgroud)

Jha*_*iya 13

你可以考虑使用 UITapGestureRecognizerUIImageView检测触摸.

而且也没有忘记设置userInteractionIsEnabledYES.

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

实施imageTapped:方法.

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

   }
Run Code Online (Sandbox Code Playgroud)


Jam*_*mie 3

您可以将 UIPanGesureRecognizer 添加到保存 UIImage 的 UIImageView 中。这将允许您检测用户何时平移图像并相应地移动图像平移。文档的参考在这里。

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

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

希望这可以帮助。