iPhone SDK:麻烦拖动一个UIImageView
-
19-09-2019 - |
题
我试图拖动iPhone屏幕一个UIImageView在我的应用程序。
目前我已经设置了拖动功能被罚款和拖动图像不动它在屏幕上,麻烦的是,你不必拖动图像视图来移动它,你也可以在任何地方在屏幕上拖动它会移动图像。 我是新来这个平台,让我真的不能想怎样做才能解决这个问题。 我的当前代码拖动的图像是:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:pig];
startLocation = pt;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:pig];
CGRect frame = [pig frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[pig setFrame: frame];
}
任何帮助理解。顺便说猪是的UIImageView。 另外我注意到,如果我设置图像视图“猪”的用户交互,以使图像不再拖累能干,但在未将其设置为启用它。
解决方案
您可以尝试子类的UIImageView,并直接在其上实现touchesBegan:
和touchesMoved:
。然后在InterfaceBuilder中选择您的UIImageView的子类。
替代文字http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg一>
哦,是的,现在你需要重新设置用户交互...
其他提示
您需要将它括在开始/提交至少动画序列中的计时器线程:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.02] /* 0.02 seconds suration*/
/* move pig coordinates here */
[UIView commitAnimations];
保存新的运动在touchesMoved,然后在计时器线程处理它们。
您可以使用的UIView类,使其可拖动并定义拖动区域: https://github.com/rtoshiro/TXDragAndDrop
不隶属于 StackOverflow