触摸

  UIView支持触摸事件  因为继承于UIResponder,而且支持多点触摸,使用时需要定义UIView子类,实现触摸相关的方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;点击开始时执行此方法(多么见名知意)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 触摸结束时执行

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 在屏幕上滑动时执行

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;当触摸序列被打断时(如电话)执行

 

// 点击视图改变颜色

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    // touches 是一个集合 存储touche   

//    NSLog(@"%s %d", __func__, __LINE__);

//    NSLog(@"%@ ----- %d", touches, [touches count]);    

    UITouch *touch = [touches anyObject];// 某一个手指

    NSLog(@"%d", [touch tapCount]); // 点击次数

    NSLog(@"%f", [touch timestamp]);    

 

    self.backgroundColor = [UIColorcolorWithRed:(arc4random() % 256)/255.0green:(arc4random() % 256)/255.0blue:(arc4random() % 256)/255.0alpha:1.0];

    

    CGPoint location = [touch locationInView:self];

    NSLog(@"%.1f %.1f", location.x, location.y);

    NSLog(@"%@", touch.view);  // 触摸所在的视图

    NSLog(@"%@", touch.window); // 触摸事件所在的窗口

}

原文地址:https://www.cnblogs.com/NatureZhang/p/3669936.html