我是全新的觸摸和拖動,我試圖製作八個可拖動的圖像。下面是它看起來像在我ViewController.m文件:在iOS中拖動多個圖像
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
//check for which peg is touched
if ([touch view] == darkGreyPeg){darkGreyPeg.center = location;}
else if ([touch view] == brownPeg){brownPeg.center = location;}
//there are six more else ifs
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self ifCollided];
}
如果我拿出if語句,以及locationInView:self.view到locationInView:touch.view,我能夠左右拖動darkGreyPeg沒有問題,並在適當的時候觸發我的[ifCollided]。
我正在看一個youTube教程(Milmers Xcode教程,「拖動多個圖像」),他有完全相同類型的代碼,而我的工作不起作用。任何人都可以向我指出爲什麼?
謝謝。
你能解釋一下嗎?我不是個聰明人。 – novalsi