=======10.5 UIScrollview概念和用法:实现简单的相册功能====
思路:
在UIScrollView上再加上一个小的UIScrollView,而在小的UIScrollView上有ImageView支持图片缩放;
测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *view = [[RootViewController alloc] init];
self.window.rootViewController = view;
return YES;
}
//
// RootViewController.m
// my_ScrollView
//
// Created by fenghuo on 14-7-21.
// Copyright (c) 2014年 fenghuo. All rights reserved.
//
#import "RootViewController.h"
#import "ImageScrollView.h"
@interface RootViewController () <UIScrollViewDelegate>
{
UIScrollView *_scrollView;
UIImageView *_imageView;
}
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// base scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 460)];
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.tag = INT_MAX;
_scrollView.showsHorizontalScrollIndicator = NO;
// 设置内容大小
_scrollView.contentSize = CGSizeMake(340*4, 460);
[self.view addSubview:_scrollView];
int _x = 0;
for (int index = 0; index < 4; index++) {
ImageScrollView *imgScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(0+_x, 0, 320, 460)];
imgScrollView.tag = index;
NSString *imgName = [NSString stringWithFormat:@"%d.jpg", index+1];
imgScrollView.imageView.image = [UIImage imageNamed:imgName];
[_scrollView addSubview:imgScrollView];
_x += 340;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 记录之前的次数
int pre = 0;
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 当前第几张图片;
int current = scrollView.contentOffset.x / 340;
// 将图片缩放还原
ImageScrollView *imgScrollView = (ImageScrollView *)[scrollView viewWithTag:pre];
if (current != pre && imgScrollView.zoomScale > 1) {
imgScrollView.zoomScale = 1;
}
pre = current;
}
@end
=======ImageScrollView.h===========
#import <UIKit/UIKit.h>
// 支持单张图片缩放
@interface ImageScrollView : UIScrollView <UIScrollViewDelegate>
@property (nonatomic, retain) UIImageView *imageView;
@end
=======ImageScrollView.m===========
//
// ImageScrollView.m
// my_ScrollView
//
// Created by fenghuo on 14-7-23.
// Copyright (c) 2014年 fenghuo. All rights reserved.
//
#import "ImageScrollView.h"
@implementation ImageScrollView
@synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 设置scrollView的属性
self.maximumZoomScale = 2.5;
self.minimumZoomScale = 1;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
// 添加图片
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:_imageView];
// 设置代理
self.delegate = self;
// 添加双击事件
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInOrOut:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
}
return self;
}
#pragma mark - UIScrollView Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imageView;
}
#pragma mark - Target Action
- (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture
{
if (self.zoomScale >= 2.5) {
[self setZoomScale:1 animated:YES];
}else {
// 得到相应的双击区域,然后缩放相应的区域
CGPoint point = [tapGesture locationInView:self];
// -40,指缩放到相应的中心点
[self zoomToRect:CGRectMake(point.x - 40, point.y - 40, 80, 80) animated:YES];
}
}
@end
结果展示: