RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
#import "ImagescrollView.h"
@interface RootViewController () <UIScrollViewDelegate>
{
NSInteger _startIndex;
}
#pragma mark - 声明私有属性
@property (nonatomic, retain) RootView *rootView;
@end
@implementation RootViewController
#pragma mark - 自定义视图 代替 自带视图
- (void)loadView
{
self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.view = _rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置代理
_rootView.scrollView.delegate = self;
}
#pragma mark - 代理方法
#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
_startIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
}
#pragma mark - 结束自由滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//获取当前页面
NSInteger currentIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
//如果不是当前页面
if (currentIndex == _startIndex) {
return;
}
//给数组中的每个元素都发送 setScaleone消息 perform执行完成
[scrollView.subviews makeObjectsPerformSelector:@selector(setScaleOne)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 重写
#pragma mark - dealloc
-(void)dealloc
{
[_rootView release];
[super dealloc];
}
RootView.m
#import "RootView.h"
#import "ImageScrollView.h"
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor cyanColor];
[self addAllViews];
}
return self;
}
#pragma mark - 添加全部控件
- (void)addAllViews
{
// 添加scrollView
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.frame] autorelease];
[self addSubview:_scrollView];
// 图片总数
NSInteger count = 9;
// for循环,添加ImageScrollView
for (int i = 0; i < count; i++) {
NSString *imgName = [NSString stringWithFormat:@"bg_%d.JPG", i];
UIImage *image = [UIImage imageNamed:imgName];
ImageScrollView *imageScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) image:image];
[_scrollView addSubview:imageScrollView];
[imageScrollView release];
}
// 设置scrollView的一些属性
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * count, _scrollView.frame.size.height);
_scrollView.pagingEnabled = YES;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
}
#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
[_scrollView release];
[super dealloc];
}
ImageScollView.m
#import "ImageScrollView.h"
@interface ImageScrollView () <UIScrollViewDelegate>
@property (nonatomic, retain) UIImageView *imageView;
@end
@implementation ImageScrollView
- (instancetype)initWithFrame:(CGRect)frame
image:(UIImage *)image
{
if (self = [super initWithFrame:frame]) {
// 初始化图片
self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
_imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
[self addSubview:_imageView];
// 设置自身为代理
self.delegate = self;
// 设置最大和最小缩放比例
self.minimumZoomScale = 0.5;
self.maximumZoomScale = 2;
}
return self;
}
#pragma mark - 代理方法
#pragma mark 设置缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imageView;
}
#pragma mark 缩放结束,修改到中心
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view
atScale:(CGFloat)scale
{
CGRect frame = view.frame;
if (scrollView.frame.size.width < scrollView.contentSize.width) {
frame.origin.x = 0;
} else {
frame.origin.x = (scrollView.frame.size.width - scrollView.contentSize.width) / 2;
}
if (scrollView.frame.size.height < scrollView.contentSize.height) {
frame.origin.y = 0;
} else {
frame.origin.y = (scrollView.frame.size.height - scrollView.contentSize.height) / 2;
}
view.frame = frame;
}
#pragma mark 设置视图恢复到原来的位置
- (void)setScaleOne
{
CGRect frame = _imageView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
_imageView.frame = frame;
self.zoomScale = 1;
}
#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
[_imageView release];
[super dealloc];
}