作個簡短的筆記,因為嘗試的程式發現流程根本有問題,
所以把相關的實作的功能作個紀錄,之後再來想流程怎麼修改。
首先是振動部分,振動很簡單,必須先import AudioToolbox.framework然後填入:
#import "AudioToolbox/AudioServices.h"
調用:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
就解決了,沒辦法調整長振動和短振動時間。
接著就是閃光的部份,我把他寫成一個method:
//閃光燈開關
-(void)flashSwitch:(BOOL)turnOn{
NSLog(@"閃光燈開關: %d",isOn);
NSArray *array=[AVCaptureDevice devices];
for(AVCaptureDevice *p in array){
if([p hasTorch]){
if([p isFlashModeSupported:AVCaptureTorchModeOn]){
if([p lockForConfiguration:nil]){
p.torchMode=turnOn;
}//lock and check lock
[p unlockForConfiguration];
}//end if LED
}//end if hastorch
}//end
這邊有些怪怪的,因為沒有作break動作,而iphone或ipad不就是只有背後才有閃光燈嗎?
然後這邊因為我想要實現按鈕按下時候開啟閃光,按鈕放開時候關閉:這裡會發現如果閃光燈開關方法直接實作在按鈕的觸發事件裡的話會讀不到,猜測應該是線程處理的問題,於是,NSNotificationCenter就登場了:
//發送通知
[[NSNotificationCenter defaultCenter]
postNotificationName:@"通知的key值"
object:self];
//註冊通知監聽
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"通知的key值"
object:nil];
//通知觸發的方法
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"通知的key值"]){
//觸發方法寫在這個裡面
}
}
這跟Android的Broadcast概念是一樣的,我把他用在按鈕回饋操作閃光燈的開關行為上,運轉正常。
這邊要注意,在註冊通知監聽那邊,有個object參數,可以作為傳值用途,輸入nil(null)就是給他空的值啦!
最後我用了一個Timer的動作,用來計時,操作的內容如下:
//開啟計時
NSTimer *timer=[ NSTimer scheduledTimerWithTimeInterval:時間間格 target:self selector:@selector(method:) userInfo:nil repeats:YES];
//結束計時
if(timer)
[timer invalidate];
//計時觸發的方法
-(void)method:(NSTimer *)theTimer{
//要作什麼事情寫在裡面就好
}
然後,這是timer 開啟計時的參數說明,懶得打所以放原文上來:
以上。secondsThe number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.targetThe object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.aSelectorThe message to send to target when the timer fires. The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method.userInfoThe user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.repeatsIf YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
沒有留言:
張貼留言
你好,我是小書,如果文章內容有錯誤,或是看到有建議以及任何感想時,歡迎提出分享,我們一起學習一起努力。