首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为iPhone实现卷键快门?

如何为iPhone实现卷键快门?
EN

Stack Overflow用户
提问于 2011-12-06 08:41:12
回答 5查看 12.9K关注 0票数 20

我想用iOS5的本机摄像机实现同样的行为

  • 按音量+按钮拍摄照片

,归档它的理想方法是什么?是否有任何方法来捕获按下的卷键事件?

在搜索了几个小时之后,我找到了一个解决方案:使用NSNotificationCenter

代码语言:javascript
运行
复制
...
    [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(volumeChanged:)
         name:@"AVSystemController_SystemVolumeDidChangeNotification"
         object:nil];
...
- (void)volumeChanged:(NSNotification *)notification{
    [self takePhoto];   
}

然而,它有两个问题:

  • 有一个半透明的“当前系统卷”覆盖,每次按音量键时都会显示出来,这不是我想要的。
  • 用于本机相机,当您按下音量键作为快门时,系统音量不会改变,但是,通过使用上述方法,系统音量将发生变化。
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-12-06 16:42:00

我已经找到了另一种方法来隐藏“系统卷覆盖”和“绕过系统卷更改时按下卷键”。

最糟糕的是:这是一个超级丑陋的黑客。

然而,的好处是:这个丑陋的黑客不使用私有API.

另一个注意事项是:它只适用于ios5+ (无论如何,对于我的问题,因为ios5+只适用于ios5,所以这个丑陋的攻击正好适合我的问题)。

它的工作方式:“扮演一个音乐/电影播放器应用程序,让音量键来调整应用-音量”。

代码:

代码语言:javascript
运行
复制
// these 4 lines of code tell the system that "this app needs to play sound/music"
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"photo-shutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];
[p release];

// these 5 lines of code tell the system that "this window has an volume view inside it, so there is no need to show a system overlay"
[[self.view viewWithTag:54870149] removeFromSuperview];
MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 100, 100)];
[self.view addSubview:vv];
vv.tag = 54870149;
[vv release];

(花了5个小时来发现这个超丑的方法.妈的..。草尼马啊!)

另一件事:如果你采取上述黑客,你需要每次运行代码时,你的应用程序变得活跃。因此,您可能需要在应用程序委托中添加一些代码。

代码语言:javascript
运行
复制
- (void)applicationDidBecomeActive:(UIApplication *)application 
票数 12
EN

Stack Overflow用户

发布于 2012-05-05 10:14:38

构建在huxia代码的基础上,它可以在ios5+上运行,无需每次代码处于活动状态时运行,只需在开始时运行一次即可。

代码语言:javascript
运行
复制
// these 4 lines of code tell the system that "this app needs to play sound/music"
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"photoshutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];

//make MPVolumeView Offscreen
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
票数 12
EN

Stack Overflow用户

发布于 2012-09-10 09:32:40

代码语言:javascript
运行
复制
[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(volumeChanged:)
     name:@"AVSystemController_SystemVolumeDidChangeNotification"
     object:nil];

代码语言:javascript
运行
复制
- (void)volumeChanged:(NSNotification *)notification{ 

    CGRect frame = CGRectMake(-1000, -1000, 100, 100);
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
    [volumeView release];

    [self takePhoto];   
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8397170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档