如何在UIView中使UIbutton拖动? [英] how to make UIbutton draggable within UIView?

查看:154
本文介绍了如何在UIView中使UIbutton拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拖放我的仪表板上的按钮,我可以使用以下代码拖放按钮

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

推荐答案

- (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拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