首页 > 解决方案 > iOS:如何将视图与手指一起拖动到特定的 y 偏移?

问题描述

我正在使用平移手势从下到上拖动具有预定义偏移位置的视图。

我当前的实现是在手势结束后视图更新。但是,我希望视图与手指一起更新(手势正在进行)并在手势结束时停在预定义的(下一个)位置。

如果有人能指出我正确的方向,将不胜感激。谢谢你。

m_openPercentage = @"30,60";

- (void)onContentViewPanned:(UIPanGestureRecognizer *)gesture
{
    CGPoint velocity = [gesture velocityInView:self.view];
    NSArray *m_Openpercentage = [m_OpenPercentage componentsSeparatedByString:@","];
    NSMutableArray *fullpercentage = [NSMutableArray arrayWithArray:m_Openpercentage];
    float diffy = 0.0;
    if(([gesture state] == UIGestureRecognizerStateEnded))
        {
            if(velocity.y < 0)
            {
                if(m_isfullscreen && ![fullpercentage containsObject:@"100"])
                {
                    [fullpercentage addObject:@"100"];
                }
                diffy = 1-([[fullpercentage objectAtIndex:counter] floatValue]/100);
                CGFloat ypos = self.view.frame.size.height * diffy;
                [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
                    [self.view setNeedsLayout];
                    self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
                    if(self.view.frame.origin.y==0)
                        [self showNavigationbar:1];
                } completion:^(BOOL finished) {
                }];

                if(counter<[fullpercentage count]-1)
                    counter++;


            }
            else
            {
                    if(counter>0)
                {
                    float diffy = 1-([[fullpercentage objectAtIndex:counter-1] floatValue]/100);
                    CGFloat ypos = self.view.frame.size.height * diffy;

                    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
                        self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
                    } completion:^(BOOL finished) {


                    }];
                }
                else
                {
                    diffy = 1-(m_initialPercentage/100);
                    CGFloat ypos = self.view.frame.size.height * diffy;

                    if(self.view.frame.origin.y < ypos)
                    {

                    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
                        self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
                    } completion:^(BOOL finished) {
                    }];
                    }
                    else
                    {
                        [self removeoverlayview];
                        [self.view removeFromSuperview];
                        id tmpControllerObj = [parentController getParentController];
                        [tmpControllerObj view].userInteractionEnabled = YES;
                    }
                }

                if((counter-1)>=0)
                    counter--;
                [self showNavigationbar:0];
            }
            if(![m_Openpercentage containsObject:@"100"] && m_isfullscreen)
                [self updatefullview];
        }

}

标签: objective-cuipangesturerecognizersmooth-scrolling

解决方案


推荐阅读