UIPageViewController在viewControllerAfterViewController之前调用zoom X1

时间:2013-05-22 15:39:33

标签: ios uipageviewcontroller

我想在调用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];
}

EDITED(2X):

我认为UIPanGestureRecognizer存在问题。

在“SecondViewController.view”和“pageViewController”中:PanGesture用于通过“viewControllerAfterViewController”和“viewControlleBeforeViewController”移动到下一个/上一个“ImageViewController”

在“ImageViewController.scrollView”中:当我放大图像时,PanGesture用于移动到特定区域。

我没有以编程方式添加PanGesture。我只想将panGesture保留在scrollView中,这是在图像中移动的责任。并保持另一个panGesture在视图级别链接,负责切换页面。我认为它们是Apple的私人方法。

但是当我在scrollView中平移时出现问题。

1 个答案:

答案 0 :(得分:0)

我的一个项目中遇到了类似的问题,我做了什么,我在UIPageViewController的UISwipeGestureRecognizer中添加了一个额外的目标。你可以尝试这个(它可能被其他开发人员认为是黑客,但我不这么认为)。

在viewDidLoad或其他init / setup方法中,您可以尝试以下方法:

for(UIGestureRecognizer *gesture in yourPageViewController.gestureRecognizers) {
     if([gesture isKindOfClass:[UISwipeGestureRecognizer class]]) { 
        [gesture addTarget:self action:@selector(zoomX1)]
     }
}