2013-06-27 42 views
0

我想确定点击的位置是否在区域内。我有4 CGPoints,我知道这可以通过使用UITouch来完成。另外,我有屏幕使用功能抽头位置确定iPhone屏幕点击是否在区域?

- (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

@ H2CO3除非最后一点是错字,那不是矩形。 – Kevin

+0

@ H2CO3因为他的区域不是矩形,所以他应该创建一个CGPath并使用'CGPathContainsPoint',或者纠正他的问题,以便他的点组成一个矩形。 –

+0

@凯文和乔纳森:对。 – 2013-06-27 12:48:47

回答

2

您应该使用CGRect来表示矩形,而不是四个CGPoint s,然后用CGRectContainsPoint()检查rect是否包含该点。

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 

    } 


}