IOS触摸与手势

一:触摸

//触摸开始

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

   //UITouch的常用属性

    UITouch *touch = [touches anyObject];

    

    //点击的次数

    NSLog(@"tap count: %ld", touch.tapCount);

    

    //触摸的阶段

    NSLog(@"phase: %ld", touch.phase);

    

    //在视图上的位置坐标

    CGPoint point = [touch locationInView:self];

    NSLog(@"location in view: %@", NSStringFromCGPoint(point));

    

    CGPoint windowPoint = [touch locationInView:[UIApplication sharedApplication].keyWindow];

    

    NSLog(@"location in window: %@", NSStringFromCGPoint(windowPoint));

    

    //在视图上前一个点的坐标

    CGPoint prePoint = [touch previousLocationInView:self];

    NSLog(@"previous location in view: %@", NSStringFromCGPoint(prePoint));


}


//触摸移动

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

{

//    NSLog(@"触摸移动");

    //UITouch的常用属性

    UITouch *touch = [touches anyObject];

    

    //在视图上的位置坐标

    CGPoint point = [touch locationInView:self];

    NSLog(@"location in view: %@", NSStringFromCGPoint(point));

    

    //在视图上前一个点的坐标

    CGPoint prePoint = [touch previousLocationInView:self];

    NSLog(@"previous location in view: %@", NSStringFromCGPoint(prePoint));

}



//触摸结束

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


  NSLog(@"触摸结束");


}


//触摸被取消

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

{

     NSLog(@"触摸被取消");

}

 

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

{

    UITouch *touch = [touches anyObject];

    

    if (touch.tapCount == 1) {

        //单击

        //[self singleTap];

        [self performSelector:@selector(singleTap)

                   withObject:nil

                   afterDelay:0.2];

    } else if (touch.tapCount == 2) {

        //双击

        [NSObject cancelPreviousPerformRequestsWithTarget:self

                                                 selector:@selector(singleTap)

                                                   object:nil];

        [self doubleTap];

    }

    

}


- (void)singleTap

{

    NSLog(@"单击");

}


- (void)doubleTap

{

    NSLog(@"双击");

}


//运动事件

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

{

    NSLog(@"摇一摇开始");

}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

    NSLog(@"摇一摇结束");

}


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

{

    

}


//远程控制事件

- (void)remoteControlReceivedWithEvent:(UIEvent *)event

{

    

}


二:手势

   //1.轻击

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    

    tap.numberOfTapsRequired = 2;

    tap.numberOfTouchesRequired = 3;

    

    [self.view addGestureRecognizer:tap];

    

    //2.捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

    [self.view addGestureRecognizer:pinch];

    

    //3.拖移

    //UISwipeGestureRecognizer

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction)];

    [self.view addGestureRecognizer:pan];

//

    //4.轻扫

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];

    [self.view addGestureRecognizer:swipe];

    

    

    //5.长按

    //UILongPressGestureRecognizer

    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressAction:)];

    [self.view addGestureRecognizer:press];

    

    //6.旋转

    //UIRotationGestureRecognizer

}


- (void)tapAction:(UITapGestureRecognizer *)sender

{

    NSLog(@"tap");

}


- (void)pinchAction:(UIPinchGestureRecognizer *)sender

{

    NSLog(@"");

}


- (void)panAction

{

    NSLog(@"pan");

}


- (void)swipeAction

{

    NSLog(@"swipe");

}


- (void)pressAction:(UILongPressGestureRecognizer *)sender

{

    NSLog(@"press");

    

    if(sender.state == UIGestureRecognizerStateBegan)

    {

        NSLog(@"开始");

    } else if (sender.state == UIGestureRecognizerStateChanged)

    {

        NSLog(@"移动");

    } else if (sender.state == UIGestureRecognizerStateEnded)

    {

        NSLog(@"结束");

    }

    

}







你可能感兴趣的:(触摸与手势)