UIScrollView缩放后无法滚动

4

我似乎已经实现了所有正确的代码。我的代码中是否有什么遗漏?

//
//  ScrollyiPadViewController.m
//  ScrollyiPad
//
//  Created by Sidwyn Koh on 5/19/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "ScrollyiPadViewController.h"

@implementation ScrollyiPadViewController

-(void)viewDidLoad{
    [self.view addSubview:scrollView];
    [scrollView addSubview:imageView];

    UIImage *image = [UIImage imageNamed:@"hardware-01-20100127.jpg"];
    scrollView.contentSize = [image size];
    scrollView.maximumZoomScale = 2.0;
//  scrollView.delegate = self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}

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



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
4个回答

16

我遇到了类似的问题,在缩放后,平移操作失效。后来发现在代理函数'scrollViewDidEndZooming:withView:atScale:'中需要根据传递的'atScale'更新ScrollView的contentsize属性。

代码大概是这样的: self.scrollView.contentSize = CGSizeMake(self.imageView.image.size.width*scale, self.imageView.image.size.height*scale);

self.imageView.frame会根据缩放比例自动更新,因此不需要根据缩放比例调整frame大小。

如果没有显式地设置scrollView的contentsize,则它会被赋值为imageView的frame大小...这就是导致imageView无法滚动的原因。


1

我会

scrollView.contentSize = imageView.frame.size;

而不是

scrollView.contentSize = [image size];

imageView 没有这样的属性 :(我也尝试了 imageView.bounds.size 和 imageView.frame.size,但都不起作用(你甚至无法滚动/缩放) - Sidwyn Koh
抱歉,我修正了我的答案,应该是imageView.frame.size。 - mvds

1
也许你需要在UIScrollView中设置ImageView的框架; < p > < code > imageView.frame = CGRectMake(0, 0, imageViewer.image.size.width, imageViewer.image.size.height);


-1
找到了,我忘记设置clipstoBounds=YES。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接