iOS:触摸控件UITouch、事件类UIEvent

简介:

UITouch:触摸控件类   UIEvent:事件类

 

️️UITouch的介绍️️️

一、触摸状态类型枚举

typedef NS_ENUM(NSInteger, UITouchPhase) {

    UITouchPhaseBegan,             // 开始触摸

    UITouchPhaseMoved,             // 触摸移动

    UITouchPhaseStationary,       // 触摸没有移动

    UITouchPhaseEnded,             //触摸结束

    UITouchPhaseCancelled,         //取消触摸

};

 

@interface UITouch : NSObject

二、属性:

//时间戳记录了触摸事件产生或变化时的时间,单位是秒

@property(nonatomic,readonly) NSTimeInterval   timestamp;

//当前触摸事件在一个周期中所处的状态               

@property(nonatomic,readonly) UITouchPhase  phase;

 //表示短时间内点按屏幕的次数            

@property(nonatomic,readonly) NSUInteger  tapCount; 

//触摸的主半径                

@property(nonatomic,readonly) CGFloat majorRadius;

//触摸的主半径的公差

@property(nonatomic,readonly) CGFloat majorRadiusTolerance;

//触摸产生时所处的窗口。由于窗口可能发生变化,当前所在的窗口不一定是最开始的窗口

@property(nonatomic,readonly,retain) UIWindow    *window;

//触摸产生时所处的视图。由于视图可能发生变化,当前视图也不一定时最初的视图

@property(nonatomic,readonly,retain) UIView      *view;

//触摸手势数组

@property(nonatomic,readonly,copy)   NSArray   *gestureRecognizers;

 

三、方法:

//返回当前触摸点的位置

- (CGPoint)locationInView:(UIView *)view;

//返回上一个触摸点的位置

- (CGPoint)previousLocationInView:(UIView *)view;

@end

 

四、由于触摸点被放在了NSSet,所以通过NSSet集合的一个属性和方法可以获取触摸点:

//获取全部的触摸点

@property (readonly, copy) NSArray *allObjects;

//获取当前触摸点

- (id)anyObject;

 

 

===============================================================

 

 

 

️️UIEvent的介绍️️️

 //输入事件类型枚举

typedef NS_ENUM(NSInteger, UIEventType) {

   UIEventTypeTouches,             //触摸事件

    UIEventTypeMotion,              //运动事件

    UIEventTypeRemoteControl,    //远程控制事件

};

//输入事件不同类型的一些具体事件枚举

typedef NS_ENUM(NSInteger, UIEventSubtype) {

    UIEventSubtypeNone                              = 0,

    UIEventSubtypeMotionShake                       = 1 

    UIEventSubtypeRemoteControlPlay                 = 100, 

    UIEventSubtypeRemoteControlPause                = 101, 

    UIEventSubtypeRemoteControlStop                 = 102,

    UIEventSubtypeRemoteControlTogglePlayPause      = 103,

    UIEventSubtypeRemoteControlNextTrack            = 104,

    UIEventSubtypeRemoteControlPreviousTrack        = 105,

    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,

    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,

    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,

    UIEventSubtypeRemoteControlEndSeekingForward    = 109;

};

 

//类

@interface UIEvent : NSObject

//属性

@property(nonatomic,readonly) UIEventType    type ;            //事件类型

@property(nonatomic,readonly) UIEventSubtype  subtype ;     //同一事件类型的具体事件

@property(nonatomic,readonly) NSTimeInterval  timestamp;    //事件触发时间间隔

//方法

※所有触摸点

- (NSSet *)allTouches; 

※窗体上的触摸点

- (NSSet *)touchesForWindow:(UIWindow *)window;

※视图上的触摸点

- (NSSet *)touchesForView:(UIView *)view;

※手势触摸点

 - (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture;

 @end

 

五、举例如下:演示触摸事件(都是用鼠标代替手指在模拟器上进行测试)

实例一:打印触摸输出

1.设置用户交互和触摸点

//支持用户交互,能够处理触摸事件
self.view.userInteractionEnabled = YES;
    
//支持多点触摸
self.view.multipleTouchEnabled = YES;

 

2.实现UIResponser中触摸的4个事件

触摸开始

复制代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始");
    
    //取出一个touch对象
    UITouch *touch = [touches anyObject];
    
    //取出当前点
    CGPoint location = [touch locationInView:self.view];
    
    //取出上一点
    CGPoint previousLocation = [touch previousLocationInView:self.view];
    
    NSLog(@"%@,%@",NSStringFromCGPoint(location),NSStringFromCGPoint(previousLocation));
    
    
    //遍历每一个touch对象
    [touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) {
        
    }];
}
复制代码

