开发者

how to make UIbutton draggable within UIView?

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];
 }


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