터치가 다른 모양의 프레임 위에 있는지 어떻게 알 수 있습니까?
-
16-11-2019 - |
문제
위치를 만지는 방법을 알고 있으므로 이제는 터치 된 위치가있는 CGPoint가 있습니다.터치가 UIView 이상인 경우 알아내는 가장 좋은 방법은 무엇입니까?나는 그 방법을 알고있다 :
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
.
등.하지만 이것이 가장 좋은 방법입니까?
해결책
If you simply want to know if a point is inside the bounds of a view, you can use the pointInside:withEvent:
method.
CGPoint touchPoint = [theTouch locationInView:theView];
// If the point was retrieved for a different view, it must be converted to the coordinate space of the destination view using convertPoint:fromView:
if([theView pointInside:touchPoint withEvent:nil]) {
NSLog(@"point inside");
}
다른 팁
Ugh's answer covers views, which I believe is what you want; if you have an arbitrary CGRect
, you can use CGRectContainsPoint
:
BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
Yes, CGRectContainsPoint will save you from writing so many comparision equations.
제휴하지 않습니다 StackOverflow