gpt4 book ai didi

iphone - 如何检测图像是否被触摸

转载 作者:太空狗 更新时间:2023-10-30 03:27:29 34 4
gpt4 key购买 nike

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

最佳答案

您可以考虑使用 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 
{

}

关于iphone - 如何检测图像是否被触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577705/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com