Obj-C typedef 枚举中制定了一个 Swift 枚举声明,但现在我不知道如何在 Swift 中实现它。我看到了一个功能,但我不知道我会用什么来设置空闲/唤醒这两种情况?如何标记暂停和恢复检测空闲计时器已禁用?
// 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 文档中有更多信息:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html
更新
似乎您只是在询问如何设置UIApplication
sharedApplication
单例的idleTimerDisabled
属性。你这样做是这样的:
UIApplication.sharedApplication().idleTimerDisabled = false