我的iOS学习历程 - UITouch(触摸反应)

平常我们用手机的时候,手机是怎么通过我们点击来做出相对应的反应呢,这篇就来介绍触摸反应:
在UITouch最重要的三个方法就是:
1.touchesBegan(触摸开始)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //  UITouch 保存手指信息(触摸的点)
    UITouch *touch = [touches anyObject];
 //  了解一下touch信息
    /*
       <UITouch: 0x7fd83ac41b40> 
     phase: Began tap count: 1 
     window: <UIWindow: 0x7fd83ac48e30; 
     frame = (0 0; 375 667); (模拟器大小)
     gestureRecognizers = <NSArray: 0x7fd83ac49fe0>; 
     layer = <UIWindowLayer: 0x7fd83ac49230>> 
     view: <TouchView: 0x7fd83ae7b350;(触摸的视图)
     frame = (100 100; 50 50); (触摸视图的view)
     layer = <CALayer: 0x7fd83ae2ae40>> 
     location in window: {140, 126} (当前触摸的点相对于window的坐标)
     previous location in window: {140, 126} (保存触摸的上一个点相对于window坐标)
     location in view: {40, 26}  (当前触摸的点相对于view的坐标)
     previous location in view: {40, 26} (保存触摸的上一个点相对于view的坐标)
     */
    //  取出当前触摸的点
    //  返回一个当前触摸的点 相对于传进去的参数view
    CGPoint p1 = [touch locationInView:self];
    // 返回当前点的上一个点 相对于传进去的参数
    CGPoint p2 = [touch previousLocationInView:self];
}

2.touchesMoved(触摸移动)
比如我们想移动一个view且移动的时候改变他的颜色

UITouch *touch = [touches anyObject];
    //  取出当前点
    //  取出当前点的上一个点
    //  计算X轴偏移量
    //  计算Y轴偏移量
    //  改变视图原来的终点
    // 返回当前点的上一个点 相对于传进去的参数
    CGPoint p2 = [touch previousLocationInView:self];
    //  返回一个当前触摸的点 相对于传进去的参数view
    CGPoint p1 = [touch locationInView:self];
    self.center = CGPointMake(self.center.x + p1.x - p2.x, self.center.y + p1.y - p2.y);

    //  随机变颜色
    //  [0 - 255] 三原色填0~1的小数(arc4random()%256/255.0)
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    //  三原色加透明度

3.touchesEnded 触摸停止

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

4.touchesCancelled触摸中断
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
触摸被中断 例如触摸中来电话,小退出 可以触发中断这个方法
}

而平常我们的摇一摇屏幕手机就接收命令来做出对应动作的是哪个命令呢?
1.motionBegan 屏幕晃动的开始

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}

2.motionEnded 晃动结束
一般都是晃动结束执行指令,如果你想在什么时候执行指令就写在哪个方法里

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//  晃动结束跳转界面
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //  跳转
    [self presentViewController:secondVC animated:YES completion:nil];
    [secondVC release];
}

3.motionCancelled 晃动中断

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}

我们的手机晃动,触摸接收指令是有一个顺序的,这个顺序就是我们的响应者链:
响应者链 分为两个过程
1.查询过程
当你点击屏幕,先定位到应用程序 -> window -> 控制器 -> self.view -> view上的子视图一一查找 -> 定位到被点击的子视图 查询过程结束
2.响应过程
首先看本视图能不能处理事件(实现了touchBegan等方法 就叫做可以处理事件) -> 父视图 ->
一层一层往下看能不能处理,知道window,如果都不能处理,改次点击时间被遗弃 (无效点击)
注意:
默认关闭交互的控件:UILabel UIImageView
如果需要打开交互:
可以写一个,YES为关闭,除了以上两个其余默认是开启的

