关闭

tabview 增加手势判断UIPanGestureRecognizer

1701人阅读 评论(0) 收藏 举报
- (void)awakeFromNib{
 UIPanGestureRecognizer* recognizer;
    // handleSwipeFrom 是偵測到手势,所要呼叫的方法
    recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    // 不同的 Recognizer 有不同的实体变数
    // 例如 SwipeGesture 可以指定方向
    // 而 TapGesture 則可以指定次數
    //recognizer.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    recognizer.delegate = self;
    [_tableView addGestureRecognizer:recognizer];
    [recognizer release];
}
 
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translation = [gestureRecognizer translationInView:[[UIApplication sharedApplication] keyWindow]];
     
    // Check for horizontal gesture
    if (sqrt(translation.x * translation.x) / sqrt(translation.y * translation.y) > 1)
    {
        return YES;
    }
     
    return NO;
}
 
- (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer {
 
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        ;
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGFloat xOffSet = [recognizer translationInView:[[UIApplication sharedApplication] keyWindow]].x;
         
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if (xOffSet < 0) {
            [appDelegate makeRightViewVisible];
        }
        else if (xOffSet > 0) {
            [appDelegate makeLeftViewVisible];
        }
         
        self.navigationController.view.frame = CGRectMake(xOffSet,
                                                          self.navigationController.view.frame.origin.y,
                                                          self.navigationController.view.frame.size.width,
                                                          self.navigationController.view.frame.size.height);
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (self.navigationController.view.frame.origin.x < -kTriggerOffSet)
            [self moveToLeftSide];
        // animate to right side
        else if (self.navigationController.view.frame.origin.x > kTriggerOffSet)
            [self moveToRightSide];
        // reset
        else
            [self restoreViewLocation];
 
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1104191次
    • 积分:15281
    • 等级:
    • 排名:第721名
    • 原创:405篇
    • 转载:193篇
    • 译文:23篇
    • 评论:84条
    博客专栏
    文章分类