如何使 UIbutton 在 UIView 中可拖动?

发布于 2024-10-07 18:15:30 字数 612 浏览 0 评论 0原文

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

- (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);
  }

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

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

请帮我解答我的问题

I need to drag and drop the button in my dash board i can drag and drop the button using the following code

- (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);
  }

but i need while drag and drop i need to change the positions of the buttons too

note:i wrote the code only for one button

please help me for my question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2024-10-14 18:15:31

使用 UIButton 的 UIControlEventTouchDragInside 和 UIControlEventTouchUpInside 事件,例如

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

您可以使用 hbdraggablebutton 控件..

Use UIControlEventTouchDragInside and UIControlEventTouchUpInside events of UIButton like

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

You can use hbdraggablebutton control..

烂柯人 2024-10-14 18:15:30
- (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];
 }
- (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];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文