我需要在仪表板中拖放按钮我可以使用以下代码拖放按钮
- (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);
}
但我需要在拖放时我也需要更改按钮的位置
注意:我只为一个按钮编写了代码
请帮我解答我的问题
最佳答案
- (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 在 UIView 中可拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4416472/