之前我們已經學習了觸控處理和手勢識別,其實這兩個同屬於iOS事件的觸控事件,今天我們來學習下iOS事件的另外兩個事件: ###一、運動事件 運動事件,是通過加速器進行觸發,和觸控事件一樣,繼承UIResponder類的物件才能處理運動事件 ######UIResponder處理運動事件的方法:
#pragma mark 運動開始時執行
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
#pragma mark 運動結束後執行
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
#pragma mark 運動被意外取消時執行
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
複製程式碼
你沒有看錯,這裡說的運動事件,只是搖晃一下手機而已,所以只有運動開始、運動結束、運動取消,無法取得運動過程中的運動速度、運動方向等資料,這些需要另外的框架去實現,我們可以理解這裡的運動時間為 “擺動事件” 。 ######監聽運動事件前提:
- 監聽物件必須成為第一響應者,控制元件需要
- (BOOL)canBecomeFirstResponder
方法返回YES
- 在檢視控制器的
- (void)viewWillAppear:(BOOL)animated
方法中呼叫運動控制元件的becomeFirstResponder
方法,使控制元件顯示時成為第一響應者 - 在檢視控制器的
- (void)viewDidDisappear:(BOOL)animated
方法中呼叫運動控制元件的resignFirstResponder
方法,使控制元件不顯示時登出控制元件的第一響應者身份
####例項: ######KCImageView.m
#import "KCImageView.h"
#define kImageCount 3
@implementation KCImageView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.image = [self getImage];
}
return self;
}
#pragma mark 設定控制元件可以成為第一響應者
- (BOOL)canBecomeFirstResponder{
return YES;
}
#pragma mark 運動開始
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//這裡只處理搖晃事件
if (motion == UIEventSubtypeMotionShake) {
self.image = [self getImage];
}
}
#pragma mark 運動結束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
}
#pragma mark 隨機取得圖片
- (UIImage *)getImage{
int index = arc4random() % kImageCount;
NSString *imageName = [NSString stringWithFormat:@"avatar%i.png",index];
UIImage *image = [UIImage imageNamed:imageName];
return image;
}
@end
複製程式碼
######KCShakeViewController.m
#import "KCShakeViewController.h"
#import "KCImageView.h"
@interface KCShakeViewController (){
KCImageView *_imageView;
}
@end
@implementation KCShakeViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark 檢視顯示時讓控制元件變成第一響應者
- (void)viewDidAppear:(BOOL)animated{
_imageView = [[KCImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
_imageView.userInteractionEnabled = true;
[self.view addSubview:_imageView];
[_imageView becomeFirstResponder];
}
#pragma mark 檢視不顯示時登出控制元件第一響應者的身份
- (void)viewDidDisappear:(BOOL)animated{
[_imageView resignFirstResponder];
}
@end
複製程式碼
###二、遠端控制事件
iOS遠端控制事件,是通過其他遠端裝置觸發的(比如耳機控制按鈕),iOS遠端控制事件相關的只有-(void)remoteControlReceivedWithEvent:(UIEvent *)event
#####監聽遠端控制事件的前提:
- 啟動遠端事件接收,呼叫
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- UI控制元件同樣要求必須成為第一響應者【使用參考運動事件】 但如果是檢視控制器或UIApplication,就沒有要求成為第一響應者
- 應用程式必須是 當前音訊額控制者
######目前iOS7給我們的遠端控制許可權僅限於音訊控制
typedef NS_ENUM(NSInteger, UIEventSubtype) {
// 不包含任何子事件型別
UIEventSubtypeNone = 0,
// 搖晃事件(從iOS3.0開始支援此事件)
UIEventSubtypeMotionShake = 1,
//遠端控制子事件型別(從iOS4.0開始支援遠端控制事件)
//播放事件【操作:停止狀態下,按耳機線控中間按鈕一下】
UIEventSubtypeRemoteControlPlay = 100,
//暫停事件
UIEventSubtypeRemoteControlPause = 101,
//停止事件
UIEventSubtypeRemoteControlStop = 102,
//播放或暫停切換【操作:播放或暫停狀態下,按耳機線控中間按鈕一下】
UIEventSubtypeRemoteControlTogglePlayPause = 103,
//下一曲【操作:按耳機線控中間按鈕兩下】
UIEventSubtypeRemoteControlNextTrack = 104,
//上一曲【操作:按耳機線控中間按鈕三下】
UIEventSubtypeRemoteControlPreviousTrack = 105,
//快退開始【操作:按耳機線控中間按鈕三下不要鬆開】
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
//快退停止【操作:按耳機線控中間按鈕三下到了快退的位置鬆開】
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
//快進開始【操作:按耳機線控中間按鈕兩下不要鬆開】
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
//快進停止【操作:按耳機線控中間按鈕兩下到了快進的位置鬆開】
UIEventSubtypeRemoteControlEndSeekingForward = 109,
};
複製程式碼
#####例項:
#import "ViewController.h"
@interface ViewController (){
UIButton *_playButton;
BOOL _isPlaying;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self initLayout];
}
- (BOOL)canBecomeFirstResponder{
return NO;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSURL *url = [NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"];
_player = [[AVPlayer alloc] initWithURL:url];
}
#pragma mark 遠端控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
if(event.type == UIEventTypeRemoteControl){
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[_player play];
_isPlaying = true;
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
[self btnClick:_playButton];
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"Next...");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"Previous...");
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:
NSLog(@"Begin seek forward...");
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
NSLog(@"End seek forward...");
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
NSLog(@"Begin seek backward...");
break;
case UIEventSubtypeRemoteControlEndSeekingBackward:
NSLog(@"End seek backward...");
break;
default:
break;
}
[self changeUIState];
}
}
#pragma mark 介面佈局
- (void)initLayout{
//專輯封面
UIImage *image = [UIImage imageNamed:@"wxl.jpg"];
CGRect *frame = [UIScreen mainScreen].applicationFrame;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = image;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
//播放控制皮膚
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 88)];
view.backgroundColor = [UIColor lightGrayColor];
view.alpha = 0.9;
[self.view addSubview:view];
//新增播放按鈕
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
_playButton.bounds = CGRectMake(0, 0, 50, 50);
CGFloat playBtnX = view.frame.size.width/2;
CGFloat playBtnY = view.frame.size.height/2;
_playButton.center = CGPointMake(playBtnX, playBtnY);
[self changeUIState];
[_playButton addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:_playButton];
}
#pragma mark 介面狀態
- (void)changeUIState{
if(_isPlaying){
UIImage *pauseImage = [UIImage imageNamed:@"playing_btn_pause_n.png"];
UIImage *pauseImageH = [UIImage imageNamed:@"playing_btn_pause_h.png"];
[_playButton setImage:pauseImage forState:UIControlStateNormal];
[_playButton setImage:pauseImageH forState:UIControlStateHighlighted];
}else{
UIImage *playImage = [UIImage imageNamed:@"playing_btn_play_n.png"];
UIImage *playImageH = [UIImage imageNamed:@"playing_btn_play_h.png"];
[_playButton setImage:playImage forState:UIControlStateNormal];
[_playButton setImage:playImageH forState:UIControlStateHighlighted];
}
}
- (void)btnClick:(UIButton *)btn{
if (_isPlaying) {
[_player pause];
}else{
[_player play];
}
_isPlaying =! _isPlaying;
[self changeUIState];
}
@end
複製程式碼
這次筆記貼了很多程式碼,是因為這兩個事件使用簡單,理論知識不多,光講理論,也不好理解,貼程式碼非常直觀。