UIScrollView SetZoomScale不起作用

Jos*_*ose 2 xcode zoom uiscrollview ios

在阅读了一些关于它的其他猜测后,我尝试过:
在ViewController.m上

DelegateScrollView ScrollView;

-(void)viewDidLoad
{

    [super viewDidLoad];
    ScrollView = [[DelegateScrollView alloc] initWithFrame:CGRectMake(0,-   self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    ScrollView.delegate = ScrollView;
    ScrollView.scrollEnabled = YES;
    ScrollView.backgroundColor = [UIColor blackColor ];
    ScrollView.maximumZoomScale = 5.0f;
    ScrollView.minimumZoomScale = 1.0f;
    [self.view addSubview:ScrollView];
    [ScrollView setZoomScale:2 animated:YES];

     }
Run Code Online (Sandbox Code Playgroud)

DelegateScrollView.h:

@interface ChildDelegateScrollView : UIScrollView <UIScrollViewDelegate>

@end 
Run Code Online (Sandbox Code Playgroud)

并且zoom也永远不会发生我也在ViewController.h中试过这个:@interface ViewController : UIViewController <UIScrollViewDelegate> 然后像这样设置委托ScrollView.delegate = self;而不起作用,如何正确设置ScrollView的委托?

Jos*_*den 11

来自UIScrollView类引用的这一行似乎是相关的:

要使缩放和平移工作,委托必须实现viewForZoomingInScrollView:和scrollViewDidEndZooming:withView:atScale:

我能够在UIScrollView上获取UILabel,以使用此UIViewController代码进行缩放:

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate> {
    UIScrollView *_scrollView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    scrollView.delegate = self;
    scrollView.scrollEnabled = YES;
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.maximumZoomScale = 5.0f;
    scrollView.minimumZoomScale = 1.0f;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    [self.view addSubview:scrollView];
    _scrollView = scrollView;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 50.0, 100.0, 25.0)];
    label.backgroundColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"Test";
    [_scrollView addSubview:label];
}

- (void)viewDidAppear:(BOOL)animated
{
    [_scrollView setZoomScale:4.0 animated:YES];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _scrollView.subviews.firstObject;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{

} 

@end
Run Code Online (Sandbox Code Playgroud)