使用平移/滑动手势的UIView /抽屉

时间:2016-08-10 11:19:34

标签: ios objective-c uiview uigesturerecognizer uiviewanimation

我正在尝试cookie.Shareable = true; // Needed with Outputcache 打开&关闭Swipe / Pan Gesture&我从以下链接中找到了一些帮助 Link,它接近我想要制作的东西。

我希望UIView开启100像素默认&用户可以使用手势轻扫/平移UIView,直到父UIView&的75%回到100像素,但它在下面的代码中轻弹。我希望UIViewController的{​​{1}}位置为UIView,因此它可以像从顶部打开的抽屉。

X

如果UIView处于默认值(100像素),则有什么可以防止用户在顶部(向上)方向上平移0只能向下滑动到所需的- (void)viewDidLoad { [super viewDidLoad]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; drawerView = [self.storyboard instantiateViewControllerWithIdentifier:@"drawerVC"]; [drawerView.view setFrame:CGRectMake(0, -self.view.frame.size.height + 100, self.view.frame.size.width, self.view.frame.size.height * 0.75)]; [drawerView.view setBackgroundColor:[UIColor redColor]]; [drawerView.view addGestureRecognizer:panGesture]; } -(void)move:(UIPanGestureRecognizer*)recognizer { recognizer.view.center = CGPointMake(self.view.frame.size.width/2, recognizer.view.center.y + translation.y); [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; if (recognizer.state == UIGestureRecognizerStateEnded) { CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)); CGFloat slideMult = magnitude / 200; NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); float slideFactor = 0.1 * slideMult; // Increase for more of a slide CGPoint finalPoint = CGPointMake(0, recognizer.view.center.y + (velocity.y * slideFactor)); finalPoint.x = 0; finalPoint.y = MIN(MAX(finalPoint.y, 0), drawerView.view.frame.size.height*.75); if (fabs(recognizer.view.frame.origin.y) >= fabs(yOffset)) { return; } NSLog(@"ended %f",finalPoint.y); if (finalPoint.y < recognizer.view.frame.size.height/2) { // [self movePanelToOriginalPosition]; } else{ [self movePanelToCenterPosition]; } } } -(void)movePanelToCenterPosition { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ drawerView.view.frame = CGRectMake(0, 0, drawerView.view.frame.size.width, drawerView.view.frame.size.height); } completion:^(BOOL finished) { // Stuff to do }]; }

1 个答案:

答案 0 :(得分:0)

在你的行动中:你检查移动是否开始或正在进行的方法

else if(recognizer.state == UIGestureRecognizerStateBegin || recognizer.state == UIGestureRecognizerStateChanged)&#34;并且在if视图中处于极限。如果是这样,您可以禁用/重新启用手势识别器。这将取消平底锅...

- (void) move:(UIGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged)
    {
        BOOL shouldEnablePan = NO; // TODO: do some logic here to figure out if you want to allow pan

        if(!shouldEnablePan && [sender isKindOfClass:[UIPanGestureRecognizer class]])
        {
            sender.enabled = NO;
            sender.enabled = YES;
        }

    } else ...
}