UIScrollView可以缩放,但不能平移

时间:2013-06-19 06:19:42

标签: ios objective-c

我有一个内部有UIView的UIScrollView,在UIView旁边我创建了一个按钮来添加textLabels。 理想情况下,我想要一个非常大的画布,并能够在其上放置文字并平移和缩放。然而,UIScrollView它会缩放,但根本不会平移

似乎当我删除UIView中我添加的UIScrollView时,它可以正常工作。

heres viewDidLoad:

[super viewDidLoad];

CGFloat mainViewWidth = 700;
CGFloat mainViewHeight = 500;

//scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height * kNumberOfPages);
//self.mainScrollView.bounds = CGRectMake(0., 0., 3000, 3000);
self.mainScrollView.scrollsToTop = NO;
self.mainScrollView.delegate = self;
self.mainScrollView.maximumZoomScale = 50.;
self.mainScrollView.minimumZoomScale = .1;
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;


self.mainView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, mainViewWidth, mainViewHeight)];
[self.mainView setUserInteractionEnabled:NO];
self.mainView.backgroundColor = [[UIColor alloc] initWithRed:0.82110049709463495
                                                        green:1
                                                        blue:0.95704295882687884
                                                        alpha:1];


[self.mainScrollView setContentSize:CGSizeMake(5000, 5000)];
[self.mainScrollView insertSubview:self.mainView atIndex:0];

编辑: 以下是UIScrollViewDelegate的所有内容

#pragma mark - Scroll View Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender {

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    //
    return self.mainView;
}


- (void)scrollViewDidEndZooming:(UIScrollView *)zoomedScrollView withView:(UIView *)view atScale:(float)scale
{

}

1 个答案:

答案 0 :(得分:1)

我刚刚经历了同样的两难困境。

我的UIScrollView存在于故事板中,如果我在该故事板中添加UIView(containerView)到UIScrollView,则图像无法平移。而且各种其他的中心怪异也会发生。

但如果我通过代码执行:

// Set up the image we want to scroll & zoom
UIImage *image = [UIImage imageNamed:@"plan-150ppi.jpg"];
self.imageView = [[UIImageView alloc] initWithImage:image];

self.containerView = [[UIView alloc] initWithFrame:self.imageView.frame];
[self.containerView addSubview:self.imageView];
[self.scrollView addSubview:self.containerView];

// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.containerView.frame.size;

......然后就可以了。