2013-09-25 70 views
0

我有幾個UIImages,我希望用戶能夠單獨拖動每一個。我希望他們能夠觸摸圖像(必須觸摸圖像而不是其他地方),並將其拖動到屏幕上而不影響其他圖像。此代碼移動在同一時間兩個圖像到同一個地方,無論身在何處,用戶在觸摸屏上:如何移動或拖動多個UIImage?

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

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
[self touchesBegan:touches withEvent:event]; 
} 

if語句這樣我試着使用,並且它在完全停止的動作,沒有拖動全部:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:self.view]; 
if (touch.view == playerCardOne) { 
    playerCardOne.center = location; 
    [self.view bringSubviewToFront:(playerCardOne)]; 
} 
else if ([touch view] ==playerCardTwo) { 
playerCardTwo.center = location; 
[self.view bringSubviewToFront:(playerCardTwo)]; 
} 
} 

任何人都可以幫忙嗎?

回答

0

嘗試把這個在您的

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

    if ([touch view] == playerCardOne) 
    { 
     playerCardOne.center = location; 
    } 
    else if ([touch view] == playerCardTwo) 
    { 
     playerCardTwo.center = location; 
    } 
} 

,並採取了:

[self touchesBegan:touches withEvent:event]; 

,並確保設置userInteractionEnabled = YES;

+0

這仍然只是把圖像和移動它們的確切位置,無論我在屏幕上點擊。 –

+0

沒關係,修復它,謝謝 –

0

您可以在下面使用。我用這個在屏幕上移動多個圖像。它爲我工作。

UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan:)]; 

[smyImageView addGestureRecognizer:span]; 
smyImageView.userInteractionEnabled = YES; 


    UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan1:)]; 

    [smyImageView1 addGestureRecognizer:span1]; 

移動(潘):

- (void)onsPan:(UIPanGestureRecognizer *)recognizer { 

    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

} 

- (void)onsPan1:(UIPanGestureRecognizer *)recognizer { 

    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

}