【发布时间】:2013-12-15 21:14:25
【问题描述】:
我是触摸和拖动的新手,我正在尝试制作八个可拖动的图像。这是我的 ViewController.m 文件中的样子:
-(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 教程,“拖动多个图像”),他有完全相同类型的代码,而我的代码不起作用。谁能指出我为什么?
谢谢。
【问题讨论】:
标签: objective-c cocoa-touch uiimageview touchesbegan touchesmoved