iOS中的基本拖放 [英] Basic Drag and Drop in iOS
问题描述
我想要一个视图,其中有车辆在周围行驶,用户也可以拖放.你认为最好的大规模战略是什么?最好是从代表车辆的视图中还是从更大的视图中获取触摸事件?是否有您满意的用于拖放的简单范例?不同策略的缺点是什么?
I want to have a view in which there are vehicles driving around that the user can also drag and drop. What do you think is the best large-scale strategy for doing this? Is it best to get touch events from the views representing the vehicles, or from the larger view? Is there a simple paradigm you've used for drag and drop that you're satisfied with? What are the drawbacks of different strategies?
推荐答案
假设你有一个 UIView
场景,其中有一张背景图片和许多 vehicles
,你可以定义每个新的车辆作为 UIButton
(UIImageView 可能也可以工作):
Assume you have a UIView
scene with a background image and many vehicles
, you may define each new vehicle as a UIButton
(UIImageView will probably work too):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button setImage:[UIImage imageNamed:@"vehicle.png"] forState:UIControlStateNormal];
[self.view addSubview:button];
然后您可以通过响应 UIControlEventTouchDragInside
事件将车辆移动到任何您想要的位置,例如:
Then you may move the vehicle wherever you want, by responding to the UIControlEventTouchDragInside
event, e.g.:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
与管理整个场景相比,单个车辆处理自己的阻力要容易得多.
It's a lot easier for individual vehicle to handle its own drags, comparing to manage the scene as a whole.
这篇关于iOS中的基本拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!