按照手指滑动UIView,类似于iOS中的Control Center

时间:2016-01-27 23:15:33

标签: ios objective-c uitableview uiview uipangesturerecognizer

我的UITableView位于我的主UIViewController的底部,它目前只显示前两行。为了显示其余的我不希望它滚动,我希望用户能够“拉”视图以显示额外的4行(然后可以向下滑动以将其“推”回原来的位置),我希望“拉”与控制中心在iOS中的工作方式类似。它的弹簧非常棒。

看起来我可以添加UIPanGestureRecognizer进行拉动:

UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];

- (void)pan:(UIPanGestureRecognizer *)aPan; {
  CGPoint currentPoint = [aPan locationInView:self];

  [UIView animateWithDuration:0.01f
                   animations:^{
                     CGRect oldFrame = _viewToChange.frame;
                     _viewToChange.frame = CGRectMake(oldFrame.origin.x, currentPoint.y, oldFrame.size.width, ([UIScreen mainScreen].bounds.size.height - currentPoint.y));
                   }];
}

Ref this question

执行此操作 有点工作,但是当你提起它时我的UITableView会闪烁,它最终会消失。

也没有弹簧,也无法设置“最大”,因此您无法将视图拉过某一点。

有没有人对如何实现这一点有所了解?

1 个答案:

答案 0 :(得分:3)

这是我写pan:的方式。 t是tableView(最初禁用用户交互)。代码中的40代表pan更逼真。 200是我使用的最大值,60是最小值。我直接将t添加到高度为60的tableview,并通过动画增加高度。

    - (void)pan:(UIPanGestureRecognizer *)aPan; {
    CGPoint currentPoint = [aPan locationInView:self.view];

    CGRect fr = t.frame;

    if ((currentPoint.y - fr.origin.y) < 40 && (fr.size.height <= 200) )
    {
        float nh = (currentPoint.y >= self.view.frame.size.height - 200) ? self.view.frame.size.height-currentPoint.y : 200;
        if (nh < 60) {
            nh = 60;
        }
        [UIView animateWithDuration:0.01f animations:^{
            [t setFrame:CGRectMake(0, self.view.frame.size.height-nh, t.frame.size.width, nh)];
        }];
        if (nh == 200) {
            [t setUserInteractionEnabled:YES];
        }
        else
        {
            [t setUserInteractionEnabled:NO];
        }
    }

}

或没有锅;使用tableview的scrollview委托方法,当tableview开始在关闭状态下拖动时,打开大模式,当tableview滚动到顶部滑动

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scroll");
    if (scrollView.contentOffset.y == 0) {
        [UIView animateWithDuration:0.2f animations:^{
            [scrollView setFrame:CGRectMake(0, self.view.frame.size.height-200, t.frame.size.width, 200)];
        }];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y == 0) {
        [UIView animateWithDuration:0.2f animations:^{
            [scrollView setFrame:CGRectMake(0, self.view.frame.size.height-60, t.frame.size.width, 60)];
        }];
    }
}