ios – 取消呈现视图控制器的交互式解雇转换后,视图控制器不会自动旋转

ios – 取消呈现视图控制器的交互式解雇转换后,视图控制器不会自动旋转,第1张

概述我有一个视图控制器使用modalPresentationStyle = UIModalPresentationCustom呈现另一个视图控制器.设置的内容使得呈现视图控制器视图的一部分显示在呈现的视图控制器视图下方.在此状态下,呈现视图控制器仍然可以正确处理自动旋转,并使用autolayout处理呈现的视图控制器的旋转. 我现在正尝试使用iOS 7的自定义视图控制器转换API以交互方式实现解除所呈 我有一个视图控制器使用modalPresentationStyle = UIModalPresentationCustom呈现另一个视图控制器.设置的内容使得呈现视图控制器视图的一部分显示在呈现的视图控制器视图下方.在此状态下,呈现视图控制器仍然可以正确处理自动旋转,并使用autolayout处理呈现的视图控制器的旋转.

我现在正尝试使用iOS 7的自定义视图控制器转换API以交互方式实现解除所呈现的视图控制器.它的工作原理除外,当取消交互式解雇时,自动旋转的处理停止工作. (它会在稍后解除所呈现的视图控制器之后再次工作.)为什么会发生这种情况,我该如何解决这个问题?

编辑:这是您可以运行以演示问题的代码.从下面d出一个视图,您可以通过向下滑动来消除它.如果通过不完全向下滑动来取消解雇,则呈现视图控制器的视图不再响应旋转,并且呈现的视图控制器的视图具有混乱的布局.

编辑:以下是作为Xcode项目的代码链接:
https://drive.google.com/file/d/0BwcBqUuDfCG2YlhVWE1QekhUWlk/edit?usp=sharing

对于大规模的代码转储感到抱歉,但我不知道我做错了什么.这是一个正在发生的事情的草图:VIEwController1呈现VIEwController2. VIEwController1实现了UIVIEwControllerTransitioningDelegate,因此它返回了转换的动画/交互式控制器. VIEwController2有一个平移手势识别器,可以驱动交互式解雇;它实现了UIVIEwControllerInteractiveTransitioning作为解雇的交互式控制器.如果用户将视图拖得足够远,它还会保留对动画控制器的引用以解除转换以完成转换.最后,有两个动画控制器对象. PresentAnimationController设置autolayout约束来处理呈现的视图控制器视图的旋转,dismissAnimationController结束解雇.

VIEwController1.h

#import <UIKit/UIKit.h>@interface VIEwController1 : UIVIEwController <UIVIEwControllerTransitioningDelegate>@end

VIEwController1.m

#import "VIEwController1.h"#import "VIEwController2.h"#import "PresentAnimationController.h"#import "dismissAnimationController.h"@implementation VIEwController1- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil];    if (self) {        self.Title = @"VIEw 1";        self.navigationItem.prompt = @"Press “Present” and then swipe down to dismiss.";        self.navigationItem.rightbarbuttonItem = [[UIbarbuttonItem alloc] initWithTitle:@"Present" style:UIbarbuttonItemStylePlain target:self action:@selector(pressedPresentbutton:)];    }    return self;}- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    self.vIEw.backgroundcolor = [UIcolor whitecolor];    // Some subvIEw just to check if layout is working.    UIVIEw * someSubvIEw = [[UIVIEw alloc] initWithFrame:self.vIEw.bounds];    someSubvIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;    someSubvIEw.backgroundcolor = [UIcolor orangecolor];    someSubvIEw.layer.bordercolor = [UIcolor redcolor].CGcolor;    someSubvIEw.layer.borderWIDth = 2;    [self.vIEw addSubvIEw:someSubvIEw];}// --------------------- (voID)pressedPresentbutton:(ID)sender{    VIEwController2 * presentedVC = [[VIEwController2 alloc] initWithNibname:nil bundle:nil];    presentedVC.modalPresentationStyle = UIModalPresentationCustom;    presentedVC.TransitioningDelegate = self;    [self presentVIEwController:presentedVC animated:YES completion:nil];}// --------------------// VIEw Controller Transitioning Delegate Methods.- (ID <UIVIEwControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIVIEwController *)presented presentingController:(UIVIEwController *)presenting sourceController:(UIVIEwController *)source{    return [[PresentAnimationController alloc] init];;}- (ID <UIVIEwControllerAnimatedTransitioning>)animationControllerFordismissedController:(UIVIEwController *)dismissed{    dismissAnimationController * animationController = [[dismissAnimationController alloc] init];    VIEwController2 * presentedVC = (VIEwController2 *)self.presentedVIEwController;    if (presentedVC.dismissalisInteractive) {        presentedVC.dismissAnimationController = animationController;    }    return animationController;}- (ID <UIVIEwControllerInteractiveTransitioning>)interactionControllerForPresentation:(ID <UIVIEwControllerAnimatedTransitioning>)animator{    return nil;}- (ID <UIVIEwControllerInteractiveTransitioning>)interactionControllerFordismissal:(ID <UIVIEwControllerAnimatedTransitioning>)animator{    VIEwController2 * presentedVC = (VIEwController2 *)self.presentedVIEwController;    if (presentedVC.dismissalisInteractive) {        return presentedVC;    }    else {        return nil;    }}@end

