【发布时间】:2011-08-03 10:10:44
【问题描述】:
我正在尝试拖动图像并在释放后让它返回到原来的位置。到目前为止,我可以通过使用以下代码将图像创建为按钮来拖动图像:(如该问题的答案所示:Basic Drag and Drop in iOS)
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];
后来我定义:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
释放后如何让它回到原来的位置?,对于初学者,我将保存每个图像应该返回的位置,但是,无论如何都要指示 UIImage 从它的当前位置移动并转移到另一个?,或任何其他替代解决方案?
【问题讨论】:
-
你在使用 AutoLayout 吗?
标签: iphone objective-c xcode drag-and-drop