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
场景与背景图像和许多车辆
,您可以将每个新车辆定义为 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屋!