0

当可拖动按钮被拖动到屏幕上的某个点时,我将如何触发事件?

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;
}
4

1 回答 1

4
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event{

  CGPoint touchPoint = [[[event allTouches] anyObject] locationInView:self.view];
  UIControl *control = sender;
  control.center = touchPoint;

  CGRect rectToCompare =  location you want on your super view;

   if (CGRectContainsPoint(rectToCompare, touchPoint)) {

         //trigger an event.         
   }
}
于 2013-03-27T20:23:11.397 回答