首页 > 代码库 > 简易图片浏览器【可缩放图片,滑动后恢复正常】

简易图片浏览器【可缩放图片,滑动后恢复正常】

AppDelegate.h

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (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 *rootCtrl = [[RootViewController alloc] init];
    
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:rootCtrl];
    
    NSMutableArray *mutArrary = [[NSMutableArray alloc] init];
    
    for (int i=0; i<5; i++)
    {
        NSString *imgName = [NSString stringWithFormat:@"%d.JPG",i];
        UIImage *image = [UIImage imageNamed:imgName];
        [mutArrary addObject:image];
    }
    
    rootCtrl.images = mutArrary;
    
    self.window.rootViewController = navCtrl;
    
    return YES;
}


@end
RootViewController.h

@interface RootViewController : UIViewController<UIScrollViewDelegate>
{
    NSInteger _index;
}

@property(nonatomic, retain)NSArray *images;    //存放显示的图片

RootViewController.m

#import "RootViewController.h"
#import "PhotoScrollView.h"

@interface RootViewController ()

@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];

    //创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 480)];
    //隐藏水平滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    //设置分页效果
    scrollView.pagingEnabled = YES;
    
    //设置内容尺寸
    scrollView.contentSize = CGSizeMake(340*_images.count, 480);
    [self.view addSubview:scrollView];
    
    for (int i=0; i<self.images.count; i++) {
        PhotoScrollView *photoView = [[PhotoScrollView alloc] initWithFrame:CGRectMake(340*i, 0, 320, 480)];
        photoView.tag = i + 100;
        //传值
        photoView.image =_images[i];
        [scrollView addSubview:photoView];
    }
    
}

#pragma mark - UIScrollView delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {


    //1.获取当前的页数
    int currentPage = scrollView.contentOffset.x/340;
    
    //2.还原以前的缩放试图
    if (currentPage != _index) {
        //取得视图
        int tag = _index + 100;
        PhotoScrollView *view = (PhotoScrollView *)[scrollView viewWithTag:tag];
        
        //还原
        [view setZoomScale:1];
    }
    
    
    //3.记录当前的页数
    _index = currentPage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

PhotoScrollView.h

@interface PhotoScrollView : UIScrollView<UIScrollViewDelegate>
{
    UIImageView *_imageView;
}

@property(nonatomic, retain)UIImage *image;

PhotoScrollView.m

#import "PhotoScrollView.h"

@implementation PhotoScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        
        //数据不能在这里设置
//        _imageView.image = _image;
        
        [self addSubview:_imageView];
        
        //设置最大放大倍数
        self.maximumZoomScale = 2.0;
        //设置最小缩小倍数
        self.minimumZoomScale = .5;
        
        //隐藏滚动条
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        //设置代理
        self.delegate = self;
        
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActoin:)];
        tap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

//手势响应事件
- (void)tapActoin:(UITapGestureRecognizer *)tap {

    if (self.zoomScale > 1) {
        //缩小
        [self setZoomScale:1 animated:YES];
    }else {
        //放大
        [self setZoomScale:2 animated:YES];
    }
    
}

- (void)setImage:(UIImage *)image {

    _image = image;
    
    _imageView.image = _image;
}

#pragma mark - UIScrollView delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return _imageView;
}



@end



简易图片浏览器【可缩放图片,滑动后恢复正常】