如何测试点是否在视图中 [英] How to test if a point is in a view
本文介绍了如何测试点是否在视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UIImageView
,屏幕上有一个 CGPoint
。我希望能够测试这一点,看它是否在 UIImageView
中。最好的方法是什么?
I have a UIImageView
and I have a CGPoint
on the screen. I want to be able to test that point to see if it is in the UIImageView
. What would be the best way to do this?
推荐答案
CGPoint
是对参考点没有好处。如果您的点位于窗口坐标中,那么您可以使用
CGPoint
is no good with a reference point. If your point is in window's coordinates then you can get it using
CGPoint locationInView = [imageView convertPoint:point fromView:imageView.window];
if ( CGRectContainsPoint(imageView.bounds, locationInView) ) {
// Point lies inside the bounds.
}
您也可以致电 pointInside:withEvent:
method
You may also call pointInside:withEvent:
method
if ( [imageView pointInside:locationInView withEvent:nil] ) {
// Point lies inside the bounds
}
这篇关于如何测试点是否在视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文