各種手勢使用

Touch觸摸
// 碰到屏幕就會觸發(fā)該方法

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"觸碰到了");
    UITouch *touch = [touches anyObject];
    // 獲取點(diǎn)擊坐標(biāo)
    CGPoint point = [touch locationInView:self.view];
    NSLog(@"%f, %f", point.x, point.y);
    }
    Tap點(diǎn)按
    UITapGestureRecognizer *doubleTgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTagr:)];
    // 點(diǎn)擊的次數(shù)
    //doubleTgr.numberOfTapsRequired = 2;
    [self.imageView addGestureRecognizer:doubleTgr];
    LongPress長按
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgrStart:)];
    [self.imageView addGestureRecognizer:lpgr];
    Swipe輕掃
    // 由于輕掃只能識別一個方向,所以把他的四個方向放在一個數(shù)組里留著以后辨別,這樣就能識別四個方向了
    NSArray *arr = @[[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionDown],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionUp],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionLeft],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionRight]];
    for (int i=0; i<arr.count; i++) {
    UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiper:)];
    //把方向取出來
    NSNumber *direction = arr[i];
    // 把方向轉(zhuǎn)為Integer類型賦給輕掃的方向
    swiper.direction = [direction unsignedIntegerValue];
    [self.imageView addGestureRecognizer:swiper];
    }
  • (void)swiper:(UISwipeGestureRecognizer *)swiper
    {
    if (swiper.direction == UISwipeGestureRecognizerDirectionDown) {
    NSLog(@"向下");
    }
    if (swiper.direction & UISwipeGestureRecognizerDirectionLeft) {
    NSLog(@"向左");
    }
    if (swiper.direction & UISwipeGestureRecognizerDirectionRight) {
    NSLog(@"向右");
    }
    if (swiper.direction & UISwipeGestureRecognizerDirectionUp) {
    NSLog(@"向上");
    }
    }
    Rotation旋轉(zhuǎn)
    // 創(chuàng)建一個手勢
    UIRotationGestureRecognizer *rotion = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotion:)];
    // 手勢添加在控件上
    [self.imageView addGestureRecognizer:rotion];
  • (void)rotion:(UIRotationGestureRecognizer *)rotion
    {
    NSLog(@"%f", rotion.rotation);
    rotion.view.transform = CGAffineTransformRotate(rotion.view.transform, rotion.rotation);
    // 由于此方法是旋轉(zhuǎn)過程中反復(fù)調(diào)用的,所以旋轉(zhuǎn)弧度很容易疊加,要及時清零才正常一點(diǎn)
    rotion.rotation = 0;
    }
    Pinch捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [self.imageView addGestureRecognizer:pinch];
  • (void)pinch:(UIPinchGestureRecognizer *)pinch
    {
    // 放大縮小的比例
    NSLog(@"%f", pinch.scale);

    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 把伸縮比例及時變?yōu)?,防止伸縮效果疊加
    pinch.scale = 1;
    }
    Pan拖拽
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.imageView addGestureRecognizer:pan] ;

  • (void)pan:(UIPanGestureRecognizer *)pan
    {
    // 記錄原來的位置
    static CGPoint center;

    if (pan.state == UIGestureRecognizerStateBegan) {
    center = pan.view.center;
    }

    // 獲取偏移量
    CGPoint point = [pan translationInView:self.view];
    // 最后拖拽手勢所在view的中心點(diǎn),就是原來的位置加上變動的位置
    pan.view.center = CGPointMake(center.x + point.x, center.y + point.y);
    }
    Double手勢疊加
    //可以添加多個手勢
    // 一個控件可以添加多個手勢,但一個手勢不能添加在多個控件上
    添加多個手勢時要遵守<UIGestureRecognizerDelegate>協(xié)議

// 返回yes,允許同時添加多個手勢

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
    return YES;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容