view.userInteractionEnabled = NO;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是手势解锁的Demo实现过程: 1. 首先创建一个UIView的子类,作为手势解锁的主体视图,我们称之为`GestureLockView`。 2. 在`GestureLockView`中创建一个数组`circleArray`,用于存储手势解锁的圆点。 ``` @property (nonatomic, strong) NSMutableArray *circleArray; ``` 3. 在`GestureLockView`的`layoutSubviews`方法中,创建9个圆点,并加入到`circleArray`中。 ``` - (void)layoutSubviews { [super layoutSubviews]; CGFloat margin = (self.frame.size.width - 3 * kCircleSize) / 4.0; for (int i = 0; i < 9; i++) { CGFloat x = margin + (i % 3) * (margin + kCircleSize); CGFloat y = margin + (i / 3) * (margin + kCircleSize); CGRect frame = CGRectMake(x, y, kCircleSize, kCircleSize); GestureLockCircle *circle = [[GestureLockCircle alloc] initWithFrame:frame]; circle.tag = i + 1; [self addSubview:circle]; [self.circleArray addObject:circle]; } } ``` 4. 在`GestureLockView`中创建一个数组`selectedArray`,用于存储用户选择的圆点。 ``` @property (nonatomic, strong) NSMutableArray *selectedArray; ``` 5. 在`GestureLockView`中实现手势识别的方法`touchesMoved:withEvent:`,通过判断触摸点是否在圆点内来确定用户选择的圆点,并绘制用户选择的线条。 ``` - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; for (GestureLockCircle *circle in self.circleArray) { if (CGRectContainsPoint(circle.frame, point) && !circle.selected) { circle.selected = YES; [self.selectedArray addObject:circle]; break; } } self.currentPoint = point; [self setNeedsDisplay]; } ``` 6. 在`GestureLockView`中实现绘制方法`drawRect:`,根据用户选择的圆点绘制线条。 ``` - (void)drawRect:(CGRect)rect { if (self.selectedArray.count == 0) { return; } UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = kLineWidth; path.lineJoinStyle = kCGLineJoinRound; path.lineCapStyle = kCGLineCapRound; [[UIColor whiteColor] set]; for (int i = 0; i < self.selectedArray.count; i++) { GestureLockCircle *circle = self.selectedArray[i]; if (i == 0) { [path moveToPoint:circle.center]; } else { [path addLineToPoint:circle.center]; } } [path addLineToPoint:self.currentPoint]; [path stroke]; } ``` 7. 在`GestureLockView`中实现手势结束的方法`touchesEnded:withEvent:`,判断用户手势是否正确,并通过代理方法通知外部。 ``` - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableString *password = [NSMutableString string]; for (GestureLockCircle *circle in self.selectedArray) { [password appendFormat:@"%ld", circle.tag]; } BOOL success = [password isEqualToString:self.password]; if (success) { for (GestureLockCircle *circle in self.selectedArray) { circle.selected = NO; } [self.selectedArray removeAllObjects]; [self setNeedsDisplay]; if (self.delegate && [self.delegate respondsToSelector:@selector(gestureLockView:didCompleteWithPassword:)]) { [self.delegate gestureLockView:self didCompleteWithPassword:password]; } } else { for (GestureLockCircle *circle in self.selectedArray) { circle.selected = NO; circle.error = YES; } [self.selectedArray removeAllObjects]; [self setNeedsDisplay]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kErrorDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ for (GestureLockCircle *circle in self.circleArray) { circle.error = NO; } [self setNeedsDisplay]; }); } } ``` 8. 在外部创建`GestureLockView`实例,并设置代理方法,实现手势解锁的逻辑。 ``` - (void)viewDidLoad { [super viewDidLoad]; GestureLockView *lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth)]; lockView.center = self.view.center; lockView.delegate = self; lockView.password = @"123456789"; [self.view addSubview:lockView]; } #pragma mark - GestureLockViewDelegate - (void)gestureLockView:(GestureLockView *)lockView didCompleteWithPassword:(NSString *)password { NSLog(@"password: %@", password); } ``` 至此,手势解锁的Demo已经完成了,你可以尝试在模拟器或真机上运行它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值