【问题标题】:how to make UIbutton draggable within UIView?如何在 UIView 中使 UIbutton 可拖动?
【发布时间】:2010-12-11 11:25:20
【问题描述】:

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

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

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

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

请帮我解答我的问题

【问题讨论】:

    标签: uiview drag-and-drop uibutton


    【解决方案1】:
    - (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];
     }
    

    【讨论】:

      【解决方案2】:

      使用 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控制..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-24
        • 2017-06-12
        • 2013-01-18
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 2011-04-20
        • 2017-09-28
        相关资源
        最近更新 更多