简体   繁体   English

在对UIImageView进行旋转之后,我应该如何检查触摸点是否在UIImageView区域内?

[英]After rotation being made to UIImageView, how should I check if a touch point is inside the UIImageView region?

I have tried checking the CGRect: 我试过检查CGRect:

 CGFloat imageX1 = imageView.frame.origin.x;
 CGFloat imageY1 = imageView.frame.origin.y;
 CGFloat imageX2 = imageView.frame.origin.x + imageView.frame.size.width;
 CGFloat imageY2 = imageView.frame.origin.y + imageView.frame.size.height;

 //NSLog(@"(%f, %f) - (%f, %f)", imageX1, imageY1, imageX2, imageY2); 
 if(imageX1 < touchedX && touchedX < imageX2 && 
     imageY1 < touchedY && touchedY < imageY2){ }

And then I know that this type of checking only works when the UIImageView has not been rotated/scaled. 然后我知道这种类型的检查仅在UIImageView没有旋转/缩放时才有效。

I have tried CGRectApplyAffineTransform(imageView.frame, imageView.transform); 我试过CGRectApplyAffineTransform(imageView.frame, imageView.transform); , but It creates a smallest rect for the transformed UIImageView. ,但它为转换后的UIImageView创建了一个最小的rect。

So let say you have an UIImageView rotated 45 degrees, and scaled to 1.5. 因此,假设您将UIImageView旋转45度,并缩放到1.5。 Now when you touch a couple pixels outside the UIImageView, it still considers the touch point is inside the UIImageView Rect, but It's not. 现在,当您触摸UIImageView外部的几个像素时,它仍然认为触摸点位于UIImageView Rect内部,但事实并非如此。 The UIImageView is rotated and scaled, but the CGRect is still the original one. UIImageView是旋转和缩放的,但CGRect仍然是原始的。

Anyone knows about how to deal with this? 有谁知道如何处理这个? I am pretty new to graphic stuff. 我对图形的东西很新。 Anything would help. 什么都有帮助。 Thanks! 谢谢!

try this 尝试这个

CGPoint touchLocation = [touch locationInView:self];
 if (CGRectContainsPoint(myImageView.frame, touchLocation))

I answer my own question. 我回答我自己的问题。

I moved the touch event inside a custom ImageView class, and having a ref of the parent UIView. 我将触摸事件移动到自定义ImageView类中,并具有父UIView的引用。

It solved the problem. 它解决了这个问题。

Thanks Ankit for the hint! 感谢Ankit提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM