我有一個iOS UIView UIViewAnimationTransitionFlipFromRight
。我需要它垂直翻轉。頁面捲曲轉換不會削減它。我認爲這將需要一些定製。UIView垂直翻轉動畫
任何想法?
我有一個iOS UIView UIViewAnimationTransitionFlipFromRight
。我需要它垂直翻轉。頁面捲曲轉換不會削減它。我認爲這將需要一些定製。UIView垂直翻轉動畫
任何想法?
從iOS 5.0開始,不需要編寫自己的Core Animation轉換來進行垂直翻轉。只需使用UIKit的UIViewAnimationOptionTransitionFlipFromTop
and UIViewAnimationOptionTransitionFlipFromBottom
轉換,所有這些東西變成一個單一的方法調用。
這些行爲類似於UIViewAnimationOptionTransitionFlipFromLeft
和UIViewAnimationOptionTransitionFlipFromRight
(自iOS 2.0以來一直存在)。
實例:
[UIView transitionFromView:viewToReplace
toView:replacementView
duration:1
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:nil];
上面的代碼將垂直翻轉的viewToReplace
的上海華。在動畫的中途點,在超視圖垂直於屏幕並因此不可見的瞬間,viewToReplace
被replacementView
取代。
就是這麼簡單。
只需使用Core Animation Transforms編寫自己的翻轉方法即可。
- (void)verticalFlip{
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
} completion:^{
// code to be executed when flip is completed
}];
}
請確保您有核心動畫庫/加入,其中包括和框架也已列入math.h
如果你想使用M_PI
符號。
編輯:
有它本質上是 「變化」 時,它的中途翻轉角度做這樣的事情:
- (void)verticalFlip{
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
} completion:^{
while ([yourView.subviews count] > 0)
[[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
// Add your new views here
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
} completion:^{
// Flip completion code here
}];
}];
}
這也可以工作:
- (void)verticalFlip{
// Do the first half of the flip
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
} completion:^{
while ([yourView.subviews count] > 0)
[[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
// Add your new views here
}];
// After a delay equal to the duration+delay of the first half of the flip, complete the flip
[UIView animateWithDuration:someDuration delay:durationOfFirstAnimation animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
} completion:^{
// Flip completion code here
}];
}
乾杯。
我已經看到翻轉,但我需要顯示視圖的「背面」。有沒有辦法在這個動畫的中途隱藏它的子視圖,所以它模擬了視圖在背面是「空的」? – 2011-03-22 15:25:14
我編輯了我的帖子,添加了代碼,可以做到這一點。 – 2011-03-22 16:20:29
對於3D變換,您需要執行yourView.layer.transform而不是yourView.transform – 2011-06-28 12:24:50
從布倫頓代碼爲我,所以我沒有通過蘋果的文檔和found this piece of code多一點挖不工作:
- (IBAction)toggleMainViews:(id)sender {
[UIView transitionFromView:(displayingPrimary ? primaryView : secondaryView)
toView:(displayingPrimary ? secondaryView : primaryView)
duration:1.0
options:(displayingPrimary ?
UIViewAnimationOptionTransitionFlipFromRight :
UIViewAnimationOptionTransitionFlipFromLeft)
completion:^(BOOL finished) {
if (finished) {
displayingPrimary = !displayingPrimary;
}
}
];
}
工作就像一個魅力。
這是一個垂直翻轉? – 2011-04-01 17:35:16
它不是一個垂直翻轉。它的系統提供了水平翻轉 – 2011-09-09 15:33:45
什麼是downvotes?此代碼比其他示例更短且更清晰。有沒有隱藏的陷阱? – 2012-04-06 11:21:45
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
// checks to see if the view is attached
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:flipLabel
cache:YES];
flipLabel.backgroundColor = [UIColor yellowColor];
[UIView commitAnimations];
你可以讓你想要同時翻轉查看任何修改,在這裏,我有改變背景顏色
-1,因爲a)這是一個水平翻轉,和b)我不認爲這增加了任何新的東西,我的答案還沒有提供。 – 2014-05-24 10:32:49
UIViewAnimationOptionTransitionFlipFromTop易於使用,但我們不能創建使用UIViewAnimationOptionTransitionFlipFromTop一個互動的轉變。我們需要變更圖層的變換來創建交互式變換。
只需使用CATransform3DMakeRotation創建轉換是不夠的,沒有燈光效果,沒有視角。我寫了一個樣本來添加這些效果。您可以輕鬆將其更改爲交互式轉換。
演示:
樣品的編號:
CALayer *sideALayer = sideAView.layer;
CALayer *sideBLayer = sideBView.layer;
CALayer *containerLayer = containerView.layer;
sideALayer.opacity = 1;
sideBLayer.opacity = 0;
sideBLayer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
containerLayer.transform = CATransform3DIdentity;
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = -1.0/containerViewWidth;
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
sideALayer.opacity = 0;
containerLayer.transform = CATransform3DConcat(perspectiveTransform,CATransform3DMakeRotation(M_PI_2, 0, 1, 0));
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
sideBLayer.opacity = 1;
containerLayer.transform = CATransform3DConcat(perspectiveTransform, CATransform3DMakeRotation(M_PI, 0, 1, 0));
}];
} completion:nil];
sideAView和sideBView是containerView的子視圖。
containerView被設置爲黑色背景。
這個動畫有什麼問題。正如你所看到的,在旋轉的後半部分,視角被完全去除,看起來很奇怪。 – 2015-09-21 23:07:12
這是令人印象深刻的,但不完全正確! – Fattie 2015-09-25 13:43:17
要翻轉成的UIView:
-(void) goToSeconVC
{
SecondVC *secondVCObj = [[SecondVC alloc] init];
[UIView beginAnimation: @」View Flip」 context: nil];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: self.navigationController.view cache: NO];
[sef.navigationController pushViewController: seconVCObj animated: YES];
[UIView commitAnimation];
}
**To flip into a view controller:**
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:viewController animated:YES completion:nil];
**To flip out of it:**
[self dismissViewControllerAnimated:YES completion:nil];
雨燕4.0版本 100%工作液
// view1: represents view which should be hidden and from which we are starting
// view2: represents view which is second view or behind of view1
// isReverse: default if false, but if we need reverse animation we pass true and it
// will Flip from Left
func flipTransition (with view1: UIView, view2: UIView, isReverse: Bool = false) {
var transitionOptions = UIViewAnimationOptions()
transitionOptions = isReverse ? [.transitionFlipFromLeft] : [.transitionFlipFromRight] // options for transition
// animation durations are equal so while first will finish, second will start
// below example could be done also using completion block.
UIView.transition(with: view1, duration: 1.5, options: transitionOptions, animations: {
view1.isHidden = true
})
UIView.transition(with: view2, duration: 1.5, options: transitionOptions, animations: {
view2.isHidden = false
})
}
功能召喚:
anim.flipTransition(with: viewOne, view2: viewTwo)
anim.flipTransition(with: viewTwo, view2: viewOne, isReverse: true)
最佳實踐會至創建UIView
擴展名,並將該函數保留爲該擴展名,以便任何UIView
子對象都可以訪問該擴展名。這個解決方案也可以使用completionBlock編寫。
謝謝@ C0mrade,你節省了我的時間。 – 2017-07-18 08:22:31
注意:在調用動畫之前,在其超級視圖中插入兩個視圖。 「viewToReplace」必須位於「replacementView」前面。兩個視圖都必須設置爲hidden = false。 – artkoenig 2013-08-16 13:26:42
@Artjom您聲稱與我的經歷不同。在調用'transitionFromView:...'之前,如果沒有在'superview'中插入'replacementView',這對我來說工作得很好。也許還有其他一些不明顯的相關因素,我們也採取了不同的做法,導致我們觀察到的差異?你能否提供一個示例項目來展示你所描述的內容? – 2013-08-16 13:33:14
在我的情況下,兩個視圖都是從nib初始化爲類成員。但是,如果沒有在翻轉超視圖中插入這兩種方法,您的方法將無法正常工作。 – artkoenig 2013-08-16 13:49:19