这个例子实现滚动视图,并可以对单个视图缩放
代码下载:http://pan.baidu.com/s/1nt0mZUD
MyScroolView.h
#import <UIKit/UIKit.h> @interface MyScroolView : UIScrollView<UIScrollViewDelegate> @property (strong , nonatomic) UIImageView* m_imageView; @end
MyScroolView.m
// // MyScroolView.m // ScroolView例子 // // Created by 杜甲 on 14-3-19. // Copyright (c) 2014年 杜甲. All rights reserved. // #import "MyScroolView.h" #define MRScreenWidth CGRectGetWidth([UIScreen mainScreen].applicationFrame) #define MRScreenHeight CGRectGetHeight([UIScreen mainScreen].applicationFrame) @implementation MyScroolView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.delegate = self; self.frame = CGRectMake(0, 0, MRScreenWidth, MRScreenHeight); [self initImageView]; } return self; } -(void)initImageView { self.m_imageView = [[UIImageView alloc] init]; self.m_imageView.frame = CGRectMake(0, 0, MRScreenWidth * 2.5, MRScreenHeight * 2.5); [self addSubview:self.m_imageView]; float minimumScale = self.frame.size.width / self.m_imageView.frame.size.width; [self setMinimumZoomScale:minimumScale]; [self setZoomScale:minimumScale]; } #pragma mark - UIScrollViewDelegate - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.m_imageView; } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { [scrollView setZoomScale:scale animated:NO]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
ViewController.h
#import <UIKit/UIKit.h> #import "MyScroolView.h" @interface ViewController : UIViewController<UIScrollViewDelegate> @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) MyScroolView *zoomScrollView; @end
ViewController.m
// // ViewController.m // ScroolView例子 // // Created by 杜甲 on 14-3-19. // Copyright (c) 2014年 杜甲. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; _scrollView.delegate = self; _scrollView.pagingEnabled = YES; _scrollView.userInteractionEnabled = YES; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:_scrollView]; [_scrollView setContentSize:CGSizeMake(320 * 3, 460)]; for (int i = 0; i < 3; i ++) { _zoomScrollView =[[MyScroolView alloc] init]; CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * i; frame.origin.y = 0; _zoomScrollView.frame = frame; _zoomScrollView.m_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]]; [self.scrollView addSubview:_zoomScrollView]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end