手勢識別的 創建 與 方法

    /// 手勢識別器

    // 1. 輕拍手勢

    // 手勢需要在定義是綁定一個觸發方法(SEL)

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

//    

//    // 輕拍的設置

//    // 需要輕拍兩次 才響應事件

//    tap.numberOfTapsRequired = 2;

//    // 需要幾根手指 才響應事件

//    tap.numberOfTouchesRequired = 2;

//    // 給view添加一個手勢

//    [imageView addGestureRecognizer:tap];

//    [tap release];

    

    

    

    // 2. 長按手勢(longPress)

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    // 長按 觸發方法 需要時間

    longPress.minimumPressDuration = 2;

    // 長按時 允許用戶移動手指的距離

    longPress.allowableMovement = 100;

    

    [imageView addGestureRecognizer:longPress];

    [longPress release];

    

    

    

    

    // 3. 清掃手勢(swipe)

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

//    // 設置清掃方向

//    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

//    

//    [imageView addGestureRecognizer:swipe];

//    [swipe release];

    

    

    

    

//    // 4.拖拽手勢(pan)

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

//    

//    [imageView addGestureRecognizer:pan];

//    [pan release];

//    

//    

//    

//    

//    // 5.旋轉(rotation)

//    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

//    

//    [imageView addGestureRecognizer:rotation];

//    [rotation release];

//    

//    

//    

//    

//    // 6. 捏合手勢(pinch)

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

//    

//    [imageView addGestureRecognizer:pinch];

//    [pinch release];

    

    

    

    // 7. 屏幕邊緣的拖拽

    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenPan:)];

    // 設置監測那一邊的屏幕邊緣

    screenEdgePan.edges = UIRectEdgeLeft;

    [imageView addGestureRecognizer:screenEdgePan];

    [screenEdgePan release];

    

    // 將UIImageView的用戶交互打開,使它能響應輕拍

    [imageView setUserInteractionEnabled:YES];

}


- (void)screenPan:(UIScreenEdgePanGestureRecognizer *)screenPan


{

    NSLog(@"邊緣拖拽");

}


// 捏合的觸發方法

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    NSLog(@"捏合");

    // 獲取當前的view

    UIImageView *imageView = (UIImageView *)pinch.view;

    // 在x,y軸方向 放大/縮小

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;  // 放大縮小的尺度(速度)

}


// 旋轉方法

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

    NSLog(@"旋轉");

    // 獲取到當前手勢添加到的view

    UIImageView *imageView = (UIImageView *)rotation.view;

    // 讓view旋轉 利用旋轉手勢的旋轉弧度

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

    

    rotation.rotation = 0;

}


// 拖拽的觸發方法

- (void)panAction:(UIPanGestureRecognizer *)pan

{

    NSLog(@"拖拽");

    // 通過手勢的view屬性 獲取到當前手勢添加到的 view

    UIImageView *imageview = (UIImageView *)pan.view;

    

    // 獲取到當前手指接觸的點

    CGPoint p = [pan translationInView:imageview];

    

    // 讓view變形

    imageview.transform = CGAffineTransformTranslate(imageview.transform,p.x, p.y);

    

    // 重置手勢的屬性

    [pan setTranslation:CGPointZero inView:imageview];

}


// 清掃的觸發方法

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe

{

    NSLog(@"清掃");

}

// 輕拍的觸發方法

- (void)tapAction:(UITapGestureRecognizer *)tap

{

    NSLog(@"輕拍");

    

}


// 長按的觸發方法

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress


{

    if (longPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"長按");

    }

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


發佈了27 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章