我想在调用 viewControllerAfterViewController 之前重置应用于 imageView/scrollView 的缩放因子。我有一个 UIPageViewController“SecondViewController”和另一个 UIViewController“ImageViewController”。为了解释层次结构,我更喜欢显示一些代码:
#import "ImageViewController.h"
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize pageViewController;
@synthesize imgModelArray;
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return imgModelArray.count;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self view] setBackgroundColor:[UIColor colorWithRed: 40/255.0 green:40/255.0 blue:40/255.0 alpha:1.0]];
// Init model and pageViewController
self.imgModelArray = [NSMutableArray arrayWithObjects:
[[ImageModel alloc] initWith:@"piaggo.jpg":@"softCat"],
[[ImageModel alloc] initWith:@"crumble.jpg":@"funnyDog"],
[[ImageModel alloc] initWith:@"piaggo.jpg":@"sleepingCat"],
[[ImageModel alloc] initWith:@"crumble.jpg":@"goodDog"],
[[ImageModel alloc] initWith:@"piaggo.jpg":@"softCat"],
[[ImageModel alloc] initWith:@"crumble.jpg":@"funnyDog"],
[[ImageModel alloc] initWith:@"piaggo.jpg":@"sleepingCat"],
[[ImageModel alloc] initWith:@"crumble.jpg":@"goodDog"], nil];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:50.0f] forKey:UIPageViewControllerOptionInterPageSpacingKey]];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
// Init ImageViewController - Load Model - Create page 1
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.model = [imgModelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:imageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:pageViewController];
[self.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self];
self.view.gestureRecognizers = pageViewController.gestureRecognizers;
}
// Return an ImageViewController with previous data model
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)vcFrom
{
ImageViewController *imgVc = (ImageViewController *)vcFrom;
NSUInteger currentIndex = [imgModelArray indexOfObject:[imgVc model]];
if (currentIndex == 0)
{
return nil;
}
ImageViewController *previousImgViewController = [[ImageViewController alloc] init];
previousImgViewController.model = [imgModelArray objectAtIndex:currentIndex - 1];
return previousImgViewController;
}
// Return an ImageViewController with next data model
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)vcFrom
{
ImageViewController *imgVc = (ImageViewController *)vcFrom;
[imgVc zoomX1];
NSUInteger currentIndex = [imgModelArray indexOfObject:[imgVc model]];
if (currentIndex == imgModelArray.count - 1)
{
return nil;
}
ImageViewController *nextImgViewController = [[ImageViewController alloc] init];
nextImgViewController.model = [imgModelArray objectAtIndex:currentIndex + 1];
return nextImgViewController;
}
@end
我想调用 zoomX1 方法(在 ImageViewController.m 中本地化),就在这个之前:“viewControllerAfterViewController” 什么样的事件可以做到这一点?
当我使用 viewControllerAfterViewController 时,zoomX1 工作得很好。当我在“viewControllerAfterViewController”中调用 zoomx1 时,我的视图正在消失......但是高度和宽度的值不是 0。请帮帮我!
在 ImageViewController.m 中:zoomX1
- (void)zoomX1{
// Figure out the rect we want to zoom to, then zoom to X1
UIImage *currentImage = [UIImage imageNamed:model.imageName];
CGSize currentImgSize = currentImage.size;
CGFloat w = currentImgSize.width;
CGFloat h = currentImgSize.height;
CGFloat x = 0;
CGFloat y = 0;
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrollView zoomToRect:rectToZoomTo animated:YES];
}
编辑(2X):
我认为 UIPanGestureRecognizer 有问题。
在“SecondViewController.view”和“pageViewController”中:PanGesture 用于通过“viewControllerAfterViewController”和“viewControlleBeforeViewController”移动到下一个/上一个“ImageViewController”
在 "ImageViewController.scrollView" 中:当我放大图像时,PanGesture 用于移动到特定区域。
我没有以编程方式添加 PanGesture。我只想将 panGesture 保留在 scrollView 内,它负责在 Image 内移动。并将另一个 panGesture 链接在负责切换页面的视图级别。我认为它们是Apple的私有方法。
但是当我在 scrollView 内平移时出现问题。