嘿伙计们,我想知道是否有办法让像 UIImageView 这样的对象可以拖动。我知道 UITouch 和触摸,即
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *drag=[[event allTouches] anyObject];
player.center=[drag locationInView:self.view];
}
但通过这种方式,用户可以使对象跳过屏幕到达他或她触摸屏幕的位置,但我希望用户必须手动将对象拖过屏幕。
请用代码解释,让我知道是否需要更具体或更清楚...
最佳答案
你可以这样做:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];
if(CGRectContainsPoint(self.playerView.frame,location)) {
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
self.playerView.center = location;
[UIView commitAnimations];
}
}
这应该会给你一个流畅的动画。
关于ios - 如何使对象可拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693224/