【问题标题】:Trigger an event when a UIButton is dragged to a certain point of the screen? Objective C当 UIButton 被拖动到屏幕的某个点时触发事件?目标 C
【发布时间】:2013-03-27 20:10:04
【问题描述】:

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

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

【问题讨论】:

  • 只需将您的 control.center 点与您想要在超级视图或子视图上的任何位置进行比较。然后用[self functionName];执行一些函数

标签: iphone ios objective-c xcode drag-and-drop


【解决方案1】:
- (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.         
   }
}

【讨论】:

  • CGPoint touchPoint = [(UIButton *)sender locationInView:self.view];对我不起作用。我是不是做错了什么。
  • 谢谢 :) 完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
相关资源
最近更新 更多