如何正确实现可拖动的“抽屉”自定义UIView

时间:2014-01-10 20:25:57

标签: ios uiview uiviewanimation touch-event

我的视图控制器底部有一个自定义UIView,用作“抽屉”控件。可以看到大约90个像素的视图。当用户点击视图时,它会向上动画显示其他选项,控件等。再次点击后,视图会回显到其原始位置。有效地“关闭抽屉”。

我还实现了代码,以便视图以拖动手势进行跟踪:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if(self.frame.origin.y >= 80) {
        UITouch *theTouch = [touches anyObject];
        CGPoint location = [theTouch locationInView:self];
        CGPoint previousLocation = [theTouch previousLocationInView:self];
        CGFloat target = location.y - previousLocation.y;
        self.frame = CGRectOffset(self.frame, 0, target);
    }
}

所有这一切都按预期工作。但是,视图无法正确响应快速“轻弹”手势。在touchesEnded:withEvent:方法中,我正在设置用户将手指抬到目的地的视图。如果用户拖慢然后释放,这很有效。但是如果它们快速轻弹,视图仍然需要处理0.5秒的持续时间,这对于眼睛来说是不平滑的。这是所有自定义视图的代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if(self.frame.origin.y >= 80) {
        UITouch *theTouch = [touches anyObject];
        CGPoint location = [theTouch locationInView:self];
        CGPoint previousLocation = [theTouch previousLocationInView:self];
        CGFloat target = location.y - previousLocation.y;
        self.frame = CGRectOffset(self.frame, 0, target);
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.frame.origin.y == kDefaultUpY) {
        _comingFromUp = YES;
        _comingFromDown = NO;
    }
    else if(self.frame.origin.y == kDefaultDownY) {
        _comingFromUp = NO;
        _comingFromDown = YES;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    //responding to tap
    if(self.frame.origin.y == kDefaultUpY) {
        [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
            self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height);
        } completion:nil];

        return;
    }
    else if(self.frame.origin.y == kDefaultDownY) {
        [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
            self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height);
        } completion:nil];

        return;
    }

    //responding to drag
    if(_comingFromDown) {
        if(self.frame.origin.y < kDefaultDownY) {
            //dragging up -- go up
            [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height);
            } completion:nil];
        }
        else if(self.frame.origin.y > kDefaultDownY) {
            //dragging down -- stay down
            [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height);
            } completion:nil];
        }
    }
    else if(_comingFromUp) {
        if(self.frame.origin.y < kDefaultUpY) {
            //dragging up -- stay up
            [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height);
            } completion:nil];
        }
        else if(self.frame.origin.y > kDefaultUpY) {
            //dragging down -- go down
            [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height);
            } completion:nil];
        }
    }
}

如何正确回应与“抽屉”视图相关的手势?是否有实现此类功能的标准方法?

1 个答案:

答案 0 :(得分:2)

使用手势识别器而不是自己实现触摸处理代码。您可以同时附加滑动手势识别器和平移手势识别,并设置平移手势,以便在触发平移之前滑动必须失败。使滑动手势的动画更快。像0.1或0.2秒这样的东西可能适合滑动手势。

值得注意的是:在iOS 7中,屏幕底部的滑动手势会触发系统设置抽屉从屏幕底部显示。我们的应用程序Face Dancer有一个控制抽屉,我们最近发现在iOS 7下很难将控件抽屉滑出而不会触发系统设置抽屉。我们有一个用于拖动的“拇指”区域,我们正在添加一个轻敲手势,它将在一个步骤中完全显示/隐藏它。