遙知兄弟登高處纹坐,遍插茱萸少一人!<波波鴿.png>
UIEvent 事件類(lèi)型:
-
觸摸事件(UIEventTypeTouches )
- 觸摸事件包括一個(gè)或者多個(gè)觸摸(touches), 每個(gè)觸摸有一個(gè)UITouch對(duì)象表示舞丛。當(dāng)觸摸事件發(fā)生時(shí)耘子,系統(tǒng)會(huì)通過(guò)觸摸處理的邏輯找到合適的responder并把UIEvent對(duì)象傳遞過(guò)去。responder通過(guò)touchesBegan:withEvent:等方法去接收UIEvent對(duì)象球切。
-
加速計(jì)事件拴还,運(yùn)動(dòng)事件(UIEventTypeMotion )
- 晃動(dòng)設(shè)備等
-
遠(yuǎn)程控制事件 (UIEventTypeRemoteControl)
- 主要是外部輔助設(shè)備或者耳機(jī)的遠(yuǎn)程命令,例如控制音樂(lè)聲音的大小欧聘,或者下一首歌。
-
觸摸相關(guān)方法:
/**
下面的四個(gè)方法都有的參數(shù):
@param NSSet <UITouch *> *) touches 觸摸點(diǎn)集合 (根據(jù)觸摸點(diǎn)的個(gè)數(shù)可以判斷是幾個(gè)觸摸點(diǎn))
@param event 事件對(duì)象端盆,記錄事件產(chǎn)生的時(shí)刻和類(lèi)型(下面的四個(gè)方法傳入都是同一個(gè)對(duì)象)
*/
// 開(kāi)始觸摸
// 手指開(kāi)始觸摸view,系統(tǒng)會(huì)自動(dòng)調(diào)用方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
// 手指在view上移動(dòng)怀骤,系統(tǒng)會(huì)自動(dòng)調(diào)用(隨著手指的移動(dòng),會(huì)持續(xù)調(diào)用該方法)
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 一手指離開(kāi)view焕妙,系統(tǒng)會(huì)自動(dòng)調(diào)用方法
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸結(jié)束前蒋伦,有可能系統(tǒng)的事件打斷了觸摸的響應(yīng),會(huì)調(diào)用的方法焚鹊。(系統(tǒng)方法例如:電話(huà)來(lái)了)
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
UITouch 對(duì)象:
- 我們用手指觸摸屏幕的時(shí)候痕届,就會(huì)創(chuàng)建一個(gè)與觸摸點(diǎn)關(guān)聯(lián)的 UITouch 對(duì)象,每一個(gè)觸摸點(diǎn)對(duì)應(yīng)一個(gè)UITouch 實(shí)例對(duì)象
- 這個(gè)對(duì)象保存了與觸摸相關(guān)的信息(位置末患、時(shí)間研叫、階段)
- 觸摸點(diǎn)改變(手指移動(dòng)),系統(tǒng)對(duì)應(yīng)更新 UITouch 對(duì)象璧针,適時(shí)地記錄觸摸點(diǎn)信息嚷炉。
- 觸摸結(jié)束時(shí)候,UITouch 對(duì)象被銷(xiāo)毀
UITouch 方法:
- 返回當(dāng)前觸摸點(diǎn)在 View 上面的位置探橱,傳入 View 為 nil 時(shí)候位置是在 UIWindow申屹。
- (CGPoint)locationInView:(UIView *)view;
- 返回前一個(gè)觸摸點(diǎn)在 View 上的位置。
- (CGPoint)previousLocationInView:(UIView *)view;
-
加速相關(guān)方法:
// 開(kāi)始搖晃 加速計(jì)事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
}
// 搖晃結(jié)束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
}
// 搖晃取消
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
}```
##### 小例子:[搖一搖簡(jiǎn)單實(shí)現(xiàn)GitHub](https://github.com/YunZhiJunXiPeng/HotTool.git)
- 準(zhǔn)備:
>- 先在targets -> Build Phases -> Link Binary With Libraries里面添加AudioToolbox.framework隧膏;
- >AudioservicesPlaySystemSound函數(shù)來(lái)播放簡(jiǎn)單的聲音
1.音頻長(zhǎng)度小于30秒
2.格式只能是PCM或者IMA4
3.文件必須被存儲(chǔ)為.caf哗讥、.aif、或者.wav格式
4.簡(jiǎn)單音頻不能從內(nèi)存播放胞枕,而只能是磁盤(pán)文件
- 注意局限性: 無(wú)法循環(huán)播放聲音杆煞,也無(wú)法控制立體聲效果。可以設(shè)置一個(gè)回調(diào)函數(shù)索绪,在音頻播放結(jié)束時(shí)被調(diào)用湖员,可以對(duì)音頻對(duì)象做清理工作,以及通知你的程序播放結(jié)束瑞驱。
- 在模擬器中運(yùn)行時(shí)娘摔,可以通過(guò)「Hardware」-「Shake Gesture」來(lái)測(cè)試「搖一搖」功能
-------
- 導(dǎo)入音頻的頭文件
```code
#import <AudioToolbox/AudioToolbox.h>```
- 定義一個(gè)回調(diào)函數(shù),就是我們搖一搖的音效播放完回調(diào)方法函數(shù)
```code
// 當(dāng)音頻播放完畢會(huì)調(diào)用這個(gè)函數(shù)
// 在完成處理程序標(biāo)簽存儲(chǔ)在 data 唤反。需要到 __bridge 凳寺。
static void SoundFinished(SystemSoundID soundID, void *data){
/*播放全部結(jié)束,因此釋放所有資源 */
AudioServicesDisposeSystemSoundID(soundID);
NSLog(@"%s--->%@",__func__,(__bridge UIViewController *)data);
}
- 設(shè)置允許搖一搖功能
// 允許搖一搖功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
// 設(shè)置第一響應(yīng)者
[self becomeFirstResponder];```
- 開(kāi)始搖晃的時(shí)候我們播放音效
```code
// 注冊(cè)系統(tǒng)音頻ID
SystemSoundID soundID;
// 獲取聲音的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"59" ofType:@"m4a"];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// 根據(jù)路徑創(chuàng)建
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundUrl, &soundID);
/*
*參數(shù)說(shuō)明:
* 1彤侍、剛剛播放完成自定義系統(tǒng)聲音的ID
* 2肠缨、回調(diào)函數(shù)(playFinished)執(zhí)行的run Loop,NULL表示main run loop
* 3盏阶、回調(diào)函數(shù)執(zhí)行所在run loop的模式晒奕,NULL表示默認(rèn)的run loop mode
* 4、需要回調(diào)的函數(shù)
* 5名斟、傳入的參數(shù)脑慧, 此參數(shù)會(huì)被傳入回調(diào)函數(shù)里
*/
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, SoundFinished, (__bridge void *)self);
// 開(kāi)始播放
AudioServicesPlaySystemSound (soundID);
return;
}```
```code
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"取消搖動(dòng)");
return;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) {
// 判斷是否是搖動(dòng)結(jié)束
NSLog(@"搖動(dòng)結(jié)束");
}
return;
}
遠(yuǎn)程控制相關(guān):
// 根據(jù)傳進(jìn)來(lái)的 event 進(jìn)行判斷,并進(jìn)行相應(yīng)操作即可砰盐。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{}```