2010-02-25 41 views
1

我的對象上有兩個UIImageView,我試圖將它們設置爲像書一樣的頁面動畫。我用了一段時間的內置動畫(UIViewAnimationTransitionFlipFromLeft/Right),並決定我可以做得更好。我在這裏找到了一個很好的動畫例子:http://fiftysixtysoftware.com/blog/2009/uiview-pageopen-sample-project/CABasicAnimation CALayer轉換的可見性(重疊)問題

我能夠從那裏推斷出我的動畫效果......但是我遇到的問題是我的動畫圖像(正確的圖像)總是在下面顯示左邊的圖像。

這裏是在初始化代碼:

// set up some rects for use later 
landscapeLeftRect = CGRectMake(0, 12, 512, 725); 
landscapeRightRect = CGRectMake(512, 12, 512, 725); 

// all our images 
imageLeft = [[UIImageView alloc] initWithFrame:portraitRect]; 
imageRight = [[UIImageView alloc] initWithFrame:landscapeRightRect]; 

...和執行實際的動畫(注意,這是動畫的下半年,一半的「重疊」我imageLeft視圖):

// set the image to be nextLeft 
//[imageRight setImage:image.nextLeft]; 
[self.view sendSubviewToBack:imageLeft]; 
[self.view bringSubviewToFront:imageRight]; 
//[imageRight.layer setZPosition:1.0f]; 
[imageRight setFrame:landscapeLeftRect]; 

// remove any previous animations 
[imageRight.layer removeAllAnimations]; 

// set up the anchorPoint and center 
if (imageRight.layer.anchorPoint.x != 1.0f) { 
    imageRight.layer.anchorPoint = CGPointMake(1.0f, 0.5f); 
    imageRight.center = CGPointMake(imageRight.center.x + imageRight.bounds.size.width/2.0f, imageRight.center.y); 
} 

// create an animation to hold the first half of the page turning forward 
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
transformAnimation.removedOnCompletion = YES; 
transformAnimation.duration = 0.40f; 
transformAnimation.delegate = self; 
transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
// start the animation from the current state 
CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 
                 0.0f, 
                 -1.0f, 
                 0.0f); 
// these values control the 3D projection outlook 
endTransform.m34 = -0.001f; 
endTransform.m14 = 0.0015f; 
transformAnimation.fromValue = [NSValue valueWithCATransform3D:endTransform]; 
// we should end up at the beginning... 
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 

[imageRight.layer addAnimation:transformAnimation forKey:nil]; 

事情我已經試過:(你可以看到無論是在上面的代碼中)

  • bringSubviewToFront/sendSubviewToBac ķ
  • 提前任何建議或智慧的層上設置Z指數

感謝。同樣,問題是我的imageLeft總是顯示在動畫之上。

+0

任何人現在看這個問題(超過兩年後)應該看看頁面視圖控制器('UIPageViewController')和它的動畫。 – livingtech

回答

2

我面臨同樣的問題,只是發現你可以通過設置視圖的zPosition來解決它。 我用過這樣的東西:

viewThatShouldBeSentToTheBack.layer.zPosition = -10000;

0

我發現,一旦我把動畫視圖放到前面,一切都奏效了。我正在製作翻頁動畫,並且在向後翻頁時以逆時針方式顯示頁面疊加時遇到問題。我的麻煩是我看不到動畫,因爲它在它應該覆蓋的頁面後面。

+0

我的問題的關鍵是bringSubviewToFront對我沒有任何作用。 – livingtech

0

是否確實在將頁面視圖添加到超級視圖時正確地遍歷數組?當然,你最好先把最後一頁放在最前面,這樣你的第一頁就可以在上面了...我不會侮辱你的智力 - 圖層可能會非常棘手。

+0

我在任何時候都沒有遍歷子視圖。這裏有一個有趣的二分法,我使用的動畫只能應用於圖層,但是否則我只能處理已添加到父圖層的視圖。 – livingtech

0

我遇到了這個確切的問題,我發現唯一的方法來避免它是通過設置tyhe背景的zPosition爲一個很大的負數。這將是很高興知道爲什麼...