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