Touch Demo

#import "ViewController.h"

//定义两个全局变量View视图 

@interface ViewController () {

    UIView *_view1;

    UIView *_view2;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    /*

    //创建子视图

    _view = [[UIView alloc] initWithFrame:CGRectMake(90, 90, 20, 20)];

    _view.backgroundColor = [UIColor greenColor];

    [self.view addSubview:_view];

     */

    

    _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 90, 375, 120)];

    _view1.backgroundColor = [UIColor greenColor];

    [self.view addSubview:_view1];

    

    _view2 = [[UIView alloc] initWithFrame:CGRectMake(375, 90, 375, 120)];

    _view2.backgroundColor = [UIColor redColor];

    [self.view addSubview:_view2];

    

    

    //开启多点触摸,这个不能忘记

    self.view.multipleTouchEnabled = YES;

    

    //开启响应

    self.view.userInteractionEnabled = YES;

}

//触摸开始

//touches:手指数  event:点击事件

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

    //取得touch

    UITouch *touch = [touches anyObject];

    

    //(1)取得触摸产生的时候所处的窗口

    UIWindow *window = touch.window;

    //取得当前的主窗口,

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

    //或者

    keyWindow = self.view.window;

//    NSLog(@"window:%@   keyWindow:%@",window,keyWindow);    //地址一样,说明取得是主窗口

    

    //(2取得触摸所处的视图

    UIView *view = touch.view;

//    NSLog(@"view:%@    self.view:%@",view,self.view);

    

    //(3)点击的次数

    NSInteger count = touch.tapCount;

    //区别单击和双击手势:延迟调用单击,在一定时间以内如果执行双击,则取消单击事件

    if (count == 1) {

//        [self singleTap];

//延时调用

         [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.2];

    }else if (count == 2) {

//        [self doubleTap];

        //取消单击

        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];

        //执行双击

        [self doubleTap];

    }

    

    //声明周期

//    touch.phase

    

    //(4)获取点击时的坐标

    CGPoint point = [touch locationInView:touch.view];//touch比self好

//    NSLog(@"point:%@",NSStringFromCGPoint(point));

}

//单击事件

- (void)singleTap {

    NSLog(@"单击了");

}

//双击了

- (void)doubleTap {

    NSLog(@"双击了");

}

//触摸移动

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

    //取得手指的坐标(当前位置)

//    UITouch *touch = [touches anyObject];

//    CGPoint point = [touch locationInView:touch.view];

//    _view.center = point;

    

    //获取前一个位置坐标

    UITouch *touch = [touches anyObject];

    CGPoint previousPoint = [touch previousLocationInView:touch.view];

    

    //获取当前的

    CGPoint currentPoint = [touch locationInView:touch.view];

    

    //获取水平方向的差值

    CGFloat subX = currentPoint.x - previousPoint.x;

    

    _view1.frame = CGRectMake(CGRectGetMinX(_view1.frame)+subX, 90, 375, 120);

    _view2.frame = CGRectMake(CGRectGetMinX(_view2.frame)+subX, 90, 375, 120);

    

}

//触摸结束

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

//    NSLog(@"touchesEnded");

}

//触摸中断(取消)

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

//    NSLog(@"touchesCancelled");

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

file:///Users/mac/Desktop/屏幕快照%202015-09-17%20上午8.56.52.png

原文地址:https://www.cnblogs.com/answer-Allen/p/4815277.html