我需要将按钮拖放到仪表板上我可以使用以下代码拖放按钮
- (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);
}
但我需要拖放时我也需要改变按钮的位置
注意:我只为一个按钮编写了代码
请帮助我解决我的问题
答案 0 :(得分: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];
}
答案 1 :(得分:0)
使用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控件..