4

我为我的应用程序编写了一个自定义的 segue,它部分地掩盖了源视图控制器。segue 工作正常,我得到了预期的效果。当我按下按钮将 segue 展开回源控制器时,应用程序崩溃。我在各种方法中放置了一些日志语句,但没有调用任何方法,所以我认为我把视图连接错了。这是segue代码:

- (void)perform {
NSLog(@"performing!");
UIView *sourceView = ((UIViewController *)self.sourceViewController).view;
UIView *destView = ((UIViewController *)self.destinationViewController).view;
UIViewController *sourceController = ((UIViewController *)self.sourceViewController);

if (!_rewinding) {
[sourceView.window insertSubview:destView aboveSubview:sourceView];
    destView.center = CGPointMake(sourceView.frame.size.width * 1.5, sourceView.center.y);
}

CGPoint newSourceCenter = CGPointMake(sourceView.frame.origin.x - sourceView.frame.size.width * 0.4, sourceView.center.y);
CGPoint newDestCenter = CGPointMake(sourceView.center.x + sourceView.frame.size.width * 0.1, sourceView.center.y);


[UIView animateWithDuration:1.4
                 animations:^{
                     if (_rewinding) {
                         NSLog(@"rewind in segu");
                         destView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, destView.center.y);
                         sourceView.center = CGPointMake([UIScreen mainScreen].bounds.size.width*1.5, sourceView.center.y);
                         destView.alpha = 1.0;
                     } else {
                         destView.center = newDestCenter;
                         sourceView.center = newSourceCenter;
                         sourceView.alpha = 0.5;
                     }
                 } completion:^(BOOL finished) {
                     if (_rewinding)
                     NSLog(@"completed animation");
                 }];
}

几张照片显示发生了什么。点击一个单元格时,详细视图从屏幕右侧向左滑动,源视图也向左滑动。

主/源视图 转场之后

我试过使用[source presentViewController:dest],但这样做会从窗口中删除源视图。由于我部分遮挡了源,因此动画后它必须留在屏幕上。设置它的正确方法是什么?

编辑:启用僵尸后,我在日志中得到了这个。这对应于destinationViewController。所以很明显它在segue期间没有被保留。

[58880:c07] *** -[UIStoryboardUnwindSegueTemplate performSelector:withObject:withObject:]: message sent to deallocated instance 0x1f3c7fe0
4

1 回答 1

1

我将用我最终做的事情来回答这个问题,但让它保持开放,因为我对一种在屏幕上留下部分原始视图的转场方式非常感兴趣。

我的快速解决方案是在原始视图控制器中处理所有动画。我认为这涵盖了上述所有与内存相关的问题,但可能不正确,因此使用风险自负。

- (void)showModalDetailView {
NSLog(@"show modal");
if (!self.detailViewController) {
    self.detailViewController = (SegmentDetailViewController *)[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"segDetail"];
    self.detailViewController.view.center = CGPointMake(self.view.frame.size.width * 1.5, self.view.center.y);
    [self.view.superview addSubview:self.detailViewController.view];
    self.detailViewController.delegate = self;
}

CGPoint newSourceCenter = CGPointMake(self.view.frame.origin.x - self.view.frame.size.width * 0.4, self.view.center.y);
CGPoint newDestCenter = CGPointMake(self.view.center.x + self.view.frame.size.width * 0.1, self.view.center.y);

[UIView animateWithDuration:1.4
                 animations:^{
                     if (self.shouldRewind) {
                         self.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, self.view.center.y);
                         self.detailViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width*1.5, self.detailViewController.view.center.y);
                         self.view.alpha = 1.0;
                         self.shouldRewind = NO;
                         self.detailViewController = nil;
                     } else {
                         self.detailViewController.view.center = newDestCenter;
                         self.view.center = newSourceCenter;
                         self.view.alpha = 0.5;
                         self.shouldRewind = YES;
                     }
                 } completion:^(BOOL finished) {
                     NSLog(@"animation complete");

                 }];

}
于 2013-04-10T22:48:38.473 回答