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(@"结束");

    }

    

}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值