1

我无法放大UIScrollView工作。所以问题是平移工作正常。但是,捏合和缩放不起作用。我假设的原因是我没有代表。

我的方法:

  1. 我尝试使用代表,我想出的唯一解决方案是scrollView.delegate=self

  2. 我知道我需要在 中包含一些东西ViewController.h,但我不知道如何放置@property UIScrollView *scrollView;然后将其连接到缩放功能。

我相信我走在正确的轨道上,但高度赞赏有关在哪里连接的建议。

视图控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

视图控制器.m

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:
    CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    scrollView.delegate = self;
    CGSize containerSize = CGSizeMake(1280, 1280);
    UIView *containerView = [[UIView alloc] initWithFrame:(CGRect)    {.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
    containerView.backgroundColor = [UIColor grayColor ];
    [scrollView addSubview:containerView];


    // Set up our custom view hierarchy
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640.0f, 80.0f)];
    redView.backgroundColor = [UIColor redColor];
    [containerView addSubview:redView];

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 560.0f, 640.0f, 80.0f)];
    blueView.backgroundColor = [UIColor blueColor];
    [containerView addSubview:blueView];

    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 160.0f, 320.0f, 320.0f)];
    greenView.backgroundColor = [UIColor greenColor];
    [containerView addSubview:greenView];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slow.png"]];
    imageView.center = CGPointMake(320.0f, 320.0f);
    [containerView addSubview:imageView];
    scrollView.panGestureRecognizer.minimumNumberOfTouches=2;
    CGSize containerSize2 = CGSizeMake(640, 640);
    scrollView.contentSize=containerSize2;

    CGRect viewRect = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,  self.view.bounds.size.width, self.view.bounds.size.height);

    [self.view addSubview:scrollView];

}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that we want to zoom
    return self.containerView; //This gives error. 
}
4

3 回答 3

0

问题很可能是您没有添加viewForZoomingInScrollView.

UIScrollView 类可以有一个必须采用 UIScrollViewDelegate 协议的委托。要使缩放和平移工作,代理必须同时实现 viewForZoomingInScrollView: 和 scrollViewDidEndZooming:withView:atScale:; 另外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同。

所以添加这个:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

从此处的文档中阅读更多信息

于 2013-02-24T04:29:52.987 回答
0

只需提供适当的缩放比例。

scrollView.maximumZoomScale = 2.0; //Anything greater than 1.0 will allow you to zoom in
于 2013-02-24T05:24:05.320 回答
0

解决了我的问题的答案:在 ViewRect() 之后的 viewDidLoad() 中添加此代码

CGRect viewRect = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,  self.view.bounds.size.width, self.view.bounds.size.height);

CGRect scrollViewFrame = scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);

scrollView.minimumZoomScale = minScale;
scrollView.maximumZoomScale = 1.0f;
scrollView.zoomScale = minScale;

这应该是 viewController.h 的样子:

@interface ViewController ()
@property UIView *containerView;
@end
@implementation ViewController

@synthesize containerView = _containerView;
于 2013-02-24T19:34:57.853 回答