从屏幕右侧滑出viewcontroller

时间:2014-03-21 09:18:15

标签: ios objective-c ecslidingviewcontroller-2

我想从主视图控制器的右侧创建一个滑出菜单。但是我希望滑出菜单显示在主视图的顶部而不是将其推到一侧。

我看过ECSlidingViewController 2,它允许从任何一侧滑出,但是滑出的菜单在暴露的边缘上显得切断。它创建顶视图控制器向左或向右移动的效果,以显示其下的隐藏菜单。我想让它看起来滑出的隐藏菜单进入主视图控制器,我们可以看到它的左边缘,所以没有内容被切断。

这是否可能,ECSlidingViewController 2是否适用于此?

修改

这就是我的尝试:

检测触摸开始:

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

    BBAppDelegate *myAppDelegate = (BBAppDelegate*)[UIApplication sharedApplication].delegate;

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    NSArray *myViewArray = [[NSBundle mainBundle] loadNibNamed:@"FilterView" owner:self options:nil];
    self.filterView = myViewArray[0];


    [myAppDelegate.window addSubview:self.filterView];

    if (CGRectContainsPoint(self.filterView.frame, touchLocation)) {

        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}

然后拖动:

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

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (dragging) {

        CGRect frame = self.filterView.frame;
        frame.origin.x = self.filterView.frame.origin.x + touchLocation.x;
        frame.origin.y =  self.filterView.frame.origin.y + touchLocation.y;
        self.filterView.frame = frame;
    }
}

但是,当我开始触摸时,filterView会替换所有视图,并且无论如何都不会滑动或动画。我虽然这是因为没有提供一个起点 - 所以我设置了self.filterView就像这样:

self.filterView = [[UIView alloc] initWithFrame:CGRectMake(0,0,120,564)];

但是,在显示视图时,更改这些值对视图的位置/大小没有影响。

修改2

好的,我开始了一个新项目进行测试,并设法通过跟踪我的屏幕上的视图来移动。

我正在使用UIPanGestureRecognizer

像这样:

viewDidLoad

- (void)viewDidLoad

{     [super viewDidLoad];

NSArray *myViewArray = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
UIView *myView = myViewArray[0];

self.filterView = myView;

self.filterView.frame = CGRectMake(100, 100, 200, 200);

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self
                                                                     action:@selector(dragging:)];
pan.delegate = self;

[self.filterView addGestureRecognizer:pan];


[self.view addSubview:self.filterView];

}

然后在拖动方法中:

-(void)dragging:(UIPanGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan)
        {
            NSLog(@"Received a pan gesture");
        self.panCoord = [gesture locationInView:gesture.view];


        }
    CGPoint newCoord = [gesture locationInView:gesture.view];
    float dX = newCoord.x-self.panCoord.x;


    gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);
}

这允许视图仅水平移动。

几乎就在那里 - 我现在要弄清楚的是当用户抬起手指时它会抓住一个位置。以及如何在其开始位置偏离屏幕时移动此视图。

编辑3

好的,所以我想控制出现在屏幕上的视图的速度和速度。如果用户快速滑动,则视图以他们滑动的速度到达。如果他们慢慢滑动,它会缓慢地进入。但是我也设置了最低时间 - 所以如果他们需要很长时间 - 它默认为最小量。

以下是代码:

{
   float start;
    float end;

    NSDate *methodStart;
    NSDate *methodEnd;

}



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

methodStart = [NSDate date];

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];

start = location.x;}


-(void)dragging:(UIPanGestureRecognizer *)gesture{

if(gesture.state == UIGestureRecognizerStateBegan)
    {

    self.panCoord = [gesture locationInView:gesture.view];


    }

CGPoint newCoord = [gesture locationInView:gesture.view];
float dX = newCoord.x-self.panCoord.x;





gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);


if (gesture.state == UIGestureRecognizerStateEnded){

    methodEnd = [NSDate date];
    NSTimeInterval executionTime = [methodEnd timeIntervalSinceDate:methodStart];


    double time = executionTime;

    float distance = start - end;


    int distanceOverTime = distance / time;
    float veloticty = distanceOverTime / 100;

    double miniTimeToUse = 0.5;
    double newMinTime;
    if (time < miniTimeToUse){

        newMinTime =  miniTimeToUse;
    }else {
        newMinTime = time;
    }


    if (gesture.view.frame.origin.x+dX >= 180){

        [UIView animateWithDuration:newMinTime
                              delay:0
             usingSpringWithDamping:0.9
              initialSpringVelocity:veloticty
                            options:0
                         animations:^{

                             self.filterView.frame = CGRectMake(300, 100, 200, 200);

                         } completion:nil];

    }
    if (gesture.view.frame.origin.x+dX <= 181){

    [UIView animateWithDuration:newMinTime
    delay:0
    usingSpringWithDamping:0.9
    initialSpringVelocity:veloticty
    options:0
    animations:^{

        self.filterView.frame = CGRectMake(50, 100, 200, 200);

    } completion:nil];


    }
}

}

现在只是为了让它从侧面拉出视图。

0 个答案:

没有答案