我想确定点按的位置是否在区域内。我有4个CGPoints,我知道这可以通过使用UITouch来完成。此外,我使用功能
有屏幕点击位置- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView:self.view];
}
例如我的4个CGPoints
self.firstPoint = CGPointMake(50.0f, 50.0f);
self.secondPoint = CGPointMake(200.0, 50.0);
self.thirdPoint = CGPointMake(200.0, 200.0);
self.fourthPoint = CGPointMake(50.0, 120.0);
提前致谢
答案 0 :(得分:2)
您应该使用CGRect
代表rect而不是CGPoint
,然后使用CGRectContainsPoint()
检查rect是否包含该点。
答案 1 :(得分:1)
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
image=[UIImage imageNamed:@"anyImage.gif"];
newView = [[UIImageView alloc]initWithImage:image];
if (location.y<480|| location.y>50)
{
//write your code
}
}