任何人都可以给我一个关于触摸拖动输入以从一个按钮拖动到另一个触发两个按钮事件的示例.
它是如何工作的?
它是如何工作的?
例如,我想从Do到Fa拖动Do,Re,Mi,Fa的事件被触发.@H_403_4@
@H_403_4@
- (void) setupVC { soundBankPlayer = [[SoundBankPlayer alloc] init]; [soundBankPlayer setSoundBank:@"Piano"]; arrMusicalNotes = [NSArray arrayWithObjects:@"60",@"62",@"64",@"65",@"67",@"69",@"71",@"72",nil]; } #pragma mark - Setup Musical Note - (IBAction)btnMusicalNoteclick:(id)sender { int numOfNote = [[arrMusicalNotes objectAtIndex:((UIButton*)sender).tag] intValue]; NSLog(@"%i",numOfNote); [soundBankPlayer queueNote:numOfNote gain:1.0f]; [soundBankPlayer playQueuedNotes]; } - (IBAction)btnDragOut:(id)sender { NSLog(@"Out"); }
哦,我已经看到,当我按住Simulator时,会触发方法btnDragOut.当我从Simulator拖出按钮时,会触发此按钮的方法.
现在我希望当我拖出一个按钮(手指仍在模拟器中)时触发方法btnDragOut.有谁知道吗?@H_403_4@
解决方法
您可以通过Storyboard或viewDidLoad方法中的代码将UIPanGestureRecognizer添加到您的UIViewController子类视图中:
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)]; [self.view addGestureRecognizer:gestureRecognizer];
然后你可以在被拖动的当前UIButton的UIViewController子类的实现文件中添加属性:@H_403_4@
@interface YourViewController () @property (weak,nonatomic) UIButton *currentButton; @end
现在在action方法中,您可以检测UIControlEventTouchDragEnter和UIControlEventTouchDragExit事件,如下所示:@H_403_4@
- (void)handleDrag:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint point = [gestureRecognizer locationInView:self.view]; UIView *draggedView = [self.view hitTest:point withEvent:nil]; if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { if ([draggedView isKindOfClass:[UIButton class]] && !self.currentButton) { self.currentButton = (UIButton *)draggedView; NSLog(@"Enter: %ld",(long)self.currentButton.tag); // send enter event to your button [self.currentButton sendActionsForControlEvents:UIControlEventTouchDragEnter]; } if (self.currentButton && ![self.currentButton isEqual:draggedView]) { NSLog(@"Out: %ld",(long)self.currentButton.tag); // send exit event to your button [self.currentButton sendActionsForControlEvents:UIControlEventTouchDragExit]; self.currentButton = nil; } } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { self.currentButton = nil; } }