ios學習之UISwipeGestureRecognizer手勢識別

來源:互聯網
上載者:User

標籤:

tap是指輕觸手勢。類似滑鼠操作的點擊。從iOS 3.2版本開始支援完善的手勢api:

 
  • tap:輕觸
  • long press:在一點上長按
  • pinch:兩個指頭捏或者放的操作
  • pan:手指的拖動
  • swipe:手指在螢幕上很快的滑動
  • rotation:手指反向操作
 

- (void)viewDidLoad

 

{

 [superviewDidLoad];

    infoView=[[UIViewalloc] initWithFrame:CGRectMake(20, 300, 768-400, 70)]; 

    infoView.backgroundColor=[UIColorblueColor]; 

    infoView.alpha=0.6; 

    infoView.layer.cornerRadius=6; 

    infoView.layer.masksToBounds=YES; 

    [self.view addSubview:infoView]; 

   /******************監視手勢控制*****************/

    UISwipeGestureRecognizer *recognizer; 

    

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 

    [[selfview] addGestureRecognizer:recognizer]; 

    [recognizer release]; 

    

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; 

    [[selfview] addGestureRecognizer:recognizer]; 

    [recognizer release]; 

    

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 

    [[selfview] addGestureRecognizer:recognizer]; 

    [recognizer release]; 

    

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 

    [[selfview] addGestureRecognizer:recognizer]; 

    [recognizer release]; 

}

 

/******************手勢控制操作及切換特效*****************/

//滑動事件1

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{

    //如果往左滑

    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {

        //先載入資料,再載入動畫特效

        [self nextQuestion];

        self.view.frame = CGRectMake(320, 0, 320, 480);

        [UIViewbeginAnimations:@"animationID"context:nil];

        [UIViewsetAnimationDuration:0.3f];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

        [UIViewsetAnimationRepeatAutoreverses:NO];

        self.view.frame = CGRectMake(0, 0, 320, 480);

        [UIViewcommitAnimations];

    }

    //如果往右滑

    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

        [self lastQuestion];

        self.view.frame = CGRectMake(-320, 0, 320, 480);

        [UIViewbeginAnimations:@"animationID"context:nil];

        [UIViewsetAnimationDuration:0.3f];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

        [UIViewsetAnimationRepeatAutoreverses:NO];

        self.view.frame = CGRectMake(0, 0, 320, 480);

        [UIViewcommitAnimations];

    }

}

//滑動觸發事件2

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 

    NSLog(@"Swipe received."); 

    

    if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) { 

        NSLog(@"swipe down");

 

        [UIViewbeginAnimations:@"animationID"context:nil]; 

        [UIViewsetAnimationDuration:0.7f]; 

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut]; 

        [UIViewsetAnimationRepeatAutoreverses:NO]; 

        [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.viewcache:YES];

        [infoViewremoveFromSuperview]; 

        [self.view addSubview:infoView];

        [UIViewcommitAnimations];

    }

    

    if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) { 

        NSLog(@"swipe up");

        

        [UIViewbeginAnimations:@"animationID"context:nil]; 

        [UIViewsetAnimationDuration:0.7f]; 

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut]; 

        [UIViewsetAnimationRepeatAutoreverses:NO]; 

        [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.viewcache:YES];

        [infoViewremoveFromSuperview]; 

        [self.view addSubview:infoView];

        [UIViewcommitAnimations];

    }

}

//點擊出發事件

-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{

    NSLog(@">>>tap it");

}

ios學習之UISwipeGestureRecognizer手勢識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.