hibou 主界面自己侧滑的定义

要打滑View参加UIPanGestureRecognizer


#pragma mark 手势识别器回调方法

- (void)dragView:(UIPanGestureRecognizer *)gesture{

    switch (gesture.state) {

        case UIGestureRecognizerStateBegan:

            [self startDrag];

            break;

            

        case UIGestureRecognizerStateEnded:

            [self endDrag];

            break;

        default:

            [self Draging:gesture];

            break;

    }

}


#pragma mark 開始拖动,记录下開始的x

- (void)startDrag{

    CGRect frame = _mainView.frame;

    _lastDownX = frame.origin.x;

}


#pragma mark 正在拖动,View跟着动

- (void)Draging:(UIPanGestureRecognizer *)gesture{

    CGFloat maxX = [UIScreen mainScreen].bounds.size.width * kSlideRatio;

    CGPoint transform = [gesture translationInView:_mainView];

    

    CGRect frame = _mainView.frame;

    frame.origin.x = _lastDownX + transform.x;

    

    if (frame.origin.x < 0) {   //不能够移动到屏幕左边去

        frame.origin.x = 0;

    }else if(frame.origin.x > maxX){

        frame.origin.x = maxX;  //限制移动的最右边

    }

    _mainView.frame = frame;

    

    // 设置界面scale动画

    CGFloat tx = frame.origin.x;

    if (tx < 0) {

        tx = 0;

    }

    CGFloat txScale = tx / _settingsViewController.view.frame.size.width;

    // imageview缩放

    double scale = kDefaultSettingScale + txScale * (1 - kDefaultSettingScale);

    if (scale > 1){

        scale = 1;

    }

    

    CGAffineTransform cgTransform = CGAffineTransformMakeScale(scale, scale);

    [_settingsViewController setAnimationScale:cgTransform];

}


#pragma mark 拖动结束,推断要往哪边滚动

- (void)endDrag{

    int maxX = [self getSettingRight];

    int currentX = _mainView.frame.origin.x;

    if (currentX < maxX * 0.5) {    //没到一半,回到左边

        currentX = 0;

    }else{      //回到右边

        currentX = maxX;

    }

    

    CGRect frame = _mainView.frame;

    frame.origin.x = currentX;

    [self scrollMainViewToFrame:frame withDuration:0.25];

}


版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-09-24 14:54  hrhguanli  阅读(260)  评论(0编辑  收藏  举报