2013-12-12 70 views
0

我想圍繞屏幕右邊緣旋轉視圖,就像開門一樣。這是我到目前爲止有:圍繞屏幕右側旋轉UIView

CATransform3D transform = CATransform3DIdentity; 
transform.m34 = -1.0f/500; 
view.layer.transform = CATransform3DRotate(transform, kDegreesToRadians(90), 0, 1, 0); 
[UIView animateWithDuration:2.0f animations:^{ 
    view.layer.transform = transform; 
} completion:nil]; 

我不是CATranforms完全固體又或矩陣對於這個問題,所以如果有人可以把我在正確的方向,它會非常感謝!

+0

您是否嘗試更改錨點? http://stackoverflow.com/questions/7438385/ios-rotate-view-around-an-anchor-point-and-follow-touch – atxe

回答

2

UIView動畫是動畫視圖的屬性,而不是動畫層。據我所知,你不能使用UIView animateWithDuration:call爲視圖的層設置動畫。

您需要創建一個CABasicAnimation並將其自己添加到視圖的圖層中。

這已經有一段時間,因爲我做了CAAnimations,但讓我們看看我能挖:作爲atxe說

,你應該做這個動畫之前圖層的錨點移動到1 .5,所以它圍繞右邊緣旋轉。

這裏是我們的「凱文&凱爾」動畫片的應用程序,但這門的動畫擺動它的鉸鏈開一些代碼:

//Open the door. 
rotateAnimation = [CABasicAnimation animation]; 
rotateAnimation.keyPath = @"transform"; 
rotateAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 

//Pivot on the right edge (y axis rotation) 
theTransform = CATransform3DIdentity; 
    theTransform.m34 = -1/100.0; 
    theTransform = CATransform3DRotate(theTransform, degreesToRadians(70), 0, 1, 0); 


rotateAnimation.toValue = [NSValue valueWithCATransform3D:theTransform]; 

rotateAnimation.duration = 0.5; 

// leaves presentation layer in final state; preventing snap-back to original state 
rotateAnimation.removedOnCompletion = NO; 
rotateAnimation.fillMode = kCAFillModeBoth; 

rotateAnimation.repeatCount = 0; 
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotateAnimation.delegate = self; 
[treeDoorImageView.layer addAnimation:rotateAnimation forKey:@"transform"]; 

變換邏輯幾乎是相同的你(我用的-1/100一個更誇張的3D效果的M34值。你-1/500設置是比較正常的。)

注意,而不是試圖做從一個UIView動畫塊的動畫,我創建了一個CABasicAnimation,並將其從Value和toValue屬性設置爲開始和結束變換值。

您應該可以使用此代碼作爲動畫的起點。

+0

這樣一個夢幻般的答案。謝謝! – barndog

0

您需要使用CAAnimation,而不是UIView塊動畫。

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
animation.duration = 2; 
animation.fromValue = [NSValue valueWithCATransform3D:view.layer.transform]; // From the existing transform. 
animation.toValue = [NSValue valueWithCATransform3D:transform]; // To the new one. 

view.layer.transform = transform; // Set the new transform. 
[view.layer addAnimation:animation forKey:@"door"]; // Animate visual change. 

此外,作爲axte指出的那樣,你將不得不設置圖層的anchorPoint(1, 0.5)申請圍繞層的右邊緣旋轉。