作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 Obj-C typedef 枚举中制定了 Swift 枚举声明,但现在我不知道如何在 Swift 中实现它。有人向我展示了一个功能,但我不知道在这两种情况下我会使用什么来设置空闲/唤醒?如何标记idleTimerDisabled检测的暂停和恢复?
// ViewController.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> // import AVFoundation
#import "PulseDetector.h"
#import "Filter.h"
typedef NS_ENUM(NSUInteger, CURRENT_STATE) {
STATE_PAUSED,
STATE_SAMPLING
};
#define MIN_FRAMES_FOR_FILTER_TO_SETTLE 10
@interface ViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
// other code here
// !!!!!!! we're now sampling from the camera. My problem !!!!!!!!
self.currentState=STATE_SAMPLING;
// stop the app from sleeping
[UIApplication sharedApplication].idleTimerDisabled = YES;
// update our UI on a timer every 0.1 seconds
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
}
-(void) stopCameraCapture {
[self.session stopRunning];
self.session=nil;
}
#pragma mark Pause and Resume of detection
-(void) pause {
if(self.currentState==STATE_PAUSED) return;
// switch off the torch
if([self.camera isTorchModeSupported:AVCaptureTorchModeOn]) {
[self.camera lockForConfiguration:nil];
self.camera.torchMode=AVCaptureTorchModeOff;
[self.camera unlockForConfiguration];
}
self.currentState=STATE_PAUSED;
// let the application go to sleep if the phone is idle
[UIApplication sharedApplication].idleTimerDisabled = NO;
}
-(void) resume {
if(self.currentState!=STATE_PAUSED) return;
// switch on the torch
if([self.camera isTorchModeSupported:AVCaptureTorchModeOn]) {
[self.camera lockForConfiguration:nil];
self.camera.torchMode=AVCaptureTorchModeOn;
[self.camera unlockForConfiguration];
}
self.currentState=STATE_SAMPLING;
// stop the app from sleeping
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
快速翻译
// we're now sampling from the camera
enum CurrentState {
case statePaused
case stateSampling
}
var currentState = CurrentState.stateSampling
func setState(state: CurrentState){
switch state
{
case .statePaused:
// what goes here? Something like this?
UIApplication sharedApplication.idleTimerDisabled = false
case .stateSampling:
// what goes here? Something like this?
UIApplication sharedApplication.idleTimerDisabled = true
}
}
// what goes here? Something like this?
currentState = CurrentState.stateSampling
最佳答案
您没有提供翻译后的 Swift 枚举,因此我将定义一个看起来合适的枚举。假设您的枚举如下所示:
enum CurrentState
{
case statePaused
case stateSampling
}
如果您有 CurrentState
类型的变量,例如 currentState
字段,
你可以给它一个像这样的值:
currentState = CameraState.stateSampling
在 Swift 中,枚举被视为具有类型安全的真实对象,因此,如果将对象声明为枚举类型,您可以根据需要使用更短的点语法,如下所示:
currentState = .stateSampling
这里的实际 Swift 文档中有更多信息:
更新
您似乎只是询问如何设置 UIApplication
sharedApplication
单例的 idleTimerDisabled
属性。你这样做:
UIApplication.sharedApplication().idleTimerDisabled = false
关于objective-c - typedef 枚举到 Swift 枚举。如何使用案例来标记idleTimerDisabled检测的Pause和Resume?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787244/
我是一名优秀的程序员,十分优秀!