我是iOS新手。我试图改变导航控制器中的过渡动画,使用Segue从下到上加载一个新的UIView控制器。我相信它不会太难实现,但可能是我无法理解它。
我在其他帖子中找不到任何解决方案。
这是我一直在尝试的代码: -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"alarmSegue"]) {
tab2ViewController *destViewController = segue.destinationViewController;
UIView *destView = destViewController.view;
destViewController.selectionName = @"alarms";
[sender setEnabled:NO];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView animateWithDuration:0.0
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
[destView setTransform:CGAffineTransformMakeTranslation(0, -1000)];
//[destView setFrame:CGRectMake(0, 440, 400, 45)];
//destView.frame = CGRectMake(0, 0, 320, 460);
}
completion:^(BOOL finished){
[sender setEnabled:YES];
}];
[UIView commitAnimations];
}
}
我只想使用Segue从下到上实现SIMPLE过渡。我也想设置目标控制器的一些属性。
答案 0 :(得分:2)
使用以下内容替换[UIView beginAnimations:nil context:nil];
和[UIView commitAnimations];
之间的代码(包括这两行):
CGAffineTransform baseTransform = destView.transform; //1
destView.transform = CGAffineTransformTranslate(baseT,0,destView.bounds.size.height); //2
[UIView animateWithDuration: 0.5 animations:^{
destView.transform = baseTransform; //3
}];
您可以进一步将延迟,选项和完成参数添加到该UIView Animation方法中。 Xcode可能会帮助你自动完成这些。
答案 1 :(得分:2)
嗯,你快到了。
你需要改变两件事:
1更改视图的初始位置。您希望视图从下到上显示,因此最初您的视图应位于底部。这段代码应该在动画之前编写。
[destView setTransform:CGAffineTransformMakeTranslation(0,-1000)];
2然后你需要从底部滑动到原始位置,因此在动画块内:
[destView setTransform:CGAffineTransformMakeTranslation(0, 0)];
还有一个建议,我猜你使用的是更新版本的iOS(iOS4以上)而不是使用动画块使用UIView的动画方法
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
有关详细信息,请参阅description。
编辑:
根据您的问题编辑,您正在寻找使用storyboard在应用程序中更改NavigationController的过渡动画。为此你可以使用Custom Segue。您将不得不创建一个自定义segue,一个UIStorySegue的子类并覆盖其perform
方法,如下所示:
-(void)perform {
//UIViewAnimationOptions options;
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = 0.9f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[destinationController.view.layer addAnimation:transition forKey:kCATransition];
[sourceViewController presentViewController:destinationController animated:NO completion:nil];
}
您还必须在故事板中将segue类更改为此自定义segue,以使其正常运行。
答案 2 :(得分:1)
尝试使用transition effects
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"alarmSegue"]) {
CATransition transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = direction;
[self.view.layer addAnimation:transition forKey:kCATransition];
tab2ViewController *destViewController = segue.destinationViewController;
UIView *destView = destViewController.view;
destViewController.selectionName = @"alarms";
[sender setEnabled:NO];
}
}