IOS/Objective-C:在没有故事板序列的情况下,是否可以在模式转换中使用自定义序列?
我在故事板中有两个视图控制器,我想使用自定义向下顺序(与常规封面垂直方向相反)来演示第一个视图控制器中的第二个 我已经用代码对故事板segue进行了子类化,我希望这些代码能够起作用 但是,在本例中,我没有将情节提要中的一个片段从一个视图控制器拖到另一个视图控制器。相反,我在代码中处理模态表示 如果在代码中调用第二个视图控制器,是否可以调用自定义segue?或者,您必须在脚本中有一个实际的segue来分配自定义类吗 如果可能的话,你会把代码放在哪里 下面是我用来展示模态视图控制器的代码IOS/Objective-C:在没有故事板序列的情况下,是否可以在模式转换中使用自定义序列?,ios,objective-c,segue,Ios,Objective C,Segue,我在故事板中有两个视图控制器,我想使用自定义向下顺序(与常规封面垂直方向相反)来演示第一个视图控制器中的第二个 我已经用代码对故事板segue进行了子类化,我希望这些代码能够起作用 但是,在本例中,我没有将情节提要中的一个片段从一个视图控制器拖到另一个视图控制器。相反,我在代码中处理模态表示 如果在代码中调用第二个视图控制器,是否可以调用自定义segue?或者,您必须在脚本中有一个实际的segue来分配自定义类吗 如果可能的话,你会把代码放在哪里 下面是我用来展示模态视图控制器的代码 UISto
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"chat" bundle:nil];
UIViewController *myVC = [storyboard instantiateViewControllerWithIdentifier:@"chatVC"];
[(UINavigationController*)self.window.rootViewController presentViewController:myVC animated:NO completion:nil];
似乎您正在尝试在应用程序中完成许多自定义动画转换,因此我建议您使用自己的animator类,而不是尝试创建自定义片段。与我向您发布的上一个问题的示例(在我上面的评论中链接)类似,您需要设置某种委托(在上一个示例中是一个UINavigationControllerDelegate),以确定用于呈现/取消的自定义动画类(在上一个示例中是用于推送/弹出)某些视图控制器: 这里有一个例子来说明我的意思: ViewController.m(将其用作UIViewControllerTransitioningDelegate):
#导入“ViewController.h”
#导入“SlideDownAnimator.h”
#导入“SlideUpAnimator.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(iAction)\u presentCustomDowndown:(id)发送方{
//设置一个我们将要演示的假视图控制器
UIViewController*fakeSecondViewController=[[UIViewController alloc]init];
fakeSecondViewController.view.backgroundColor=[UIColor blueColor];
//确保设置设计视图控制器的转换委托
//这里不必将其设置为self,您可以将其设置为任何将实现的内容:UIViewControllerTransitioningDelegate
fakeSecondViewController.transitioningDelegate=self;
//当我们在视图控制器上调用present时,它会向transitioningDelegate请求动画协调器,我们将在下面提供
[self.navigationController presentViewController:fakeSecondViewController已设置动画:是完成:^{
//dismis在几秒钟后显示自定义解雇的样子(显然这只是示例代码,您将自己处理解雇)
调度时间(调度时间(现在调度时间,(int64_t)(2.0f*NSEC_/u秒)),调度获取主队列()之后^{
[self.navigationController dismissViewControllerAnimated:YES完成:nil];
});
}];
}
//代表要求发言
-(id)显示了animationControllerForPresentedController:(UIViewController*)
呈现控制器:(UIViewController*)呈现
sourceController:(UIViewController*)源{
//返回我们自己的自定义Animator类(遵循UIViewControllerAnimatedTransitioning协议)
返回[[SlideDownAnimator alloc]init];
}
//代表要求解雇
-(id)animationControllerForDismissedController:(UIViewController*)已解除{
//返回我们自己的自定义Animator类(遵循UIViewControllerAnimatedTransitioning协议)
返回[[SlideUpAnimator alloc]init];
}
SlideDownAnimator.h(自定义向下Animator):
#导入
#进口
NS\u假设\u非空\u开始
@界面SlideDownAnimator:NSObject
@结束
NS\u假设\u非空\u结束
SlideDownAnimator.m:
#import "SlideDownAnimator.h"
@implementation SlideDownAnimator
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
return 0.4f;
}
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// grab the toViewController (the vc being presented)
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// manually add it to our container view
[[transitionContext containerView] addSubview:toViewController.view];
// get the frame so we can do some of our own animations on it
CGRect toVCFrame = toViewController.view.frame;
CGFloat toVCHeight = toVCFrame.size.height;
// offset the y coordiante by it's height so that it's completely above our current screen
toVCFrame.origin.y = -toVCHeight;
// set the initial frame so it's above our screen
[toViewController.view setFrame:toVCFrame];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
// animate the y position to 0 so it slides down
CGRect finalVCFrame = toViewController.view.frame;
finalVCFrame.origin.y = 0;
[toViewController.view setFrame:finalVCFrame];
} completion:^(BOOL finished) {
// make sure to call this so we'll call back to our presentViewdController's completion block
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
@end
#import "SlideUpAnimator.h"
@implementation SlideUpAnimator
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
return 0.4f;
}
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// grab our fromViewController (the vc being dismissed)
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// and our toViewController (the vc that will be shown after dismissal)
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// add our toViewController as a subview of our container view (make sure to add it beneath the dismissing view controller so we can let it complete it's animation first)
[[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
// push the view back up above the screen
CGRect fromVCFrame = fromViewController.view.frame;
fromVCFrame.origin.y = -fromVCFrame.size.height;
[fromViewController.view setFrame:fromVCFrame];
} completion:^(BOOL finished) {
// make sure to call this so we'll call back to our presentViewdController's completion block
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
@end
#导入“SlideDownAnimator.h”
@实现SlideDownAnimator
-(NSTimeInterval)transitionDuration:(可为null的id)transitionContext{
返回0.4f;
}
-(void)animateTransition:(非空id)transitionContext{
//抓取toViewController(正在呈现的vc)
UIViewController*toViewController=[transitionContext ViewControllerWorky:UITransitionContextToViewControllerKey];
//手动将其添加到容器视图中
[[transitionContext containerView]添加子视图:toViewController.view];
//获取帧,以便我们可以在其上制作一些自己的动画
CGRect toVCFrame=toViewController.view.frame;
CGFloat toVCHeight=toVCFrame.size.height;
//将y坐标偏移其高度,使其完全高于当前屏幕
toVCFrame.origin.y=-toVCHeight;
//设置初始帧,使其位于屏幕上方
[toViewController.view setFrame:toVCFrame];
[UIView animateWithDuration:[self-transitionDuration:transitionContext]动画:^{
//将y位置设置为0的动画,使其向下滑动
CGRect finalVCFrame=toViewController.view.frame;
finalVCFrame.origin.y=0;
[toViewController.view setFrame:finalVCFrame];
}完成:^(布尔完成){
//确保调用此函数,以便我们将调用presentViewdController的完成块
[transitionContext completeTransition:![transitionContext transitionWasCancelled];
}];
}
@结束
我的猜测是,在解雇时,您还需要一个自定义的向上滑动动画,因此您还需要一个实现该自定义转换的动画师:
SlideUpAnimator.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SlideUpAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end
NS_ASSUME_NONNULL_END
#导入
#进口
NS\u假设\u非空\u开始
@接口SlideUpAnimator:NSObject
@结束
NS\u假设\u非空\u结束
SlideUpAnimator.m:
#import "SlideDownAnimator.h"
@implementation SlideDownAnimator
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
return 0.4f;
}
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// grab the toViewController (the vc being presented)
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// manually add it to our container view
[[transitionContext containerView] addSubview:toViewController.view];
// get the frame so we can do some of our own animations on it
CGRect toVCFrame = toViewController.view.frame;
CGFloat toVCHeight = toVCFrame.size.height;
// offset the y coordiante by it's height so that it's completely above our current screen
toVCFrame.origin.y = -toVCHeight;
// set the initial frame so it's above our screen
[toViewController.view setFrame:toVCFrame];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
// animate the y position to 0 so it slides down
CGRect finalVCFrame = toViewController.view.frame;
finalVCFrame.origin.y = 0;
[toViewController.view setFrame:finalVCFrame];
} completion:^(BOOL finished) {
// make sure to call this so we'll call back to our presentViewdController's completion block
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
@end
#import "SlideUpAnimator.h"
@implementation SlideUpAnimator
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
return 0.4f;
}
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// grab our fromViewController (the vc being dismissed)
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// and our toViewController (the vc that will be shown after dismissal)
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// add our toViewController as a subview of our container view (make sure to add it beneath the dismissing view controller so we can let it complete it's animation first)
[[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
// push the view back up above the screen
CGRect fromVCFrame = fromViewController.view.frame;
fromVCFrame.origin.y = -fromVCFrame.size.height;
[fromViewController.view setFrame:fromVCFrame];
} completion:^(BOOL finished) {
// make sure to call this so we'll call back to our presentViewdController's completion block
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
@end
#导入“SlideUpAnimator.h”
@实现滑动平移器
-(NSTimeInterval)transitionDuration:(可为null的id)transitionContext{
返回0.4f;
}
-(void)animateTransition:(非空id)tr