iOS事件:触摸事件.运动事件.远程控制事件
iOS中,提供了事件处理:触摸事件,运动事件,远程控制事件.这很大得方便程序猿的工作.
这里先简单做个介绍:
//
// ViewController.m
// demo
//
// Created by shaoting on 15/11/23.
// Copyright © 2015年 9elephas. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController
/**
* IOS事件分类:1,触摸事件2,运动事件3,远程控制事件
1.触摸事件:触碰屏幕
2.运动事件:通过加速器触发(如晃动手机)
3.远程控制事件:通过其他设备远程控制(如蓝牙)
在iOS中,只有继承自UIResponder类的对象才能触发事件
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"iOS事件";
self.view.backgroundColor = [UIColor whiteColor];
//开个图
UIImage * image = [UIImage imageNamed:@"屏幕快照 2015-11-23 17.37.19"];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
self.imageView.image = image;
[self.view setBackgroundColor:[UIColor colorWithPatternImage:image]]; //view的背景色使用image
[self.view addSubview:_imageView];
}
//触摸事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"一根手指或多根手指触摸时触发");
UITouch * touch = [touches anyObject]; //获得任意触摸对象
NSLog(@"触摸对象%@",touch);
CGPoint current = [touch locationInView:self.view];//获得当前的触摸对象
NSLog(@"当前的触摸对象%@",NSStringFromCGPoint(current)); //把CGPoint对象转换为字符串对象
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"一根或者多根手指离开屏幕时触发");
}
//以图片随鼠标移动为例
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"一根或者多根手指移动时触发");
UITouch * touch = [touches anyObject];
CGPoint current = [touch locationInView:self.view];//当前位置
CGPoint previous = [touch previousLocationInView:self.view];//当前位置的前一个位置
CGPoint center = _imageView.center;//移动前的中心位置
CGPoint offset = CGPointMake(current.x - previous.x, current.y - previous.y);
_imageView.center = CGPointMake(center.x+offset.x, center.y+offset.y);
NSUInteger count = touch.tapCount;//短时间内点击的次数
NSLog(@"短时间内点击的次数%ld",count);
UITouchPhase phase = touch.phase;//触摸周期内的各个状态
NSLog(@"触摸周期内的各个状态%ld",phase);
NSTimeInterval tamp = touch.timestamp;//触摸产生或者变化的时间戳
NSLog(@"触摸产生或者变化的时间戳%f",tamp);
UIView * view = touch.view; //触摸时所在的视图
NSLog(@"触摸时所在的视图%@",view);
UIWindow * window = touch.window;//触摸时所在的window
NSLog(@"触摸时所在的window%@",window);
}
//运动事件
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"运动开始时触发");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"运动结束时触发");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"运动被意外取消时触发");
}
//远程控制事件
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
NSLog(@"接收到远程控制消息时执行");
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
其中,模拟器开启晃动手机的功能键是:
触摸事件:
//
// ViewController.m
// demo1
//
// Created by shaoting on 15/11/23.
// Copyright © 2015年 9elephas. All rights reserved.
// #import "ViewController.h"
#define Hight [UIScreen mainScreen].bounds.size.height-49
#define Width [UIScreen mainScreen].bounds.size.width @interface ViewController ()<UIGestureRecognizerDelegate>{
int _count;
}
@end @implementation ViewController
/**
* 复杂手势:6种
点按手势:UITapGestureRecognizer
捏合手势:UIPinchGestureRecognizer
拖动手势:UIPanGetureRecognizer
清扫手势:UISwipeGetureRecognizer
旋转手势:UIRotationGetureReconizer
长按手势:UILongPressGetureReconizer
6种手势中点按手势属于离散型手势(不用判断状态),其余手势属于连续手势(除清扫手势外均需要判断状态). 各手势的触发时机:点按事件:点击屏幕然后松开时触发
长按事件:长时间点击屏幕不松开即会触发(不加长按&拖动冲突解决),长时间点击屏幕松开即会触发(加长按&拖动手势冲突解决ß)
拖动事件:手指在屏幕上拖动时触发
旋转手势:手指在屏幕上旋转时触发
清扫手势:只在清扫完毕(手指清扫屏幕离开屏幕时)才会触发
捏合手势:手指捏合时触发 事件冲突:点击手势会和旋转手势会发生冲突.
清扫手势会和拖动手势会发生冲突. */
- (void)viewDidLoad {
[super viewDidLoad];
[self initLayout];
[self addGesture]; // Do any additional setup after loading the view, typically from a nib.
}
//布局
-(void)initLayout{
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , Width, Hight)];
_imageView.userInteractionEnabled = YES; //必须设置为YES,否则无法就收手势操作
UIImage * image = [UIImage imageNamed:@""];
_imageView.image = image;
self.title = @"";
[self.view addSubview:_imageView];
}
//添加手势
-(void)addGesture{
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = ; //设置点击次数,默认为1
tap.numberOfTouchesRequired = ;//设置为手指数
[self.view addGestureRecognizer:tap];//添加手势进视图控制器上的视图 UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch]; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan]; UISwipeGestureRecognizer * left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:left];
UISwipeGestureRecognizer * right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:right]; UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.view addGestureRecognizer:rotation]; UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:longPress]; //手势冲突解决
[pan requireGestureRecognizerToFail:left];
[pan requireGestureRecognizerToFail:right];//解决拖动手势和清扫手势的冲突
[longPress requireGestureRecognizerToFail:pan];//解决长按手势和拖动手势的冲突 //多种手势在不同视图上的同时执行
//iOS默认只会触发一次,因为触发是沿着响应者链依次匹配,只要匹配到响应就会停止.可以修改- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer的返回值,默认返回NO
//上面演示了添加长按手势进视图控制器视图上,下面再添加长按手势到_imageView上,让这两个手势可以同时在不同视图上执行.
UILongPressGestureRecognizer * longpress1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress1:)];
longpress1.delegate = self;
[_imageView addGestureRecognizer:longpress1]; }
//点按事件
//点按是否显示状态栏
-(void)tap:(UITapGestureRecognizer *)tap{
BOOL recognizer = !self.navigationController.navigationBarHidden;
[self.navigationController setNavigationBarHidden:recognizer];
}
//捏合事件
//缩放图片
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
// NSLog(@"%ld",(long)pinch.state); // 捏合状态
//状态:0--未开始 1--手势开始 2--手势进行 3--手势完成
// typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
//
// UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
// UIGestureRecognizerStateChanged, // 手势状态发生转变
// UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)
// UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
//
// UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态
//
// UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
// };
if (pinch.state == UIGestureRecognizerStateChanged) {
_imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); //.scele记录缩放属性
}else if(pinch.state == UIGestureRecognizerStateEnded){ //缩放完毕后恢复
[UIView animateWithDuration: animations:^{
_imageView.transform = CGAffineTransformIdentity;//取消形变
}];
}
}
//拖动手势
-(void)pan:(UIPanGestureRecognizer *)pan{ if (pan.state == UIGestureRecognizerStateChanged) {
CGPoint transform = [pan translationInView:self.view];//利用translationInView:取得在相对视图中的移动
_imageView.transform = CGAffineTransformMakeTranslation(transform.x, transform.y);
}else if(pan.state == UIGestureRecognizerStateEnded){
[UIView animateWithDuration: animations:^{
_imageView.transform = CGAffineTransformIdentity;
}];
}
} //清扫手势
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
//因为清扫手势虽然是连续手势,但是只有在清扫完毕时才会触发,所以不用判断状态
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
[self nextImage];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
[self lastImage];
}
}
-(void)nextImage{
int index=(_count++)%;
NSString *imageName=[NSString stringWithFormat:@"%i",index];
_imageView.image=[UIImage imageNamed:imageName];
_count=index;
[self showName];
}
-(void)lastImage{
int index=(_count+-)%;
NSString *imageName=[NSString stringWithFormat:@"%i",index];
_imageView.image=[UIImage imageNamed:imageName];
_count=index;
[self showName]; }
-(void)showName{
NSString * name = [NSString stringWithFormat:@"%i",_count];
self.title = name;
}
//旋转事件
//旋转使图片旋转
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
if (rotation.state == UIGestureRecognizerStateChanged) {
_imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);// .rotation记录了改变的弧度
}else if(rotation.state == UIGestureRecognizerStateEnded){
[UIView animateWithDuration: animations:^{
_imageView.transform = CGAffineTransformIdentity;
}];
}
}
//长按事件
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
//长按选择是否下载图片
if (longPress.state == UIGestureRecognizerStateBegan) {
UIAlertController * alterC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否下载该图片" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * cance = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction * down = [UIAlertAction actionWithTitle:@"下载" style:UIAlertActionStyleDefault handler:nil];
[alterC addAction:cance];
[alterC addAction:down];
[self presentViewController:alterC animated:YES completion:nil]; }
}
-(void)longPress1:(UILongPressGestureRecognizer *)longpress{
NSLog(@"添加手势进_imageView");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([otherGestureRecognizer.view isKindOfClass:[self.view class]]) {//如果是控制器视图则继续沿着响应者链传播
return YES;
}
return NO;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
运动事件:
监听运动事件对于UI控件有个前提就是监听对象必须是第一响应者(对于UIViewController视图控制器和UIAPPlication没有此要求)。这也就意味着如果监听的是一个UI控件那么-(BOOL)canBecomeFirstResponder;方法必须返回YES。同时控件显示时(在-(void)viewWillAppear:(BOOL)animated;事件中)调用视图控制器的becomeFirstResponder方法。当视图不再显示时(在-(void)viewDidDisappear:(BOOL)animated;事件中)注销第一响应者身份。(是真的吗?)
//
// ViewController.m
// demo2
//
// Created by shaoting on 15/11/25.
// Copyright © 2015年 9elephas. All rights reserved.
// #import "ViewController.h" @interface ViewController (){
UIImageView * _imageView;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_imageView.image = [UIImage imageNamed:@""];
[self.view addSubview:_imageView];
} #pragma mark 运动开始
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"运动开始了");
//这里只处理摇晃事件
if (motion==UIEventSubtypeMotionShake) {
_imageView.image=[self getImage];
}
}
#pragma mark 运动结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"运动结束了");
}
-(UIImage *)getImage{
int index= arc4random()%;
NSString *imageName=[NSString stringWithFormat:@"%i",index];
UIImage *image=[UIImage imageNamed:imageName];
return image;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
远程控制事件:
这里有点问题,以后再写吧.
iOS事件:触摸事件.运动事件.远程控制事件的更多相关文章
- iOS:触摸控件UITouch、事件类UIEvent
UITouch:触摸控件类 UIEvent:事件类 ❤️❤️❤️UITouch的介绍❤️❤️❤️ 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhas ...
- IOS (补充)触摸事件处理
[1]事件的基本概念 UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象. 分三类:触摸事件.晃动事件.远程控制事件 触摸事件:用户通过触摸设备屏幕操作对象.输入数据.支持多点触摸,包括1 ...
- iOS基础 - 触摸事件与手势识别
一.iOS的输入事件 UIKit可识别三种类型的输入事件: 触摸事件 运动(加速计)事件 远程控制事件 二.UIEvent iOS中许多事件对象都是UIEvent类的实例,记录事件产生的时刻和类型 U ...
- iOS:触摸事件和手势识别的介绍
触摸事件和手势识别的介绍 1.iOS的输入事件 UIKit可识别三种类型的输入事件: 触摸事件 运动事件 远程控制事件 iOS中许多事件对象都是UIEvent类的实例,UIEvent记录了事件所产生 ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- iOS开发系列之远程控制事件
在今天的文章中还剩下最后一类事件:远程控制,远程控制事件这里主要说的就是耳机线控操作.在前面的事件列表中,大家可以看到在iOS中和远程控制事件有关的只有一个- (void)remoteControlR ...
- iOS的触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称其为@''响应者对象''UIApplication,UIViewController,UIView都 ...
- 关于IOS浏览器:document,body的click事件触发规则
今天做了个手机页面,点击某个按钮->弹出菜单,再点击菜单以外的任意位置->关闭菜单,在其他浏览器里面没有问题,但是在IOS浏览器中并不会关闭. 网上解决这个bug的帖子很多,这篇帖子主要是 ...
- 关于ios苹果系统的中的右键事件,查遍了全网都没有的小技巧。
前阵子公司要求写一套手机端,兼容各种平台和系统,当然,pc端也没有放过. 我用了bootstrap框架和jq.在安卓中的右键事件只需要取消浏览器默认事件,然后长按就可以触发pc端的右键事件,非常好,一 ...
随机推荐
- JavaSE基础之this关键字的引用
1.0 this 指代当前对象, 在一般方法中可以通过this来引用当前对象的成员(方法,属性). 2.0 通过 this() 调 用重载的构造器,需要注意的是,通过此种方法调用的重载构造器 ...
- 初始hibernate(一)
Hibernate(开放源代码的对象关系映射框架) Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的or ...
- MATLAB实现矩阵分块相乘
要实现一下功能,这里$\bf{x}_i$为行向量 $${\bf{A}} = \left[ \begin{array}{l}{{\bf{x}}_1}\\{{\bf{x}}_2}\end{array} \ ...
- Q: How could I use MATLAB interface for parameter selection?
Q: How could I use MATLAB interface for parameter selection? One can do this by a simple loop. See t ...
- Objective-C:Foundation框架-常用类-NSMutableArray
NSMutableArray是NSArray对的子类,它是可变的,可以随意添加或者删除元素.与Array,也有静态和动态两种创建方法.也可已使用NSArray的方法来创建NSMutableArray. ...
- GBK编码相关
如上图.的GBK编码是A3AE, 那么·对应的无符号整数值应该是A*16**3+E*16**2+A+3, 无符号整数值所在地址的第一个自己是A3,第二个字节是AE
- mysql 忘记root密码修改方法
先将mysql安装bin目录(例如:c:xxx\xxx\mysql\bin 加入环境变量) 1.在命令行窗口下输入net stop mysql5 或 net stop mysql 2.开一个命令行窗 ...
- spring aop编程
1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代 ...
- html圆角提示效果
<fieldset> <legend>标题</legend> 内容 </fieldset>
- COleDateTime类型的应用
使用COleDateTime类1) 获取当前时间. CTime time; time = CTime::GetCurrentTime();2) 获取时间元素. int y ...