IOS:验证点是否在矩形内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8037710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: verify if a point is inside a rect
提问by cyclingIsBetter
Is there a way to verify if a CGPoint
is inside a specific CGRect
.
有没有办法验证 aCGPoint
是否在特定的CGRect
.
An example would be:
I'm dragging a UIImageView
and I want to verify if its central point CGPoint
is inside another UIImageView
一个例子是:我正在拖动 aUIImageView
并且我想验证它的中心点CGPoint
是否在另一个内部UIImageView
回答by Ole Begemann
Swift 4
斯威夫特 4
let view = ...
let point = ...
view.bounds.contains(point)
Objective-C
目标-C
bool CGRectContainsPoint(CGRect rect, CGPoint point);
bool CGRectContainsPoint(CGRect rect, CGPoint point);
Parameters
参数
rect
The rectangle to examine.point
The point to examine. Return Value true if the rectangle is not null or empty and the point is located within the rectangle; otherwise, false.
rect
要检查的矩形。point
要检查的点。如果矩形不为 null 或为空并且点位于矩形内,则返回值为 true;否则为假。
A point is considered inside the rectangle if its coordinates lie inside the rectangle or on the minimum X or minimum Y edge.
如果一个点的坐标位于矩形内或最小 X 或最小 Y 边上,则认为该点位于矩形内。
回答by Julian Król
In Swift that would look like this:
在 Swift 中,它看起来像这样:
let point = CGPointMake(20,20)
let someFrame = CGRectMake(10,10,100,100)
let isPointInFrame = CGRectContainsPoint(someFrame, point)
Swift 3 version:
斯威夫特 3 版本:
let point = CGPointMake(20,20)
let someFrame = CGRectMake(10,10,100,100)
let isPointInFrame = someFrame.contains(point)
Link to documentation. Please remember to check containment if both are in the same coordinate system if not then conversions are required (some example)
回答by Stavash
UIView's pointInside:withEvent: could be a good solution. Will return a boolean value indicating wether or not the given CGPoint is in the UIView instance you are using. Example:
UIView 的 pointInside:withEvent: 可能是一个很好的解决方案。将返回一个布尔值,指示给定的 CGPoint 是否在您正在使用的 UIView 实例中。例子:
UIView *aView = [UIView alloc]initWithFrame:CGRectMake(0,0,100,100);
CGPoint aPoint = CGPointMake(5,5);
BOOL isPointInsideView = [aView pointInside:aPoint withEvent:nil];
回答by Chuy47
In swift you can do it like this:
在 swift 你可以这样做:
let isPointInFrame = frame.contains(point)
"frame" is a CGRect and "point" is a CGPoint
“frame”是一个 CGRect ,“point”是一个 CGPoint
回答by Tanvir Singh
In objective c you can use CGRectContainsPoint(yourview.frame, touchpoint)
在目标 c 中,您可以使用CGRectContainsPoint(yourview.frame, touchpoint)
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch* touch = [touches anyObject];
CGPoint touchpoint = [touch locationInView:self.view];
if( CGRectContainsPoint(yourview.frame, touchpoint) ) {
}else{
}}
In swift 3 yourview.frame.contains(touchpoint)
在 swift 3 yourview.frame.contains(touchpoint)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first!
let touchpoint:CGPoint = touch.location(in: self.view)
if wheel.frame.contains(touchpoint) {
}else{
}
}
回答by Pankaj purohit
It is so simple,you can use following method to do this kind of work:-
就这么简单,您可以使用以下方法来完成此类工作:-
-(BOOL)isPoint:(CGPoint)point insideOfRect:(CGRect)rect
{
if ( CGRectContainsPoint(rect,point))
return YES;// inside
else
return NO;// outside
}
In your case,you can pass imagView.centeras point and another imagView.frameas rect in about method.
在你的情况下,你可以通过imagView.center作为点和另一个imagView.frame作为 rect 在 about 方法中。
You can also use this method in bellow UITouchMethod :
您也可以在下面的UITouch方法中使用此方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
回答by Luis Franco R.
I'm starting to learn how to code with Swift and was trying to solve this too, this is what I came up with on Swift's playground:
我开始学习如何使用 Swift 进行编码并试图解决这个问题,这就是我在 Swift 的操场上想到的:
// Code
var x = 1
var y = 2
var lowX = 1
var lowY = 1
var highX = 3
var highY = 3
if (x, y) >= (lowX, lowY) && (x, y) <= (highX, highY ) {
print("inside")
} else {
print("not inside")
}
It prints inside
它在里面打印
回答by Anjali Prasad
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect rect1 = CGRectMake(vwTable.frame.origin.x,
vwTable.frame.origin.y, vwTable.frame.size.width,
vwTable.frame.size.height);
if (CGRectContainsPoint(rect1,touchLocation))
NSLog(@"Inside");
else
NSLog(@"Outside");
}