iOS开发UIEvent事件简介
1、UIEvent简介
UIEvent是代表iOS系统中的一个事件,一个事件包含一个或多个的UITouch;
UIEvent分为四类: UIEventType
typedef NS_ENUM(NSInteger, UIEventType) {
UIEventTypeTouches,//触摸事件类型 iOS3.0之后可以用
UIEventTypeMotion,//摇晃事件类型 iOS3.0之后可以用
UIEventTypeRemoteControl,//遥控事件类型 iOS4.0之后可以用
UIEventTypePresses NS_ENUM_AVAILABLE_IOS(9_0),//物理按钮事件类型 iOS9.0之后可以用
};
子事件类型:UIEventSubtype
typedef NS_ENUM(NSInteger, UIEventSubtype) {
//事件没有子类型 iOS3.0之后可以用
UIEventSubtypeNone = ,
//事件子类型晃动的设备 iOS3.0之后可以用
UIEventSubtypeMotionShake = ,
//遥控的事件子类型 iOS4.0之后可以用
UIEventSubtypeRemoteControlPlay = ,//播放
UIEventSubtypeRemoteControlPause = ,//暂停
UIEventSubtypeRemoteControlStop = ,//停止
UIEventSubtypeRemoteControlTogglePlayPause = ,//播放和暂停之间切换【操作:播放或暂停状态下,按耳机线控中间按钮一下】
UIEventSubtypeRemoteControlNextTrack = ,//下一曲【操作:按耳机线控中间按钮两下】
UIEventSubtypeRemoteControlPreviousTrack = ,//上一曲【操作:按耳机线控中间按钮三下】
UIEventSubtypeRemoteControlBeginSeekingBackward = ,//快退开始【操作:按耳机线控中间按钮三下不要松开】
UIEventSubtypeRemoteControlEndSeekingBackward = ,//快退结束【操作:按耳机线控中间按钮三下到了快退的位置松开】
UIEventSubtypeRemoteControlBeginSeekingForward = ,//快进开始【操作:按耳机线控中间按钮两下不要松开】
UIEventSubtypeRemoteControlEndSeekingForward = ,//快进结束【操作:按耳机线控中间按钮两下到了快进的位置松开】
};
2、相关API
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIEvent : NSObject @property(nonatomic,readonly) UIEventType type NS_AVAILABLE_IOS(3_0);//事件类型
@property(nonatomic,readonly) UIEventSubtype subtype NS_AVAILABLE_IOS(3_0);//子事件类型 @property(nonatomic,readonly) NSTimeInterval timestamp;//事件发生时间 //返回与接收器相关联的所有触摸对象。
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, nullable) NSSet <UITouch *> *allTouches;
#else
- (nullable NSSet <UITouch *> *)allTouches;
#endif
- (nullable NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;//返回属于一个给定视图的触摸对象,用于表示由接收器所表示的事件。
- (nullable NSSet <UITouch *> *)touchesForView:(UIView *)view;//返回属于一个给定窗口的接收器的事件响应的触摸对象。
- (nullable NSSet <UITouch *> *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture NS_AVAILABLE_IOS(3_2);//返回触摸对象被传递到特殊手势识别 //会将丢失的触摸放到一个新的 UIEvent 数组中,你可以用 coalescedTouchesForTouch(_:) 方法来访问
- (nullable NSArray <UITouch *> *)coalescedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0); //辅助UITouch的触摸,预测发生了一系列主要的触摸事件。这些预测可能不完全匹配的触摸的真正的行为,因为它的移动,所以他们应该被解释为一个估计。
- (nullable NSArray <UITouch *> *)predictedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0); @end
3、触摸事件示例
相关方法:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);
拖动和移动效果图:

代码展示:
//拖动视图
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currPoint = [touch locationInView:self.view];
CGPoint prePoint = [touch previousLocationInView:self.view];
CGRect frame = self.btnView.frame;
frame.origin.x += currPoint.x -prePoint.x;
frame.origin.y += currPoint.y -prePoint.y;
self.btnView.frame = frame;
} //点击移动
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGRect frame = self.btnView.frame;
frame.origin = [touch locationInView:self.view];
self.btnView.frame = frame;
}
4、摇晃事件示例
相关方法:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
效果图:模拟器摇一摇「Hardware」-「Shake Gesture」来测试 ,快捷键:command+control+z

