它是如何工作的?
例如,我想从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.有谁知道吗?
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];[self.vIEw addGestureRecognizer:gestureRecognizer];
然后你可以在被拖动的当前UIbutton的UIVIEwController子类的实现文件中添加属性:
@interface YourVIEwController ()@property (weak,nonatomic) UIbutton *currentbutton;@end
现在在action方法中,您可以检测UIControlEventtouchdragenter和UIControlEventtouchDragExit事件,如下所示:
- (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; }}总结
以上是内存溢出为你收集整理的ios – Touch拖动输入如何工作?全部内容,希望文章能够帮你解决ios – Touch拖动输入如何工作?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)