==========无限互联IOS视频学习笔记=====UI高级=====
9.1 触摸和手势:触摸的基本概念
9.2 触摸和手势:单击双击、移动视图
9.3 触摸和手势:捏合手势
1.事件类型
·在iOS上,事件有多种形式:
·触摸事件
·运动事件
·远程控制事件
2.事件处理的方法
响应者类通过复写以下方法,可以监听触摸事件
// 当⼀个或多个手指触碰屏幕时
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
// 当⼀个或多个手指在屏幕上移动时
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
// 当⼀个或多个手指离开屏幕时
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
// 当触摸序列被诸如电话呼入这样的系统事件所取消时
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
3. UITouch触摸对象
4·UITouch类中常用属性
window:
触摸产⽣时所处的窗⼝。
view:
触摸产⽣时所处的视图。
tapCount:
轻击(Tap)操作和鼠标的单击操作类似,tapCount表⽰短时间内轻击屏幕的次数。因此可以根据tapCount判断单击、双击或更多的轻击。
timestamp:
时间戳记录了触摸事件产生或变化时的时间。单位是秒。
phase:
触摸事件在屏幕上有⼀个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。⽽通过phase可以查看当前触摸事件在⼀个周期中所处的状态。phase是UITouchPhase类型的,这是⼀个枚举配型,包含了
· UITouchPhaseBegan(触摸开始)
· UITouchPhaseMoved(接触点移动)
· UITouchPhaseStationary(接触点⽆移动)
· UITouchPhaseEnded(触摸结束)
5.UITouch类中常用方法
- (CGPoint)locationInView:(UIView *)view: 函数返回⼀个CGPoint类型的值,表⽰触摸在view这个视图上的位置,这⾥返回的位置是针对view的坐标系的。
- (CGPoint)previousLocationInView:(UIView *)view: 该⽅方法记录了前⼀个坐标值,函数返回也是⼀个CGPoint类型的值, 表⽰示触摸在 view这个视图上的位置,这⾥返回的位置是针对view的坐标系的.
触摸模拟代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
TouchView *touchView = [[TouchView alloc] initWithFrame:CGRectMake(0, 0, 320, 350)];
touchView.backgroundColor = [UIColor grayColor];
[self.window addSubview:touchView];
return YES;
}
@interface TouchView : UIView
#import "TouchView.h"
@implementation TouchView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 开启触摸响应,默认是yes
self.userInteractionEnabled = YES;
// 开启多点触摸,默认NO;
self.multipleTouchEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
UITouch *touch = [touches anyObject];
NSUInteger tapCount = touch.tapCount;
NSLog(@"tapCount----%d", tapCount);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesEnded");
}
@end