需要實現如下功能:將可拖拽的按鈕拖拽到屏幕的指定位置時,自動觸發一個事件。不知道應該從哪兒入手,請高人指教,謝謝。
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setFrame:CGRectMake(10.0f, 300.0f, 300.0f, 42.0f)];
[btn1 setTitle:[NSString stringWithFormat:@"View Fullscreen Decor"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:btn1];
[super viewDidLoad];
[myScrollView addSubview:btn1];
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
//添加拖拽停止監聽
[btn1 addTarget:self action:@selector(imageMovedEnd:withEvent:) forControlEvents:UIControlEventTouchUpInside];
//拖拽停止處理
- (IBAction) imageMovedEnd:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
NSLog(@"End... %f,%f",point.x,point.y);
// do sth.. 判斷Point是否在屏幕的指定位置即可,簡單的碰撞檢測,可以自己去Google搜一下
}