从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
答案 0 :(得分:1)
你没有提供你翻译的Swift枚举的样子,所以我要定义一个似乎合适的。让我们说你的枚举看起来像这样:
enum CurrentState
{
case statePaused
case stateSampling
}
如果您有CurrentState
类型的变量,例如currentState
字段,
你可以给它一个像这样的值:
currentState = CameraState.stateSampling
在Swift中,枚举被视为具有类型安全性的真实对象,因此如果将对象声明为枚举类型,则可以使用更短的点语法,如果您愿意:
currentState = .stateSampling
此处的实际Swift文档中提供了更多信息:
<强>更新强>
您似乎只是在询问如何设置idleTimerDisabled
UIApplication
单例的sharedApplication
属性。你这样做:
UIApplication.sharedApplication().idleTimerDisabled = false