VIEwController2.h

#import <UIKit/UIKit.h>#import "dismissAnimationController.h"@interface VIEwController2 : UIVIEwController <UIVIEwControllerInteractiveTransitioning>@property (weak,nonatomic) UIVIEw * contentVIEw;@property (nonatomic,Readonly) BOol dismissalisInteractive;@property (strong,nonatomic) dismissAnimationController * dismissAnimationController;@end

VIEwController2.m

#import "VIEwController2.h"@interface VIEwController2 ()@property (strong,nonatomic) ID<UIVIEwControllerContextTransitioning> TransitionContext;@end@implementation VIEwController2- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil];    if (self) {        _dismissalisInteractive = NO;    }    return self;}- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    self.vIEw.backgroundcolor = [UIcolor colorWithWhite:0 Alpha:0.5];    // Set up content vIEw.    CGRect frame = UIEdgeInsetsInsetRect(self.vIEw.bounds,UIEdgeInsetsMake(15,15,15));    UIVIEw * contentVIEw = [[UIVIEw alloc] initWithFrame:frame];    self.contentVIEw = contentVIEw;    contentVIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;    contentVIEw.backgroundcolor = [UIcolor cyancolor];    contentVIEw.layer.bordercolor = [UIcolor bluecolor].CGcolor;    contentVIEw.layer.borderWIDth = 2;    [self.vIEw addSubvIEw:contentVIEw];    // Set up pan dismissal gesture recognizer.    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dismissalPan:)];    [self.vIEw addGestureRecognizer:panGesture];}// --------------------- (voID)dismissalPan:(UIPanGestureRecognizer *)panGesture{    switch (panGesture.state) {        case UIGestureRecognizerStateBegan: {            _dismissalisInteractive = YES;            [self.presentingVIEwController dismissVIEwControllerAnimated:YES completion:nil];            break;        }        case UIGestureRecognizerStateChanged: {            CGPoint translation = [panGesture translationInVIEw:self.vIEw];            CGfloat percent;            if (translation.y > 0) {                percent = translation.y / self.vIEw.bounds.size.height;                percent = MIN(percent,1.0);            }            else {                percent = 0;            }            // SwiPing content vIEw down.            CGPoint center;            center.x = CGRectGetMIDX(self.vIEw.bounds);            center.y = CGRectGetMIDY(self.vIEw.bounds);            if (translation.y > 0) {                center.y += translation.y;  // Only allow swiPing down.            }            self.contentVIEw.center = center;            self.vIEw.backgroundcolor = [UIcolor colorWithWhite:0 Alpha:(0.5 * (1.0 - percent))];            [self.TransitionContext updateInteractiveTransition:percent];            break;        }        case UIGestureRecognizerStateEnded: // Fall through.        case UIGestureRecognizerStateCancelled: {            _dismissalisInteractive = NO;            ID<UIVIEwControllerContextTransitioning> TransitionContext = self.TransitionContext;            self.TransitionContext = nil;            dismissAnimationController * dismissAnimationController = self.dismissAnimationController;            self.dismissAnimationController = nil;            CGPoint translation = [panGesture translationInVIEw:self.vIEw];            if (translation.y > 100) {                // Complete dismissal.                [dismissAnimationController animateTransition:TransitionContext];            }            else {                // Cancel dismissal.                voID (^animations)() = ^() {                    CGPoint center;                    center.x = CGRectGetMIDX(self.vIEw.bounds);                    center.y = CGRectGetMIDY(self.vIEw.bounds);                    self.contentVIEw.center = center;                    self.vIEw.backgroundcolor = [UIcolor colorWithWhite:0 Alpha:0.5];                };                voID (^completion)(BOol) = ^(BOol finished) {                    [TransitionContext cancelinteractiveTransition];                    [TransitionContext completeTransition:NO];                };                [UIVIEw animateWithDuration:0.5 delay:0 options:UIVIEwAnimationoptionCurveEaSEOut animations:animations completion:completion];            }            break;        }        default: {            break;        }    }}// --------------------// VIEw Controller Interactive Transitioning Methods.- (voID)startInteractiveTransition:(ID<UIVIEwControllerContextTransitioning>)TransitionContext{    self.TransitionContext = TransitionContext;}@end

