2010-12-11 63 views
5

我需要拖动和我的仪表板将按钮拖放我可以拖动和使用下面的代码如何使UIbutton在UIView内拖动?

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

下降的按钮,但我需要同时拖放我需要更改按钮的位置过于

注:我写的代码只有一个按钮

请帮我要我的问题

回答

4
- (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]; 
} 
0

使用UIControlEventTouchDragIn的UIButton的侧面和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控制..