摇一摇显示随机图片:
@implementation MotionImageView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.image = [self getImage];
}
return self;
} - (BOOL)canBecomeFirstResponder{
return YES;
} - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion == UIEventSubtypeMotionShake) {
self.image = [self getImage];
}
}
- (UIImage *)getImage{
int index = arc4random() % +;
NSString *imageName = [NSString stringWithFormat:@"pic%i.png",index];
UIImage *image = [UIImage imageNamed:imageName];
return image; }
@end
在ViewController中使用:
//成为第一响应者
- (void)viewDidAppear:(BOOL)animated{
[self.imageView becomeFirstResponder];
}
//注销第一响应者
- (void)viewDidDisappear:(BOOL)animated{
[self.imageView resignFirstResponder];
}
//支持摇一摇功能
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
}
5、遥控事件示例
相关方法:
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);
代码实现:
#import "VideoViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface VideoViewController ()
{
UIButton *_playButton;
BOOL _isPlaying;
AVPlayer *_player;
}
@end @implementation VideoViewController - (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://music.163.com/song/media/outer/url?id=1293886117.mp3"];
_player = [[AVPlayer alloc] initWithURL:url];
} #pragma mark 远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
if(event.type == UIEventTypeRemoteControl){
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[_player play];
_isPlaying = true;
NSLog(@"播放");
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
[self btnClick:_playButton];
NSLog(@"播放和暂停之间切换【操作:播放或暂停状态下,按耳机线控中间按钮一下】");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"下一曲【操作:按耳机线控中间按钮两下】");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"上一曲【操作:按耳机线控中间按钮三下】");
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:
NSLog(@"快进开始【操作:按耳机线控中间按钮两下不要松开】");
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
NSLog(@"快进结束【操作:按耳机线控中间按钮两下到了快进的位置松开】");
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
NSLog(@"快退开始【操作:按耳机线控中间按钮三下不要松开】");
break;
case UIEventSubtypeRemoteControlEndSeekingBackward:
NSLog(@"快退结束【操作:按耳机线控中间按钮三下到了快退的位置松开】");
break;
case UIEventSubtypeRemoteControlStop:
NSLog(@"停止");
break;
case UIEventSubtypeRemoteControlPause:
NSLog(@"暂停");
break;
default:
break;
}
[self changeUIState];
} } #pragma mark 界面布局
- (void)initLayout{
//专辑封面
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"pic1.jpg"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView]; //播放控制面板
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
view.backgroundColor = [UIColor yellowColor];
view.alpha = 0.9;
[self.view addSubview:view]; //添加播放按钮
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
_playButton.bounds = CGRectMake(, , , );
_playButton.center = CGPointMake(CGRectGetWidth(view.frame)/,CGRectGetHeight(view.frame)/);
[self changeUIState];
[_playButton addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:_playButton]; } #pragma mark 界面状态
- (void)changeUIState{
if(_isPlaying){
UIImage *pauseImage = [UIImage imageNamed:@"pic2.png"];
[_playButton setImage:pauseImage forState:UIControlStateNormal];
}else{
UIImage *playImage = [UIImage imageNamed:@"pic3.png"];
[_playButton setImage:playImage forState:UIControlStateNormal];
}
} - (void)btnClick:(UIButton *)btn{
if (_isPlaying) {
[_player pause];
}else{
[_player play];
}
_isPlaying =! _isPlaying;
[self changeUIState];
}
@end
线控代码实现
号外1:获取网易云mp3路径
号外2:后台播放音乐

- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
}
6、物理按钮示例
按压事件代表了对一个游戏控制器,苹果TV远程,或其他有物理按钮的设备之间的交互。;
所以对于iPhone的手机而言在没有物理按键连接的情况下,是无法触发该事件的;
相关方法:
//物理按钮 深按API,一般用于遥控器
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);// 开始按压的时候调用
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);// 按压改变的时候调用
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);// 按压结束的时候调用
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);// 当系统发出取消按压事件的时候调用
UIPress相关API
NS_ENUM_AVAILABLE_IOS(9_0) typedef NS_ENUM(NSInteger, UIPressPhase) {
UIPressPhaseBegan, // 开始
UIPressPhaseChanged, // 变化
UIPressPhaseStationary, // 按下不动时
UIPressPhaseEnded, // 结束
UIPressPhaseCancelled, // 取消
};
NS_ENUM_AVAILABLE_IOS(9_0) typedef NS_ENUM(NSInteger, UIPressType) {
UIPressTypeUpArrow, //向上的键被按压
UIPressTypeDownArrow, //向下的键被按压
UIPressTypeLeftArrow, //向左的键被按压
UIPressTypeRightArrow, //向右的键被按压
UIPressTypeSelect, //选择 的键被按压
UIPressTypeMenu, //菜单 的键被按压
UIPressTypePlayPause, //播放/暂停 的键被按压
};
NS_CLASS_AVAILABLE_IOS(9_0) @interface UIPress : NSObject
@property(nonatomic,readonly) NSTimeInterval timestamp; //时间
@property(nonatomic,readonly) UIPressPhase phase; //按下阶段
@property(nonatomic,readonly) UIPressType type; //按下的类型
@property(nullable,nonatomic,readonly,strong) UIWindow *window; //所处视图
@property(nullable,nonatomic,readonly,strong) UIResponder *responder;//
@property(nullable,nonatomic,readonly,copy) NSArray <UIGestureRecognizer *> *gestureRecognizers;//手势
@property(nonatomic, readonly) CGFloat force; //按钮按压的力 返回一个介于0和1之间的值。数字按钮返回0或1。
iOS开发UIEvent事件简介的更多相关文章
- iOS开发UIView.h简介
1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...
- iOS开发CGImage.h简介
1.前因 由于剪切图片用到下面方法,此方法属于CGImage.h中,通过创建CGImageRef像素位图,可以通过操作存储的像素位来编辑图片. /* Create an image using the ...
- iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...
- 【iOS 开发】iOS 开发 简介 (IOS项目文件 | MVC 模式 | 事件响应机制 | Storyboard 控制界面 | 代码控制界面 | Retina 屏幕图片适配)
一. iOS 项目简介 1. iOS 文件简介 创建一个 HelloWorld 项目, 在这个 IOS 项目中有四个目录 : 如下图; -- HelloWorldTests 目录 : 单元测试相关的类 ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
- 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- iOS开发系列之远程控制事件
在今天的文章中还剩下最后一类事件:远程控制,远程控制事件这里主要说的就是耳机线控操作.在前面的事件列表中,大家可以看到在iOS中和远程控制事件有关的只有一个- (void)remoteControlR ...
- iOS开发系列之触摸事件
基础知识 三类事件中触摸事件在iOS中是最常用的事件,这里我们首先介绍触摸事件. 在下面的例子中定义一个KCImage,它继承于UIView,在KCImage中指定一个图片作为背景.定义一个视图控制器 ...
随机推荐
- Ubuntu 14.04/16.04/18.04安装最新版Eigen3.3.5
https://blog.csdn.net/xiangxianghehe/article/details/81236299 sudo cp -r /usr/local/include/eigen3 / ...
- 【HTML】框架集(Framesets)
1.Frameset的使用 所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只 要 <FRAMESET> <FRAME> 即可,而所有框架标记 要放在一个总起的 htm ...
- UDP 两种丢包处理策略:丢包重传(ARQ) 和 前向纠错(FEC)
目录 1. 两种丢包处理策略 2. 前向纠错(FEC) 3. 丢包重传(ARQ) [参考文献] 1. 两种丢包处理策略 为了保证实时性,通常适应UDP协议来针对RTP数据进行传输,而UDP无法保证数据 ...
- PAT_A1124#Raffle for Weibo Followers
Source: PAT A1124 Raffle for Weibo Followers (20 分) Description: John got a full mark on PAT. He was ...
- TIKA环境配置
本章将指导完成设置Apache Tika在Windows和Linux的配置过程.用户管理是必要的,同时安装了Apache Tika. 系统要求 JDK Java SE 2 JDK 1.6 或以上 内存 ...
- 深入理解JAVA虚拟机原理之内存分配策略(二)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 1.对象优先在Eden分配 大多情况,对象在新生代Eden区分配.当Eden区没 ...
- C#WinForm 窗体回车替换Tab
/// <summary> /// 回车切换控件 /// </summary> /// <param name="sender"></pa ...
- 总分 Score Inflation
题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...
- S1#Python之shebang
点1 - Python之shebang 一. shebang 在计算机科学中,Shebang是一个由井号和叹号构成的字符串行,其出现在文本文件的第一行的前两个字符. 在文件中存在Shebang的情况下 ...
- uname - 显示输出系统信息
总览 uname [OPTION]... 描述 显示相应的系统信息. 没有指定选项时,同 -s. -a, --all 显示所有的信息 -m, --machine 显示机器(硬件)类型 -n, --no ...