iOS 【UIKit-iOS事件之触摸事件】
2015-12-17 12:27
381 查看
引入 iOS事件 概念
iOS中有三种事件: 触摸事件 Multitouch events(一般的手指触摸屏幕的事件)、加速计事件 Accelerometer events(微信摇一摇)、远程控制事件 Remote control events(蓝牙、远程控制音量等等)
(1)触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称这些对象为响应者对象。(比如说 UIApplication、UIViewController、UIView 都是继承自UIResponder的)
①一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
②一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
③一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
④触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法(没法测试的)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
★提示:touches中存放的都是UITouch对象
——> 什么是UITouch对象?
当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象
一根手指对应一个UITouch对象
——> UITouch的作用?
保存着跟手指相关的信息,比如触摸的位置、时间、阶段
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指的触摸位置。
当手指离开屏幕时,系统会销毁相应的UITouch对象
——> 提示!
iPhone开发中,要避免使用双击事件!
——> UITouch的属性
①触摸产生时所处的窗口
@property(nonatomic,readonly,retain) UIWindow *window;
②触摸产生时所处的视图
@property(nonatomic,readonly,retain) UIView *view;
③短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger tapCount;
④记录了触摸事件产生或变化时的时间,单位是秒
@property(nonatomic,readonly) NSTimeInterval timestamp;
⑤当前触摸事件所处的状态
@property(nonatomic,readonly) UITouchPhase phase;
示例程序:(触摸移动模拟器中的view)
//
// HMView.m
// HMView 就是storyboard中需要触摸控制移动的view的自定义关联类
//
/**
完整的触摸过程:
touchesBegan ——> touchesMoved ——> touchesEnded
关于 NSSet ——> 无序数组(里面存放的是UITouch对象),区别于 NSArray ——> 有序数组。
*/
#import "HMView.h"
@implementation HMView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取一个touch对象(以前NSArray获取对象的时候是用角标,现在NSSet是无序的,所以没有角标,那么获取的方式就比较的特殊,用的是anyObject方法,可以记作“随便来一个对象”)
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"tapCount:%ld",touch.tapCount);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"%s",__func__);
// 测试完各项数据之后,我们要进行手指移动的操作模拟
// 获取一个UITouch对象
UITouch *touch = [touches anyObject];
// 获取当前UITouch对象(点)的位置
CGPoint current = [touch locationInView:self];
// 获取上一个UITouch(点)的位置
CGPoint pre = [touch previousLocationInView:self];
// x轴偏移量
CGFloat offsetX = current.x - pre.x;
// y轴偏移量
CGFloat offsetY = current.y - pre.y;
// 获取当前的center
CGPoint center = self.center;
center.x += offsetX;
center.y += offsetY;
self.center = center;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"%s",__func__);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s",__func__);
}
@end
iOS中有三种事件: 触摸事件 Multitouch events(一般的手指触摸屏幕的事件)、加速计事件 Accelerometer events(微信摇一摇)、远程控制事件 Remote control events(蓝牙、远程控制音量等等)
(1)触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称这些对象为响应者对象。(比如说 UIApplication、UIViewController、UIView 都是继承自UIResponder的)
①一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
②一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
③一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
④触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法(没法测试的)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
★提示:touches中存放的都是UITouch对象
——> 什么是UITouch对象?
当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象
一根手指对应一个UITouch对象
——> UITouch的作用?
保存着跟手指相关的信息,比如触摸的位置、时间、阶段
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指的触摸位置。
当手指离开屏幕时,系统会销毁相应的UITouch对象
——> 提示!
iPhone开发中,要避免使用双击事件!
——> UITouch的属性
①触摸产生时所处的窗口
@property(nonatomic,readonly,retain) UIWindow *window;
②触摸产生时所处的视图
@property(nonatomic,readonly,retain) UIView *view;
③短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger tapCount;
④记录了触摸事件产生或变化时的时间,单位是秒
@property(nonatomic,readonly) NSTimeInterval timestamp;
⑤当前触摸事件所处的状态
@property(nonatomic,readonly) UITouchPhase phase;
示例程序:(触摸移动模拟器中的view)
//
// HMView.m
// HMView 就是storyboard中需要触摸控制移动的view的自定义关联类
//
/**
完整的触摸过程:
touchesBegan ——> touchesMoved ——> touchesEnded
关于 NSSet ——> 无序数组(里面存放的是UITouch对象),区别于 NSArray ——> 有序数组。
*/
#import "HMView.h"
@implementation HMView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取一个touch对象(以前NSArray获取对象的时候是用角标,现在NSSet是无序的,所以没有角标,那么获取的方式就比较的特殊,用的是anyObject方法,可以记作“随便来一个对象”)
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"tapCount:%ld",touch.tapCount);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"%s",__func__);
// 测试完各项数据之后,我们要进行手指移动的操作模拟
// 获取一个UITouch对象
UITouch *touch = [touches anyObject];
// 获取当前UITouch对象(点)的位置
CGPoint current = [touch locationInView:self];
// 获取上一个UITouch(点)的位置
CGPoint pre = [touch previousLocationInView:self];
// x轴偏移量
CGFloat offsetX = current.x - pre.x;
// y轴偏移量
CGFloat offsetY = current.y - pre.y;
// 获取当前的center
CGPoint center = self.center;
center.x += offsetX;
center.y += offsetY;
self.center = center;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// UITouch *touch = [touches anyObject];
// NSLog(@"phase:%ld",touch.phase);
// NSLog(@"%s",__func__);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s",__func__);
}
@end
相关文章推荐
- jQuery移动页面开发中的触摸事件与虚拟鼠标事件简介
- Cocos2d-x触摸事件实例
- 关于父view拦截触屏事件
- ios自用学习笔记-触摸事件与手势识别
- android触屏获取坐标
- iOS事件传递过程
- cocos2d-x-触屏(Touch)事件详解
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- UI基础之----触摸事件
- cocos2d-x (四):触摸事件处理机制
- Android 触摸事件传递机制
- 触摸事件分发机制详解
- Android触摸事件分发机制
- cocos2d-x3.6 连连看点击事件
- cocos2d-x3.6 连连看触摸事件传递
- 触摸事件
- 【cocos2d-x入门实战】微信飞机大战之七:触摸事件和优先级
- Android 控件的触摸事件传递与处理
- Android onTouch事件传递机制
- Touch实现触摸事件的妙用