【发布时间】:2016-08-31 15:47:38
【问题描述】:
我一直像疯子一样四处张望。我需要对音量按钮长按和短按执行自定义操作(如 snapchat、短按拍照、长按录制视频)但失败了。
我用过https://github.com/jpsim/JPSVolumeButtonHandler 这使我能够检测到音量按钮按下事件。但经过非常努力后,我未能检测到什么时候是长按,什么时候是短按。
也可以参考Detect iPhone Volume Button Hold? (iOS 8)
我的代码
//Volume button handler
self.volumeButtonHandler = [JPSVolumeButtonHandler volumeButtonHandlerWithUpBlock:^{
// Volume Up Button Pressed
[self onVolumeUp];
} downBlock:^{
// Volume Down Button Pressed
}];
-(void)onVolumeUp {
if(self.volButtonTimer) {
secondsElapsed = 1; //To detect long press
[self.volButtonTimer invalidate];
}
self.volButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(onTimerFire)
userInfo:nil
repeats:NO];
}
-(void)onTimerFire {
NSLog(@"Long Press %d",secondsElapsed);
if (secondsElapsed > 1)
{
//Do Something
}
else secondsElapsed = 0;
}
我需要这方面的帮助。在谷歌上搜索但失败了。如果 snapchat 正在这样做,则意味着它是可行的。
谢谢
【问题讨论】:
标签: ios objective-c iphone long-press mpvolumeview