2012-09-15 54 views
1

我正在開發一個應用程序。在這我使用一個UIImageView,我正在使用下面的代碼每0.5秒更改UIImageView的位置。如何找出觸摸屏的位置

[NSTimer scheduledTimerWithTimeInterval:0.5 
           target: self 
           selector:@selector(moveImage) 
           userInfo: nil repeats:YES]; 
-(void) moveImage 
{ 
    //[image1 setCenter: CGPointMake(634, 126)]; 
    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width); 
    CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height); 
    CGPoint squarePostion = CGPointMake(x, y); 
    img.center=squarePostion; 
} 

現在我可以觸摸屏幕。我需要了解的是我的觸摸位置和圖像視圖位置都正確與否。

回答

2
use this 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

{ 

    UITouch *touch = [[event allTouches] anyObject]; 

    CGPoint touchLocation = [touch locationInView:self.view]; 

if ([touch view] == photo1) { 

     //photo1 is image view give tag to it 
     if(photo1.tag==3) 

     { 
      NSLog(@"You have been touched image view"); 
     } 



     photo1.center = touchLocation; 

    } 

} 
+0

每次去其他block.I寫你的代碼touchEnded方法。 –

0
//it used to find the point in view 
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch * touch = [touches anyObject]; 

    CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow]; 
     NSLog(@"%f,%f",pos.x, pos.y); 

} 
+0

當我點擊我的那imageview.That顯示x和y的值低campared到ImageView的X和Y值。 –