uiview - 如何使 UIbutton 在 UIView 中可拖动?

标签 uiview drag-and-drop uibutton

我需要在仪表板中拖放按钮我可以使用以下代码拖放按钮

- (void)viewDidLoad {
      [super viewDidLoad];
     [eventButton addTarget:self action:@selector(draggedOut:withEvent:)forControlEvents:UIControlEventTouchDragOutside |UIControlEventTouchDragInside];
  }

- (void) draggedOut: (UIControl *) c withEvent: (UIEvent *) ev {
       CGPoint point = [[[ev allTouches] anyObject] locationInView:self.view];
    if(point.y > 22 && point.y <300)
            c.center = CGPointMake(237, point.y - c.bounds.size.height/2 -5);
  }

但我需要在拖放时我也需要更改按钮的位置

注意:我只为一个按钮编写了代码

请帮我解答我的问题

最佳答案

- (void)viewDidLoad {
  [super viewDidLoad];
  [eventButton addTarget:self action:@selector(dragOut:withEvent:)forControlEvents:UIControlEventTouchDragOutside |UIControlEventTouchDragInside];
   }

-(IBAction)dragOut: (id)sender withEvent: (UIEvent *) event {
 UIButton *selected = (UIButton *)sender;
 selected.center = [[[event allTouches] anyObject] locationInView:self.view];
 }

关于uiview - 如何使 UIbutton 在 UIView 中可拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4416472/

相关文章:

ios - UIView subview alpha 动画不起作用

ios - 在行的按钮附近添加一个 UIView?

c# - 如何在 C# WPF 中通过拖放交换两个标签的数据?

ios - 重置导航栏

ios - 无法将 UIButton 向下转换为子类

ios - UIButton 背景图像水平缩放以填充,同时保持纵横比

ios - UITableView 中 UIView 的触摸事件

ios - 当 View 已经被按下时如何响应触摸?

c# - 将文件从 ListView 拖放到 Windows 资源管理器?

android - 在我的自定义启动器应用程序中使用 Android 拖放框架