iPhone的中途x和y坐标

时间:2014-07-25 06:45:22

标签: ios iphone resolution screen-orientation

我正在尝试找到应用视图上的水龙头位置。 我曾经问过一些人说x为160,但他们不知道y。

现在我只需要x,但知道y不会伤到知道。

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
      UITouch *myTouch = [touches anyObject];

      CGPoint aPoint = [myTouch locationInView:self.view]; //locates x cords of touch

      if (aPoint.x <160){                                      //touches left side
        Player.image = [UIImage imageNamed:@"Person1.png"];
        [NSTimer scheduledTimerWithTimeInterval:0.25 target:self                 
        selector:@selector(changeImageleft) userInfo:nil repeats:NO];
        }
    if (aPoint.x >=160){                                      //touches right side        
        Player2.image = [UIImage imageNamed:@"Person1.png"];
        [NSTimer scheduledTimerWithTimeInterval:0.25 target:self
        selector:@selector(changeImageright) userInfo:nil repeats:NO];
       }
    }

这是我必须区分视图的左侧和右侧。

2 个答案:

答案 0 :(得分:1)

您不应该将设备的尺寸硬编码到您的代码中,Android开发人员必须更加努力。 iPhone 4和iPhone 5的宽度相同,但高度不同。当方向改变时会发生什么?

您应该通过代码请求维度,这里是获取两个中间点的代码:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
  CGFloat midX = CGRectGetWidth(self.view.bounds) / 2.0f; 
  CGFloat midY = CGRectGetHeight(self.view.bounds) / 2.0f; 
}

答案 1 :(得分:0)

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *myTouch = [touches anyObject];

    CGPoint aPoint = [myTouch locationInView:self.view]; //locates x cords of touch
    CGFloat midX = self.bounds.size.width / 2.0f;
    SEL sel;

    if (aPoint.x < midX) {  //touches left side
      sel = @selector(changeImageleft);
    }
    else {  //touches right side        
      sel = @selector(changeImageright);
    }

    Player.image = [UIImage imageNamed:@"Person1.png"];
    [self performSelector:sel
               withObject:nil
               afterDelay:0.25];
  }