xcode 如何检测图像是否被触摸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6577705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:25:07  来源:igfitidea点击:

How to detect if image is touched

iphoneobjective-cxcodeimagetouch

提问by user798370

How can you detect if an image is touched in Xcode? I have looked at Apple documentation and it is really confusing... I saw:

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

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

but my image still won't move. I tried using touchesBegan + touchesMoved, and set the image's userInteractionIsEnabled to YES, but it still won't detect it :(

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



EDIT:Thank you all so much for your great suggestions! In the end, I really wanted to make it as simple as possible, and I knew that my code shouldwork, so I kept fiddling with it, and after a good night's sleep, I realized it was a fairly simple solution:

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

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

回答by Jhaliya

you could consider using UITapGestureRecognizerwith UIImageViewto detect the touches.

您可以考虑使用 UITapGestureRecognizerwithUIImageView来检测触摸。

And also not forget to set userInteractionIsEnabledto YES.

也不要忘记设置userInteractionIsEnabledYES.

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

Implement imageTapped:method.

实施imageTapped:方法。

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

   }

回答by Jamie

You could add a UIPanGesureRecognizer to the UIImageView that's holding the UIImage. This will allow you to detect when the user is panning on the image and move the images translation accordingly. The reference to the documentation is here.

您可以向保存 UIImage 的 UIImageView 添加一个 UIPanGesureRecognizer。这将允许您检测用户何时平移图像并相应地移动图像平移。对文档的引用是here。

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

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

It's nice using the gesture recognizers since it keeps the consistency with the rest of the OS as far as panning goes.

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

Hope this helps.

希望这可以帮助。

回答by PatrickG4

You need the touchesBegan method e.g.

您需要 touchesBegan 方法,例如

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

I just read that you tried it, although I am not sure why it doesn't work. Try using the tap gesture as suggested below.

我刚刚读到您尝试过,尽管我不确定为什么它不起作用。尝试使用下面建议的点击手势。

回答by Gypsa

You can try this Suppose you have an

你可以试试这个假设你有一个

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");
}
}

Adjust height,width and name according to your requirement.

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

回答by Nithin

If you are not so particular in using UIImageView, try using a custom button with your image in it and use the action methods, touchDown and touchDraggedOut for moving the image.

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

回答by rptwsthi

You Can use this:

你可以使用这个:

Do Following setting in view did load:

在视图中执行以下设置确实加载了:

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

Then Use following methods:

然后使用以下方法:

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

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