简体   繁体   English

使用平移/滑动手势的UIView / Drawer

[英]UIView/Drawer using Pan/Swipe Gesture

I am trying to make a UIView open & close on Swipe/Pan Gesture & i found some help from following link Link , it's close to what im trying to make. 我试图使UIView在Swipe / Pan Gesture上打开和关闭,我从下面的链接Link中找到了一些帮助,这与我要制作的内容很接近。

I want UIView to be open by 100 pixels default & User can swipe/pan the UIView using gesture till 75% of the parent UIViewController & back to 100 pixels but it's flicking in this below code. 我希望UIView默认以100像素打开,并且用户可以使用手势滑动/平移UIView直到父UIViewController 75%并返回100像素,但这在下面的代码中有所变化。 I want UIView 's X position to be 0 so it can be like a drawer opening from top. 我希望UIViewX位置为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
                     }];
}

Is there anything that can prevent user to pan UIView in Top(Up) direction if UIView is at default(100 pixels) & can only swipe down to desired CGPoint . 如果UIView默认(100像素)并且只能向下滑动到所需的CGPoint ,是否有什么可以阻止用户向顶部(向上)方向平移UIView

In your move: method you check if the move start or is in progress 在move:方法中,您检查移动是否开始或正在进行中

else if (recognizer.state == UIGestureRecognizerStateBegin || recognizer.state == UIGestureRecognizerStateChanged)" and in the if view is at its limit. if so you can disabled/reenabled the gesture recognizer. This will cancel the pan... else if(recognizer.state == UIGestureRecognizerStateBegin || ognir.state == UIGestureRecognizerStateChanged)“,并且在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 ...
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM