在iPad上,我们可以获得更多的工作空间,因此呈现全屏模态视图并不理想。
我知道如何在新的formSheet中呈现模态视图,并且可以在这个问题上找到密切的方法:iPad iTunes Animation
问题在于你不能选择动画的来源,所以它只是默认并从中心出现,我想自定义它以便从特定位置出现。
我可以在this video
的前几秒看到我能找到的最佳动画示例如果有人可以使用代码,教程或文档指出我正确的方向,我将非常感激!
更新
经过一番调查,我发现第一部分可以使用图层和核心动画来完成;然后为formSheet模式视图设置动画,但我仍然不太明白如何实现它,希望你们能帮忙!
答案 0 :(得分:21)
我所做的是为UIViewController创建一个新类别,如下所示
<强>的UIViewController + ShowModalFromView.h 强>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view;
@end
<强>的UIViewController + ShowModalFromView.m 强>
#import "UIViewController+ShowModalFromView.h"
@implementation UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view
{
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
// Add the modal viewController but don't animate it. We will handle the animation manually
[self presentModalViewController:modalViewController animated:NO];
// Remove the shadow. It causes weird artifacts while animating the view.
CGColorRef originalShadowColor = modalViewController.view.superview.layer.shadowColor;
modalViewController.view.superview.layer.shadowColor = [[UIColor clearColor] CGColor];
// Save the original size of the viewController's view
CGRect originalFrame = modalViewController.view.superview.frame;
// Set the frame to the one of the view we want to animate from
modalViewController.view.superview.frame = view.frame;
// Begin animation
[UIView animateWithDuration:1.0f
animations:^{
// Set the original frame back
modalViewController.view.superview.frame = originalFrame;
}
completion:^(BOOL finished) {
// Set the original shadow color back after the animation has finished
modalViewController.view.superview.layer.shadowColor = originalShadowColor;
}];
}
@end
这很直截了当。如果这对你有所帮助,请告诉我。
<强>更新强>
我已经更新了使用动画块而不是[UIView beginAnimations:nil context:nil];
/ [UIView commitAnimations]
对的答案。
答案 1 :(得分:5)
看起来你基本上是在翻译(移动)CALayer
同时缩小它并同时围绕y轴旋转它。试试这个:
NSValue *initialTransformValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
CATransform3D translation = CATransform3DMakeTranslation(finalPoint.x, finalPoint.y, 0.0);
CATransform3D scalingAndTranslation = CATransform3DScale(translation, kMyScalingFactor, kMyScalingFactor, 1.0);
CATransform3D finalTransform = CATransform3DRotate(scalingAndTranslation, myRotationAngle, 0.0, 1.0, 0.0);
NSArray *keyFrameValues = [NSArray arrayWithObjects:initialTransformValue, [NSValue valueWithCATransform3D:finalTransform], nil];
CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
myAnimation.values = keyFrameValues;
myAnimation.duration = kMyAnimationDuration;
myAnimation.delegate = self;
myAnimation.removedOnCompletion = NO;
myAnimation.fillMode = kCAFillModeForwards;
[myLayer addAnimation:myAnimation forKey:@"myAnimationKey"];
finalPoint
应该是CGPoint
坐标空间中的myLayer
。 kMyScalingFactor
缩小时应小于1.0,扩展时大于1.0; myRotationAngle
应该是弧度。使用正值可以顺时针旋转,负值可以逆时针旋转。您还需要实现动画终止处理程序以使动画“坚持”:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
myLayer.transform = finalTransform;
myLayer removeAnimationForKey:@"myAnimationKey"];
}
希望这有帮助。
答案 2 :(得分:2)
我之前通过动画视图获得了这个功能。
1)相册图稿在网格中 2)使用翻转动画转换专辑封面的视图 3)为在屏幕上移动的视图设置动画。
我很快就把它扔了。假设你有一个空的视图控制器和3个视图。
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(transition) withObject:nil afterDelay:1];
albumArtworkSquare = [[UIView alloc] initWithFrame:CGRectMake(400, 500, 300, 300)];
albumArtworkSquare.backgroundColor = [UIColor blackColor];
[self.view addSubview:albumArtworkSquare];
frontViewOfAlbumArtwork = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
frontViewOfAlbumArtwork.backgroundColor = [UIColor blueColor];
[albumArtworkSquare addSubview:frontViewOfAlbumArtwork];
backViewToTransitionTo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
backViewToTransitionTo.backgroundColor = [UIColor grayColor];
}
- (void)transition
{
[UIView animateWithDuration:2 animations:^{
albumArtworkSquare.frame = CGRectMake(10, 500, 300, 300);
}];
[UIView transitionFromView:frontViewOfAlbumArtwork toView:backViewToTransitionTo duration:2 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished)
{
[frontViewOfAlbumArtwork removeFromSuperview];
[albumArtworkSquare addSubview:backViewToTransitionTo];
}];
}
答案 3 :(得分:2)
Mihai的答案并不像iPad itunes动画那样处理翻转。我改变了一点来翻转视图。你不需要各种疯狂的CAAnimation东西,只需要几个内置的UIView动画功能。
#import <QuartzCore/QuartzCore.h>
@interface UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view;
@end
@implementation UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view
{
NSTimeInterval scaleSpeed = 0.3;
NSTimeInterval flipSpeed = 0.4;
UIView __weak *containerView = view.superview;
view.autoresizesSubviews = YES;
[self presentModalViewController:modalViewController animated:NO];
UIView __weak *presentedView = modalViewController.view.superview;
//intead of show the actual view of modalViewController, we are showing the snapshot of it to avoid layout problem
UIGraphicsBeginImageContext(presentedView.bounds.size);
[presentedView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* modalSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView __weak *originalSuperView = presentedView.superview;
CGRect originalFrame = presentedView.frame;
CGRect frameInContainer = [containerView convertRect:originalFrame fromView:originalSuperView];
[presentedView removeFromSuperview];
UIImageView* snapshotView = [[UIImageView alloc] initWithImage:modalSnapshot];
snapshotView.autoresizingMask = UIViewAutoresizingNone;
[containerView bringSubviewToFront:view];
[UIView animateWithDuration:scaleSpeed delay:0 options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionBeginFromCurrentState animations: ^{
[UIView animateWithDuration:scaleSpeed
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations: ^{
view.frame = frameInContainer;
}
completion:nil
];
} completion:^(BOOL finished) {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView transitionWithView:view duration:flipSpeed options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionTransitionFlipFromRight animations:
^{
snapshotView.frame = view.bounds;
[view addSubview:snapshotView];
} completion:^(BOOL finished) {
[originalSuperView addSubview:presentedView];
[snapshotView removeFromSuperview];
}
];
}];
}
答案 4 :(得分:1)
您可以使用此..用于基于导航控制器的应用程序..
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewControllerXIBName" bundle:nil];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES];
[self.navigationController pushViewController:obj animated:NO];
[UIView commitAnimations];
[obj release];
答案 5 :(得分:1)
我在一个有专辑艺术网格的项目中做了类似的事情。
这是我采取的方法。关键是使用CAAnimationGroup
。
1)整个动画将包括同时缩放,旋转和沿路径移动 - 首先是专辑封面图层,然后是模态视图图层。
2)通过将相册艺术图层翻转90度,缩放一点并从当前位置移动到预定位置来为其设置动画。此时它将消失(垂直于屏幕)。
3)添加模态视图。缩放并将模态视图转换为第1步中专辑封面所在的确切位置。
4)通过缩放,旋转和沿路径移动来填充屏幕,从此位置为模态视图设置动画。
5)删除模态视图。
6)呈现没有动画的模态视图。
7)选择的路径通常会将屏幕中心添加为控制点。但随后可以根据您希望动画的显示方式进行更改。
下面是一个可以看到动画组使用的功能。希望这对你有所帮助。我仍然没有弄清楚如何通过导航栏和标签栏来避免剪辑动画。 :)
+ (void)animateWithCurrentView:(UIView *)currentView
{
#define kResizeKey @"bounds.size"
#define kPathMovement @"position"
#define kRotation @"transform"
#define kGroupAnimation @"subviewBeingAnimated"
#define kLayerAnimation @"animateLayer"
//flip the view by 180 degrees in its place first.
currentView.layer.transform = CATransform3DRotate(currentView.layer.transform,radians(180), 0, 1, 0);
//set the anchor point so that the view rotates on one of its sides.
currentView.layer.anchorPoint = CGPointMake(0.0, 0.5);
/**
* Set up scaling
*/
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:kResizeKey];
//we are going to fill the screen here. So 320,480
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(320, 480)]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;
/**
* Set up path movement
*/
UIBezierPath *movePath = [UIBezierPath bezierPath];
//the control point is now set to centre of the filled screen. Change this to make the path different.
CGPoint ctlPoint = CGPointMake(160.0, 240.0);
//This is the starting point of the animation. This should ideally be a function of the frame of the view to be animated. Hardcoded here.
[movePath moveToPoint:CGPointMake(320, 60)];
//The anchor point is going to end up here at the end of the animation.
[movePath addQuadCurveToPoint:CGPointMake(0, 240) controlPoint:ctlPoint];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:kPathMovement];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;
/**
* Setup rotation animation
*/
CABasicAnimation* rotateAnimation = [CABasicAnimation animationWithKeyPath:kRotation];
//start from 180 degrees (done in 1st line)
CATransform3D fromTransform = CATransform3DMakeRotation(radians(180), 0, 1, 0);
//come back to 0 degrees
CATransform3D toTransform = CATransform3DMakeRotation(radians(0), 0, 1, 0);
//This is done to get some perspective.
CATransform3D persp1 = CATransform3DIdentity;
persp1.m34 = 1.0 / -3000;
fromTransform = CATransform3DConcat(fromTransform, persp1);
toTransform = CATransform3DConcat(toTransform,persp1);
rotateAnimation.toValue = [NSValue valueWithCATransform3D:toTransform];
rotateAnimation.fromValue = [NSValue valueWithCATransform3D:fromTransform];
//rotateAnimation.duration = 2;
rotateAnimation.fillMode = kCAFillModeForwards;
rotateAnimation.removedOnCompletion = NO;
/**
* Setup and add all animations to the group
*/
CAAnimationGroup *group = [CAAnimationGroup animation];
[group setAnimations:[NSArray arrayWithObjects:moveAnim,rotateAnimation, resizeAnimation, nil]];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
group.duration = 0.7f;
group.delegate = self;
[group setValue:currentView forKey:kGroupAnimation];
/**
* ...and go
*/
[currentView.layer addAnimation:group forKey:kLayerAnimation];
}
答案 6 :(得分:0)
如果你可以先得到模态视图,以显示你想要的地方没有动画。应该是向前的,并且设置view.superview
的边界/框架对于工作表样式可能是有用的。
如果你解决了问题,那么你可以执行你的“纯视图”动画来获取它,然后移除视图,并“呈现”模态控制器(控制该视图)以立即将控制器交换到逻辑控制视图层次结构。