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

    }

    

}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章