iOS之觸摸事件和手勢

一.事件

iOS中ViewController自身提供了一些觸發手指觸摸事件的方法,在這些觸發的方法中我們可以實現自己想要的操作.這些方法如下

1.觸摸開始方法

//開始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"你觸摸了屏幕");
}

2.觸摸結束方法

//結束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"觸摸結束");
}

3.觸摸移動手指方法

//移動
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"你移動了手指");
}

4.觸摸中斷方法

//中斷
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"觸摸中斷");
}

5.搖晃手機觸發的方法

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"開始搖晃");
   
}

6.搖晃結束

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"搖晃結束");
}

二.手勢

iOS還提供了一系列的手勢來添加到其他空間上實現不同的效果

1.點擊手勢

//點擊手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchMe)];
    //設置點擊次數
    //tap.numberOfTouchesRequired = 3
    tap.numberOfTapsRequired = 3;
    [view addGestureRecognizer:tap];

    -(void)touchMe
    {
        NSLog(@"touch");
    }

2.長按手勢

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMe:)];
    [view addGestureRecognizer:longPress];
    //控制允許滑動的距離
    longPress.allowableMovement = 110;
    //設置長按的時間
    longPress.minimumPressDuration = 2;

-(void)longPressMe:(UILongPressGestureRecognizer *)longress
{
    if (longress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"長按開始");
    }else if(longress.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"滑動");
    }else if(longress.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"滑動結束");
    }
    //NSLog(@"長按");
}

3.輕掃手勢

    //輕掃手勢
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeMe:)];
    //設置清掃的方向
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [view addGestureRecognizer:swipe];

-(void)swipeMe:(UISwipeGestureRecognizer *)swipec
{
    if (swipec.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"鄉下");
    } else if(swipec.direction ==UISwipeGestureRecognizerDirectionUp){
        NSLog(@"向上");
    }
    NSLog(@"掃啥掃");
}



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