iOS学习笔记(5):UITouch&UIEvent

2016-10-06  本文已影响0人  SevEnOye

UITouch作用

UITouch的属性

// 触摸产生时所处的窗口
@property(nonatmoic, readonly, retain) UIWindow *window;
// 触摸产生时所处的视图
@property(nonatmoic, readonly, retain) UIView *view;
// 短时间内点按屏幕的次数, 可以根据tapCount判断单击、双击或更多的点击
@property(nonatmoic, readonly) NSUInteger tapCount;
// 记录了触摸事件产生或变化时的时间,单位是秒
@property(nonatomic, readonly) NSTimeInterval timestamp;
// 当前触摸事件所处的状态
@property(nonatomic, readonly) UITouchPhase phase;

UITouch的方法

// 返回值表示触摸在view上的位置
// 这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0))
// 调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
- (CGPoint)locationInView:(UIView *)view;
// 该方法记录了前一个触摸点的位置
- (CGPoint)previousLocationInView:(UIView *)view;

view的拖拽

 // 让当前控件随着手指移动而移动
 // 获取UITouch
 UITouch *touch = [touches anyObject];
 // 获取当前点
 CGPoint curP = [touch locationInView:self];
 // 获取上一个点
 CGPoint preP = [touch previousLocationInView:self];
 // 计算手指x轴偏移量
 CGFloat offsetX = curP.x - preP.x;
 // 计算手指x轴偏移量
 CGFloat offsetY = curP.y - preP.y;
 // 修改控件的形变
 self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);

UIEvent

常见属性

// 事件类型
@property(nonatomic, readonly) UIEventType type;
@property(nonatomic, readonly) UIEventSubtype subtype;
// 事件产生的时间
@property(nonatomic, readonly) NSTimeInterval timestamp;
上一篇下一篇

猜你喜欢

热点阅读