如何测试一个点是否在视图中 [英] 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:
方法
You may also call pointInside:withEvent:
method
if ( [imageView pointInside:locationInView withEvent:nil] ) {
// Point lies inside the bounds
}
这篇关于如何测试一个点是否在视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文