简体   繁体   English

如何拖放UIButton

[英]How to drag and drop a UIButton

I have four UIButtons in my interface file. 我的界面文件中有四个UIButton。 I have outlets attached to each button. 我在每个按钮上都有插座。 I want to be able to drag and drop them across the screen. 我希望能够在屏幕上拖放它们。 What is the best way to do this? 做这个的最好方式是什么? How can I do this? 我怎样才能做到这一点? please help!!! 请帮忙!!!

There are 3 possibilities nowadays on how to enable items – that is UIViews and UIControls – to be draggable. 如今,关于如何使项目(即UIViews和UIControls)可拖动的可能性有3种。

  • override touchesMoved 覆盖touchesMoved
  • add a target/selector for dragging control events 添加用于拖动控制事件的目标/选择器
  • add a pan gesture recognizer 添加平移手势识别器

The complete tutorial found here !!! 完整的教程在这里找到!

try this u can drag and drop your button what ever you want 尝试此操作,您可以随意拖放按钮

implement this code in ViewDidLoad ViewDidLoad实现此代码

 [self.shoeimageOutlet addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [self.shoeimageOutlet addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragOutside];

it will call the method written below 它将调用下面编写的方法

- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM