拖放而不删除 UIButton
我正在 iPhone 应用程序中实现拖放系统。到目前为止,我成功实现了可拖动按钮,并且工作正常。这是我的代码:
首先,我将操作分配给按钮:
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
然后,我创建拖动:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
NSLog(@"moved");
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
但是,我想要实现的是被拖动的按钮不会从原来的位置删除。所以我想到了一个解决方案,即创建一个与第一个类似的新按钮,然后移动这个新创建的按钮。但我在实现最后一部分时遇到了麻烦。 有什么帮助吗?非常感谢!
I'm implementing a drag and drop system in an iphone app. So far I managed to implement the draggable button and it is working fine. This is the code that I have:
First, I assign the action to the button:
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
then, I create the drag:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
NSLog(@"moved");
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
However, what I want to achieve is that the button being dragged doesn't gets removed from where it is. So I thought of one solution that is to create a new button similar to the first and move the this newly created button. But I having trouble in implementing this last part.
Any help? Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要消除限制,您可以在原始按钮上添加两个事件,如下所示:
这样,当您退出当前视图(您的按钮)的边界时,该事件也会触发,并且您将能够将新按钮移动到周围超级视图。
To remove the restriction you can add two events on the origin button like so:
This way the event will fire too when you exit the bounds of the current view (your button) and you'll be able to move the new button all arround the superview.