gpt4 book ai didi

iphone - 从动画 viewWillAppear 停止 transitionFormViewController

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:44 26 4
gpt4 key购买 nike

我正在尝试组合一个简单的容器 View Controller ,它允许我从任何方向呈现 subview Controller (像标准导航 Controller 一样从右侧,也从顶部、底部和左侧放置。

针对 iOS 5 进行开发,我使用父/ subview Controller 关系,并使用带有自定义动画 block 的 transitionFromViewController 在 View 中滑动。

所有消息(viewWillAppear 等)都正确传递给 subview Controller ,但问题是,例如 viewWillAppear 似乎是从动画循环内部调用的(即当我设置 subview Controller View 的动画属性 - 如 backgroundColor 或 subview 的框架,它们实际上是动画的)。我不想要这种行为,因为我想在显示 View Controller 之前使用 viewWillAppear 方法执行初始化。我不希望此代码具有动画效果。

这是我的代码的简化版本:

@implementation ContainerViewController
@synthesize currentViewController;

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];

self.currentViewController = [[SimpleViewController alloc] init];
[self addChildViewController:self.currentViewController];
self.currentViewController.view.frame = self.view.bounds;
[self.view addSubview:self.currentViewController.view];
}

- (void) slideViewController {
UIViewController* previousViewController = self.currentViewController;

UIViewController* viewController = [[SimpleViewController alloc] init];
viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
[self addChildViewController:viewController];
[previousViewController willMoveToParentViewController:nil];

[self transitionFromViewController:previousViewController toViewController:viewController duration:1.0 options:UIViewAnimationOptionTransitionNone animations:^{
viewController.view.frame = self.view.bounds;
} completion:^(BOOL finished) {
[previousViewController removeFromParentViewController];
[viewController didMoveToParentViewController:self];
}];

self.currentViewController = viewController;
}

- (void) loop {
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self slideViewController];
[self loop];
});
}

- (void)viewDidAppear:(BOOL)animated {
[self loop];
}

@end


@implementation SimpleViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated {
CGFloat red = (arc4random()%256)/255.0;
CGFloat green = (arc4random()%256)/255.0;
CGFloat blue = (arc4random()%256)/255.0;
self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

@end

如果您尝试运行这段代码,您会在右侧看到一个新的 View Controller 幻灯片。问题是该 View Controller 的 View 的 backgroundColor 从白色动画变为随机颜色(请参阅 SimpleViewControllerviewWillAppear 方法)。我希望 View 的 backgroundColor 可以立即设置而不是动画。

最佳答案

我找到了一个解决方法,通过禁用 viewWillAppear 方法中的属性动画(有关详细信息,请参阅 Disabling implicit animations in -[CALayer setNeedsDisplayInRect:] 线程)

代码可以包装在 CATransaction 中以在没有动画的情况下提交更改。

- (void)viewWillAppear:(BOOL)animated {
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

CGFloat red = (arc4random()%256)/255.0;
CGFloat green = (arc4random()%256)/255.0;
CGFloat blue = (arc4random()%256)/255.0;

self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

[CATransaction commit];
}

但是我想知道是否有其他人遇到过这个问题,是否有更好的方法来解决它?

关于iphone - 从动画 viewWillAppear 停止 transitionFormViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com