您的位置:首页 > 产品设计 > UI/UE

UITouch 触摸

2015-12-03 15:48 387 查看

UITouch 触摸

触摸是系统的一些方法,发生触摸事件的时候会自动调用这些方法,分别是:点击的时候调用;在屏幕上移动的时候,不断调用;离开屏幕的时候调用;被打断的时候调用 (电话,锁屏,…. )

1.点击的时候调用的方法- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event;

代码:

// 按住alt键,可以使用二个手指头的手势
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%@",NSStringFromSelector(_cmd));
UITouch *touch = [touches anyObject];
// 点击屏幕的次数
NSLog(@"%lu",touch.tapCount);
if (touch.tapCount == 1) {
// 延迟0.5秒,执行singleClick方法
[self performSelector:@selector(singleClick) withObject:nil afterDelay:0.5];
// [self singleClick];
} else {
// 先取消还没有执行的单击方法
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleClick) object:nil];
[self doubleClick];
}
}

// 单击
- (void)singleClick {
_imageView.transform = CGAffineTransformScale(_imageView.transform, 1.1, 1.1);
// 介绍一下alertController, iOS8 新增的控件
// UIAlertControllerStyleActionSheet  actionSheet类型
// UIAlertControllerStyleAlert        alert类型
// 设置警告控制器的标题和信息,以及类型。
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"警告" message:@"饿了就要吃,困了就睡,你以为这是哪儿!" preferredStyle:UIAlertControllerStyleActionSheet];
// 给警告控制器,添加动作(按钮标题,按钮的类型,按钮点击的回调的block)
[ac addAction:[UIAlertAction actionWithTitle:@"回家" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"我被点了!!");
}]];
[ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点取消按钮了!!");
}]];
// 对于xxxController来说,一般都需要present出来。
[self presentViewController:ac animated:YES completion:nil];
}

// 双击
- (void)doubleClick {
_imageView.transform = CGAffineTransformScale(_imageView.transform, 0.9, 0.9);
}


2.在屏幕上移动的时候,不断调用的方法 - (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event;

代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%@",NSStringFromSelector(_cmd));
// 集合,多少个元素 (表示有多少个touch对象。1个手指头就是1...)
NSLog(@"%lu",touches.count);
// UITouch *touch = [touches anyObject];
if (touches.count > 1) {
UITouch *touch1 = touches.allObjects[0];
// UITouch *touch2 = touches.allObjects[1];
// NSLog(@"%@, %@",NSStringFromCGPoint([touch1 locationInView:self.view]), NSStringFromCGPoint([touch2 locationInView:self.view]));
// 当前,第一个手指头,相对于self.view的坐标
// [touch1 locationInView:self.view]
// 第一个手指头,相对于self.view的 前一个时刻的坐标
// [touch1 previousLocationInView:self.view]
// NSLog(@"%@, %@",NSStringFromCGPoint([touch1 previousLocationInView:self.view]), NSStringFromCGPoint([touch2 previousLocationInView:self.view]));
// 一般我们不这样去做,只是一个随便的小Demo,真要判断用手势的控件
CGPoint l1 = [touch1 locationInView:_imageView];
CGPoint p1 = [touch1 previousLocationInView:_imageView];
CGRect frame = _imageView.bounds;
_imageView.bounds = CGRectMake(0, 0, frame.size.width-l1.x-p1.x, frame.size.height-l1.y-p1.y);
}
}


3.离开屏幕的时候调用的方法 - (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event;

代码:

// 离开屏幕的时候调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%@",NSStringFromSelector(_cmd));
}


4.被打断的时候调用的方法 - (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event;

代码:

// 被打断的时候调用 (电话,锁屏,.... )
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%@",NSStringFromSelector(_cmd));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: