2014-09-28 26 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