之前我們已經學習了觸摸處理和手勢識別,其實這兩個同屬於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
遠程控制實例效果
這次筆記貼了很多代碼,是因為這兩個事件使用簡單,理論知識不多,光講理論,也不好理解,貼代碼非常直觀。