Answers:
let view = ...
let point = ...
view.bounds.contains(point)
bool CGRectContainsPoint(CGRect rect, CGPoint point);
参量
rect
要检查的矩形。point
要检查的点。返回值如果矩形不为null或为空并且该点位于矩形内,则为true;否则为false。否则为假。如果点的坐标位于矩形内或最小X或最小Y边上,则认为该点在矩形内。
在Swift中看起来像这样:
let point = CGPointMake(20,20)
let someFrame = CGRectMake(10,10,100,100)
let isPointInFrame = CGRectContainsPoint(someFrame, point)
Swift 3版本:
let point = CGPointMake(20,20)
let someFrame = CGRectMake(10,10,100,100)
let isPointInFrame = someFrame.contains(point)
在目标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{
}}
快速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{
}
}
非常简单,您可以使用以下方法来进行此类工作:-
-(BOOL)isPoint:(CGPoint)point insideOfRect:(CGRect)rect
{
if ( CGRectContainsPoint(rect,point))
return YES;// inside
else
return NO;// outside
}
在您的情况下,可以在about方法中将imagView.center作为点传递,并将另一个imagView.frame作为rect 传递。
您还可以在下面的UITouch方法中使用此方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
我开始学习如何使用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")
}
在里面打印
- (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");
}