我会向导航控制器或窗口添加子视图,但需要一些视图层次结构。
您需要为 themaster
和detail
view 控制器提供一种方法来引用imageView
每个 ivar 应该没问题。
// PSMasterViewController.m
@interface PSMasterViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation PSMasterViewController
@synthesize imageView = _imageView;
// ...
@end
我已将其放入实现中,因为我不想将其公开给主视图控制器,但您的需求可能会有所不同。
同样的detailViewController
// PSDetailViewController.h
@interface PSMasterViewController : UIViewController
@property (nonatomic, strong) UIImageView *imageView;
@end
// PSDetailViewController.m
@implementation PSDetailViewController
@synthesize imageView = _imageView;
// ...
@end
现在假设 imageView 已经在主视图控制器中创建并呈现,我们需要在即将推送时实现一些视图体操
- (void)methodThatPushesTheNextViewController;
{
CGRect destinationFrame = [self.view convertRect:self.imageView.frame toView:self.navigationController.view];
[self.imageView removeFromSuperview];
[self.navigationController.view addSubview:self.imageView];
self.imageView.frame = destinationFrame;
PSDetailViewController *detailViewController = [[PSDetailViewController alloc] init];
detailViewController.imageView = self.imageView;
self.imageView = nil; // The detailViewController should own this now
[self.navigationController pushViewController:detailViewController animated:YES];
}
现在在细节视图控制器中,我们需要做与我们刚刚做的相反的事情,这viewDidAppear:
是一个好地方,尤其是你提到的你可能想要移动和缩放
- (void)viewDidAppear:(BOOL)animated;
{
[super viewDidAppear:animated];
CGRect startFrame = [self.view convertRect:self.imageView.frame fromView:self.navigationController.view];
[self.imageView removeFromSuperview];
[self.view addSubview:self.imageView];
self.imageView.frame = startFrame;
[UIView animateWithDuration:0.25f
animations:^{
self.imageView.frame = [self someNewFrame];
}];
}