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;
} 

现在我可以触摸屏幕了。我需要找出的是我的触摸位置和图像视图位置是否正确。

4

2 回答 2

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;

    }

}
于 2012-09-15T06:02:31.933 回答
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);

}
于 2012-09-15T05:54:22.760 回答