PresentAnimationController.h

#import <Foundation/Foundation.h>@interface PresentAnimationController : NSObject <UIVIEwControllerAnimatedTransitioning>@end

PresentAnimationController.m

#import "PresentAnimationController.h"#import "VIEwController2.h"@implementation PresentAnimationController- (voID)animateTransition:(ID <UIVIEwControllerContextTransitioning>)TransitionContext{    UIVIEwController * fromVC = [TransitionContext vIEwControllerForKey:UITransitionContextFromVIEwControllerKey];    VIEwController2 * toVC = (VIEwController2 *)[TransitionContext vIEwControllerForKey:UITransitionContextToVIEwControllerKey];    UIVIEw * containerVIEw = [TransitionContext containerVIEw];    CGPoint toCenter = fromVC.vIEw.center;    CGRect toBounds = fromVC.vIEw.bounds;    toVC.vIEw.center = toCenter;    toVC.vIEw.bounds = toBounds;    [toVC.vIEw layoutIfNeeded];    [containerVIEw addSubvIEw:fromVC.vIEw];    [containerVIEw addSubvIEw:toVC.vIEw];    CGRect contentVIEwEndFrame = toVC.contentVIEw.frame;    CGRect contentVIEwStartFrame = contentVIEwEndFrame;    contentVIEwStartFrame.origin.y += contentVIEwStartFrame.size.height;    toVC.contentVIEw.frame = contentVIEwStartFrame;    UIcolor * endBackgroundcolor = toVC.vIEw.backgroundcolor;    toVC.vIEw.backgroundcolor = [UIcolor clearcolor];    voID (^animations)() = ^() {        toVC.contentVIEw.frame = contentVIEwEndFrame;        toVC.vIEw.backgroundcolor = endBackgroundcolor;    };    voID (^completion)(BOol) = ^(BOol finished) {        toVC.vIEw.autoresizingMask = UIVIEwautoresizingNone;        toVC.vIEw.translatesautoresizingMaskIntoConstraints = NO;        NSLayoutConstraint * centerXConstraint = [NSLayoutConstraint constraintWithItem:toVC.vIEw                                                                              attribute:NSLayoutAttributeCenterX                                                                              relatedBy:NSLayoutRelationEqual                                                                                 toItem:fromVC.vIEw                                                                              attribute:NSLayoutAttributeCenterX                                                                             multiplIEr:1                                                                               constant:0];        NSLayoutConstraint * centerYConstraint = [NSLayoutConstraint constraintWithItem:toVC.vIEw                                                                              attribute:NSLayoutAttributeCenterY                                                                              relatedBy:NSLayoutRelationEqual                                                                                 toItem:fromVC.vIEw                                                                              attribute:NSLayoutAttributeCenterY                                                                             multiplIEr:1                                                                               constant:0];        NSLayoutConstraint * wIDthConstraint = [NSLayoutConstraint constraintWithItem:toVC.vIEw                                                                            attribute:NSLayoutAttributeWIDth                                                                            relatedBy:NSLayoutRelationEqual                                                                               toItem:fromVC.vIEw                                                                            attribute:NSLayoutAttributeWIDth                                                                           multiplIEr:1                                                                             constant:0];        NSLayoutConstraint * heightConstraint = [NSLayoutConstraint constraintWithItem:toVC.vIEw                                                                             attribute:NSLayoutAttributeHeight                                                                             relatedBy:NSLayoutRelationEqual                                                                                toItem:fromVC.vIEw                                                                             attribute:NSLayoutAttributeHeight                                                                            multiplIEr:1                                                                              constant:0];        [containerVIEw addConstraint:centerXConstraint];        [containerVIEw addConstraint:centerYConstraint];        [containerVIEw addConstraint:wIDthConstraint];        [containerVIEw addConstraint:heightConstraint];        [TransitionContext completeTransition:YES];    };    [UIVIEw animateWithDuration:0.5 delay:0 options:UIVIEwAnimationoptionCurveEaSEOut animations:animations completion:completion];}- (NSTimeInterval)TransitionDuration:(ID <UIVIEwControllerContextTransitioning>)TransitionContext{    return 0.5;}@end

