allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
allImageScrollView.minimumZoomScale = 0.3;
allImageScrollView.maximumZoomScale = 1.0;
allImageScrollView.backgroundColor = [UIColor clearColor];
allImageScrollView.delegate = self;
[self.view addSubview:allImageScrollView];
mPicStatusesViewController = [[PicStatusesViewController alloc] init];
[allImageScrollView addSubview:mPicStatusesViewController.view];
//UIScrollView Delegete 实现
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return mPicStatusesViewController.view; //返回ScrollView上添加的需要缩放的视图
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//缩放操作中被调用
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
//缩放结束后被调用
}
原文地址:http://ios-iphone.diandian.com/post/2011-09-15/5066984
MyScrollView.h
- #import <UIKit/UIKit.h>
- @interface MyScrollView : UIScrollView <UIScrollViewDelegate> {
- UIImage *image;
- UIImageView *imageView;
- }
- @property (nonatomic, retain) UIImage *image;
- @end
MyScrollView.m
- #import "MyScrollView.h"
- @implementation MyScrollView
- @synthesize image;
- - (id)initWithFrame:(CGRect)frame {
- if ((self = [super initWithFrame:frame])) {
- self.delegate = self;
- self.minimumZoomScale = 0.5;
- self.maximumZoomScale = 2.5;
- self.showsVerticalScrollIndicator = NO;
- self.showsHorizontalScrollIndicator = NO;
- imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
- imageView.contentMode = UIViewContentModeCenter;
- [self addSubview:imageView];
- }
- return self;
- }
- - (void)setImage:(UIImage *)img {
- imageView.image = img;
- }
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
- return imageView;
- }
- - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
- CGFloat zs = scrollView.zoomScale;
- zs = MAX(zs, 1.0);
- zs = MIN(zs, 2.0);
- [UIView beginAnimations:nil context:NULL];
- [UIView setAnimationDuration:0.3];
- scrollView.zoomScale = zs;
- [UIView commitAnimations];
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *touch = [touches anyObject];
- if ([touch tapCount] == 2) {
- CGFloat zs = self.zoomScale;
- zs = (zs == 1.0) ? 2.0 : 1.0;
- [UIView beginAnimations:nil context:NULL];
- [UIView setAnimationDuration:0.3];
- self.zoomScale = zs;
- [UIView commitAnimations];
- }
- }
- - (void)dealloc {
- [image release];
- [imageView release];
- [super dealloc];
- }
- @end
MainViewController.h
- #import "MyScrollView.h"
- @interface MainViewController : UIViewController <UIScrollViewDelegate> {
- IBOutlet UIScrollView *scrView;
- NSInteger lastPage;
- }
- @end
MainViewController.m
- #import "MainViewController.h"
- @implementation MainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor blackColor];
- scrView.contentSize = CGSizeMake(1700, 480);
- scrView.showsHorizontalScrollIndicator = NO;
- for (int i = 0; i < 5; i++) {
- MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(340 * i, 0, 320, 480)];
- NSString *imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", i + 1] ofType:@"jpg"];
- ascrView.image = [UIImage imageWithContentsOfFile:imgPath];
- ascrView.tag = 100 + i;
- [scrView addSubview:ascrView];
- [ascrView release];
- }
- lastPage = 0;
- }
- //划动的动画结束后调用
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- CGFloat pageWidth = scrollView.frame.size.width;
- NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
- if (lastPage != page) {
- MyScrollView *aView = (MyScrollView *)[scrView viewWithTag:100 + lastPage];
- aView.zoomScale = 1.0;
- lastPage = page;
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- scrView = nil;
- }
- - (void)dealloc {
- [scrView release];
- [super dealloc];
- }
- @end