触摸移动

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸移动");
}

触摸结束

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
}

触摸取消(该事件在模拟器不好演示)

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸取消");
}

 

演示结果如下:

1.在屏幕上开始触摸和结束触摸时:

2015-10-08 21:21:24.875 02-touch[5988:346647] 触摸开始
2015-10-08 21:21:24.875 02-touch[5988:346647] {219.5, 181},{219.5, 181}
2015-10-08 21:21:24.983 02-touch[5988:346647] 触摸结束

2.在屏幕上触摸移动时:

2015-10-08 21:23:00.388 02-touch[5988:346647] 触摸移动
2015-10-08 21:23:00.413 02-touch[5988:346647] 触摸移动
2015-10-08 21:23:00.430 02-touch[5988:346647] 触摸移动

 

实例二:触摸时,视图变色(每触摸一次,颜色就交替改变)

<1>布局故事板,在视图中在拖一个试图UIView控件,设置合适大小和背景颜色

<2>自定义一个试图类,将控件与该类关联

         

<3>在自定义类myView.m中实现触摸开始事件

复制代码
//开始触摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.backgroundColor isEqual:[UIColor redColor]])
    {
        self.backgroundColor = [UIColor purpleColor];
    }
    else
    {
        self.backgroundColor = [UIColor redColor];
    }
}
复制代码

<4>演示结果如下

    开始颜色:红色                        触摸一次:变为紫色                 再触摸一次:又变为红色

   

 

实例三:触摸移动时,移动红色试图位置

<1>布局故事板,在视图中在拖一个试图UIView控件,设置合适大小和背景颜色

<2>自定义一个试图类,将控件与该类关联,同时将视图IBOutLet到视图控制器类中

       

<3> 在控制器类ViewController.m中实现触摸移动事件

复制代码
//添加移动事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取触摸对象touch
    UITouch *touch = [touches anyObject];
    
    //当前触摸点
    CGPoint location = [touch locationInView:self.view];
    
    
    //设置触摸点只有在myView中,才可以移动myView视图
    CGPoint point = [touch locationInView:self.myView];
    if ([self.myView pointInside:point withEvent:event])
    {
        //上一个触摸点
        CGPoint previousLocation = [touch previousLocationInView:self.view];
        
        //计算位移
        CGFloat xOffset = location.x - previousLocation.x;
        CGFloat yOffset = location.y - previousLocation.y;
        
        //设置视图新的位置
        CGPoint center = self.myView.center;
        self.myView.center = CGPointMake(center.x+xOffset, center.y+yOffset);
    }
    
}
复制代码

<4>演示结果如下:触摸红色视图位置拖拽移动即可

    

     

 

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
分类:  iOS高级

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4862353.html,如需转载请自行联系原作者
相关文章
|
测试技术 程序员 C++
iOS:项目中无用类检测和无用图片检测汇总
在涉及到项目大改版,或者涉及到某个功能模块大变更,就会涉及到图片废弃和文件废弃的情况。 但是这时候就会遗留下一个很大的问题,没有将废弃的、无用的文件类或资源删除干净。而这次需要对工程代码的无用资源和无用文件进行删除处理,感触颇多,故在此笔记。 首先,感觉很多人的代码习惯还是恶待提高。比如我发现一些人的代码操作习惯,从好到次,可以大略分以下情况
1125 0
iOS:项目中无用类检测和无用图片检测汇总
|
7月前
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
38 0
|
11月前
|
iOS开发
iOS短信验证码控件,自动输入回调两次解决办法
iOS短信验证码控件,自动输入回调两次解决办法
307 0
|
12月前
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
124 0
|
iOS开发
(六)IOS手势和触摸的用法
(六)IOS手势和触摸的用法
191 0
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
127 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
iOS开发
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
234 0
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
|
iOS开发
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
116 0
|
iOS开发
iOS开发-加在透明视图上的控件会透明
iOS开发-加在透明视图上的控件会透明
112 0
|
安全 iOS开发
iOS小技能:下拉刷新控件的适配
1. 下拉顶部背景色设置: 往tableView的父控件添加拉伸背景视图 2. present 半屏适配 iOS13 modalPresentationStyle属性默认不是全屏样式`UIModalPresentationFullScreen`,而是半屏样式,需要根据需求手动设置。 present 半屏,会导致列表下拉刷新失效。
159 0
iOS小技能:下拉刷新控件的适配