dismissAnimationController.h

#import <Foundation/Foundation.h>@interface dismissAnimationController : NSObject <UIVIEwControllerAnimatedTransitioning>@end

dismissAnimationController.m

#import "dismissAnimationController.h"#import "VIEwController2.h"@implementation dismissAnimationController- (voID)animateTransition:(ID <UIVIEwControllerContextTransitioning>)TransitionContext{    VIEwController2 * fromVC = (VIEwController2 *)[TransitionContext vIEwControllerForKey:UITransitionContextFromVIEwControllerKey];    UIVIEwController * toVC = [TransitionContext vIEwControllerForKey:UITransitionContextToVIEwControllerKey];    UIVIEw * containerVIEw = [TransitionContext containerVIEw];    [containerVIEw addSubvIEw:toVC.vIEw];    [containerVIEw addSubvIEw:fromVC.vIEw];    voID (^animations)() = ^() {        CGRect contentVIEwEndFrame = fromVC.contentVIEw.frame;        contentVIEwEndFrame.origin.y = CGRectGetMaxY(fromVC.vIEw.bounds) + 15;        fromVC.contentVIEw.frame = contentVIEwEndFrame;        fromVC.vIEw.backgroundcolor = [UIcolor clearcolor];    };    voID (^completion)(BOol) = ^(BOol finished) {        if ([TransitionContext isInteractive]) {            [TransitionContext finishInteractiveTransition];        }        [TransitionContext completeTransition:YES];    };    [UIVIEw animateWithDuration:0.5 delay:0 options:UIVIEwAnimationoptionCurvelinear animations:animations completion:completion];}- (NSTimeInterval)TransitionDuration:(ID <UIVIEwControllerContextTransitioning>)TransitionContext{    return 0.5;}@end

AppDelegate.m

#import "AppDelegate.h"#import "VIEwController1.h"@implementation AppDelegate- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    VIEwController1 * vc = [[VIEwController1 alloc] initWithNibname:nil bundle:nil];    UINavigationController * nav = [[UINavigationController alloc] initWithRootVIEwController:vc];    self.window.rootVIEwController = nav;    [self.window makeKeyAndVisible];    return YES;}@end
解决方法 我想我找到了你的问题.在PresentAnimationController.m中指定toVC.vIEw.translatesautoresizingMaskIntoConstraints = NO;并在您设置的完成块中设置所有约束
– (voID)animateTransition:

注释掉线和所有约束和addConstraint:调用它应该工作

编辑:

只是在手势被取消时才看到它有效,而不是在最初显示视图时.注释完成块中的所有内容除外

[TransitionContext completeTransition:YES];

总结

以上是内存溢出为你收集整理的ios – 取消呈现视图控制器的交互式解雇转换后,视图控制器不会自动旋转全部内容,希望文章能够帮你解决ios – 取消呈现视图控制器的交互式解雇转换后,视图控制器不会自动旋转所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104143.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存