2014-09-28 51 views
0

在我当前的ios项目中,我将屏幕的一侧分配给一个对象,屏幕的另一侧分配给另一个对象,并且我做到了这样,如果您将手指划过指定对象上屏幕的一侧会移动。但是,我想让它能够在不同的运动中同时移动两个物体,但我无法弄清楚如何去做。以下是我目前的代码。同时移动2个对象

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    if (location.x <= 270) { 
     [Person setCenter:CGPointMake(location.x, Person.center.y)]; 

    } 
    else { 
     [Person1 setCenter:CGPointMake(location.x, Person1.center.y)]; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    if (location.x <= 270) { 
     [Person setCenter:CGPointMake(location.x, Person.center.y)]; 
    } 
    else { 
     [Person1 setCenter:CGPointMake(location.x, Person1.center.y)]; 
    } 
} 

回答

2

你应该开始处理那些在touches套装中提供多点触摸 - 通过所有的UITouch对象环路,做你的处理。

编辑: 这里是你的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    for(UITouch *touch in [event allTouches]) { 
     CGPoint location = [touch locationInView:touch.view]; 

     if (location.x <= 270) { 
      [Person setCenter:CGPointMake(location.x, Person.center.y)]; 
     } 
     else { 
      [Person1 setCenter:CGPointMake(location.x, Person1.center.y)]; 
     } 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    for(UITouch *touch in [event allTouches]) { 
     CGPoint location = [touch locationInView:touch.view]; 

     if (location.x <= 270) { 
      [Person setCenter:CGPointMake(location.x, Person.center.y)]; 
     } 
     else { 
      [Person1 setCenter:CGPointMake(location.x, Person1.center.y)]; 
     } 
    } 
} 
+0

我面对同样的问题与此代码,因为我用我的原始代码。我试图重写我的方法来编写我的机制,但我不能完全弄清楚如何让对象同时移动,谢谢您的输入,但是 – user3795419 2014-09-30 00:18:02

+0

@ user3795419所以当您开始进行多点触摸时发生了什么?你能形容吗?代码应该工作......谢谢 – DaTa 2014-09-30 01:23:46

+0

我只能一次移动其中一个对象。当我尝试通过在指定区域上滑动手指同时移动两个对象时,只有其中一个移动了 – user3795419 2014-09-30 03:31:53

0

如果移动-touchesBegan & touchesMoved代码到人视图类,而不是视图/或的viewController类是在目前那么这些意见可以处理触摸彼此独立并且同时。

*编辑:更多信息: 目前,您正在使用上面粘贴的代码处理触摸事件(我在猜测UIViewController),如果您将该代码移动到您创建的Person类中,您可以使代码更简单并达到你想要的结果。 这会产生这样的效果,即人将决定它是否被触摸以及在哪里移动。

在你Person.m文件中添加以下代码,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:self.superview]; 
    [self moveToLocation:location]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:self.superview]; 
    [self moveToLocation:location]; 
} 

-(void)moveToLocation:(CGPoint)location{ 
    CGFloat halfOfPersonWidth = self.bounds.size.width /2.0f; 
    CGFloat halfOfSuperviewWidth = self.superview.bounds.size.width/2.0f; 

    // Stop Person from going off left screen edge 
    if ((location.x - halfOfPersonWidth) <= 0.0f){ 
     return; 
    } // Stop Person from going off right screen edge 
    else if ((location.x + halfOfPersonWidth) >= self.superview.bounds.size.width){ 
     return; 
    } 

    if (self.center.x < halfOfSuperviewWidth) { 
     // Person is on the left of the screen and should not move to right side 
     if ((location.x + halfOfPersonWidth) > halfOfSuperviewWidth) { 
      return; 
     } 
    } else{ 
     // Person is on the right of the screen and should not move to left side 
     if ((location.x - halfOfPersonWidth) < halfOfSuperviewWidth) { 
      return; 
     } 
    } 
    // If we have made it this far then we can move the Person 
    // move to touch location on x axis only 
    [self setCenter:CGPointMake(location.x, self.center.y)]; 
    } 

现在,在您的视图控制器类,你可以删除原始-touchesBegan您在此处粘贴& -touchesMoved代码,或者只是把它注释掉,如果你要谨慎

/* put slash asterisks above the code and asterisks slash below the code to comment out */ 

如果你建立和运行,你应该能够走动每个人查看过,但如果你把一个手指上的每个人观点的同时,你应该能够将它们同时移动。

+0

我是一个初学者,对不起,如果我听起来很有经验,但你介意给我看一个如何看起来代码明智的例子吗?非常感谢你的输入 – user3795419 2014-09-29 00:36:10

+0

完全没问题,给我5分钟 – 2014-09-29 02:38:11

+0

如果你能接受我的答案 - 我需要增加我的代表,所以我可以发表评论。 – 2014-09-29 08:14:07