2012-01-29 264 views
1

我正在嘗試動畫UIView左右滑動。我試過的是截取當前視圖的屏幕截圖,而不是更新視圖的屏幕截圖,同時更新視圖的屏幕內容,然後執行動畫。但屏幕截圖沒有顯示在屏幕上。這只是黑色,直到原始視圖滑回屏幕上。下面是我使用的代碼:左右滑動UIView

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]; 

回答

2

我發現了這個問題。我只需要在完成塊中做[iv removeFromSuperview];。現在它正在工作。

+0

你能發佈最終的代碼嗎?我遵循你的建議,但仍然遇到你描述的問題(截圖沒有出現)。 – user664939 2014-04-21 17:43:52

+0

@ user664939我真的不知道我用過這個。但從我的回答判斷,你可以添加一個完成塊來刪除視圖。 – edc1591 2014-04-21 23:46:37

+0

感謝您回覆我的評論。基於這個和其他一些問題,我能夠得到它的工作。 – user664939 2014-04-22 00:05:28

-1

通常你只需要一個窗口,在您的應用程序,包含所有你的觀點的人,所以self.view.window是不是一個好辦法,我想......

如果我有你想要實現的,也許一個容器視圖可能是正確的解決方案。

例如,假設您正在使用的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]; 

現在你有一個包含三個視圖您containerView。向左或向右移動containerView以顯示您喜歡的視圖並隱藏其他兩個視圖,然後更新屏幕外的視圖。

另一種方法是使用UIScrollView作爲containerView。

希望這有助於...

編輯:我誤解你的問題,對不起。 接着再試試......

當你把你的截圖,你加它的屏幕(屏幕外)

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); 

但是你不顯示你的截圖,所以視圖是空白的。

我認爲你必須要改變這種

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)]; 

對此

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

或者你也可以添加動畫從右側顯示它。

+0

就像我在我的問題中所說的,我只使用兩個視圖,而不是三個視圖。這個想法是我想重用self.view。這就是我使用圖像視圖的原因。 – edc1591 2012-01-29 19:54:14

+0

而我只使用一個窗口。 self.view.window是應用程序的主窗口。 – edc1591 2012-01-29 19:56:40

+0

啊......我看到......我在想你的問題是左右滑動(看你的標題),而不是顯示你的截圖。看看我的編輯。 – Beppe 2012-01-29 21:03:06