简体   繁体   English

左右滑动UIView

[英]Sliding a UIView left and right

I'm trying to animate a UIView to slide left and right. 我正在尝试动画UIView左右滑动。 What I've tried is to take a screenshot of the current view, than replace the view with the screenshot while updating the contents of the view offscreen, then doing the animation. 我尝试过的是获取当前视图的屏幕截图,而不是在屏幕外更新视图内容时用屏幕截图替换视图,然后进行动画处理。 But the screenshot isn't showing up on the screen. 但是屏幕截图未显示在屏幕上。 It's just black until the original view slides back onto the screen. 在原始视图滑回屏幕之前,它只是黑色的。 Here's the code I'm using: 这是我正在使用的代码:

    UIGraphicsBeginImageContext(self.view.window.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    iv.image = image;
    [self.view.window insertSubview:iv aboveSubview:self.view];
    self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        iv.frame = CGRectMake(self.view.frame.size.width*2, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        [self loadData];
        self.view.frame = CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    } completion:NULL];
    [iv removeFromSuperview];
    [iv release];

I found the problem. 我发现了问题。 I just had to do [iv removeFromSuperview]; 我只需要做[iv removeFromSuperview]; in the completion block. 在完成块中。 Now it's working. 现在可以了。

Usually you need just one window in your app, the one containing all your views, so self.view.window is not a good approach, I think... 通常,您在应用程序中只需要一个窗口,其中包含所有视图,因此self.view.window不是一个好方法,我认为...

If I've got what you're trying to achieve, maybe a container view could be the right solution. 如果我有您要实现的目标,那么容器视图可能是正确的解决方案。

For example, supposing you are working on an iPhone: 例如,假设您正在使用iPhone:

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 960.0f, 480.0f)];

firstView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
secondView.frame = CGRectMake(320.0f, 0.0f, 320.0f, 480.0f);
thirdView.frame = CGRectMake(640.0f, 0.0f, 320.0f, 480.0f);

[containerView addSubview:firstView];
[containerView addSubview:secondView];
[containerView addSubview:thirdView];

Now you have your containerView containing three views. 现在,您的containerView包含三个视图。 Move your containerView left or right to show the view you like and to hide the other two views, then update the views that are out of the screen. 向左或向右移动containerView以显示所需的视图并隐藏其他两个视图,然后更新屏幕外的视图。

Another approach could be using UIScrollView as containerView. 另一种方法是将UIScrollView用作containerView。

Hope this helps... 希望这可以帮助...

EDIT: I've misunderstood your question, sorry. 编辑:对不起,我误解了你的问题。 Next try... 下次尝试...

When you add your screenshot, you add it on the right of the screen (offscreen) 添加屏幕快照时,将其添加到屏幕右侧(屏幕外)

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    iv.image = image;
    [self.view.window insertSubview:iv aboveSubview:self.view];

Then you move you original view on the left (offscreen) 然后,将原始视图移到左侧(屏幕外)

    self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);

But you don't show your screenshot, so the view is blank. 但是您没有显示屏幕截图,因此视图为空白。

I think you have to change this 我想你必须改变这个

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];

To this 对此

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];

Or you can add an animation to show it from the right. 或者,您可以添加动画以从右侧显示它。

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

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