任何人都可以给我一个关于触摸拖动输入的示例,从按钮拖动到另一个按钮触发两个按钮的事件。 它是如何工作的?
例如,我想从Do到Fa拖动Do,Re,Mi,Fa的事件被触发。
这是我的代码:
- (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。有人知道吗?
答案 0 :(得分:4)
您可以通过故事板或UIPanGestureRecognizer
方法中的代码将UIViewController
添加到viewDidLoad
子类的视图中:
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[self.view addGestureRecognizer:gestureRecognizer];
然后,您可以在被拖动的当前UIViewController
的{{1}}子类的实现文件中添加属性:
UIButton
现在,在操作方法中,您可以按如下方式检测@interface YourViewController ()
@property (weak, nonatomic) UIButton *currentButton;
@end
和UIControlEventTouchDragEnter
事件:
UIControlEventTouchDragExit