如何让viewForZoomingInScrollView在Xcode中跨多个UIViewControllers工作?

时间:2017-02-18 02:52:38

标签: ios objective-c xcode uiscrollview xcode8

我对Xcode相对较新,我正在使用几个viewControllers在Objective C中创建一个应用程序,每个viewControllers都包含一个UIScrollView,其中包含一个可以缩放和滚动的UIImage。

此代码适用于第一个图像(皮刀),但我无法弄清楚如何调整UIView以在第二个图像(anatomicPlanes)上启用缩放和滚动。目前第二个图像正确导入到第二个UIScrollView,但是当我尝试缩放时,它只是向下跳跃并且在那里保持静止。

UIScrollViews的大小是使用Interface Builder设置的,没有问题。

viewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate> {

    IBOutlet UIScrollView *dermatomeScrollView;
    UIImageView *dermatomesImageView;
    IBOutlet UIScrollView *anatomicPlaneScrollView;
    UIImageView *anatomicPlanesImageView;
}

@property (strong, nonatomic) IBOutlet UIScrollView *dermatomeScrollView;

@property (strong, nonatomic) IBOutlet UIImageView *dermatomesImageView;

@property (strong, nonatomic) IBOutlet UIImageView *anatomicPlanesImageView;

@property (strong, nonatomic) IBOutlet UIScrollView *anatomicPlaneScrollView;

@end

viewController.m

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

@synthesize dermatomeScrollView, anatomicPlanesImageView, dermatomesImageView, anatomicPlaneScrollView;

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return dermatomesImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *dermatomes = [[UIImageView alloc]
                                  initWithImage:[UIImage imageNamed:@"dermatomes.jpg"]];

    self.dermatomesImageView = dermatomes;
    dermatomeScrollView. maximumZoomScale = 1.2;
    dermatomeScrollView. minimumZoomScale = 0.4;
    dermatomeScrollView. delegate = self;
    [dermatomeScrollView addSubview:dermatomesImageView];
    dermatomeScrollView.zoomScale = 0.6;

    UIImageView *planes = [[UIImageView alloc]
                               initWithImage:[UIImage imageNamed:@"anatomic planes.jpg"]];
    self.anatomicPlanesImageView = planes;
    anatomicPlaneScrollView. maximumZoomScale = 1.2;
    anatomicPlaneScrollView. minimumZoomScale = 0.4;
    anatomicPlaneScrollView. delegate = self;
    [anatomicPlaneScrollView addSubview:anatomicPlanesImageView];
    anatomicPlaneScrollView.zoomScale = 0.6;
}

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要根据请求的滚动视图返回正确的视图。委托模式中的每个方法都传递一个对方法调用的发起者的引用,因此您的委托实现可以以不同方式处理它。在这种情况下,委托方法调用的发起者是滚动视图:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (scrollView == dermatomeScrollView) {
        return dermatomesImageView;
    }

    return anatomicPlanesImageView;
}

如果添加更多滚动视图,则必须进一步扩展此方法。