【问题标题】:How to find out the touch screen position如何找出触摸屏的位置
【发布时间】:2012-09-15 05:29:36
【问题描述】:

我正在开发一个应用程序。因为我正在使用一个 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;
} 

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

【问题讨论】:

    标签: iphone


    【解决方案1】:
    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;
    
        }
    
    }
    

    【讨论】:

    • 每次都去else块。我用touchEnded方法写了你的代码。
    【解决方案2】:
    //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);
    
    }
    

    【讨论】:

    • 当我点击我的那个 imageview 时。显示 x 和 y 值低于那个 imageview x 和 y 值。
    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多