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 に答える