iPhone相冊,(UIScrollView,UIPageControl的綜合應用)捏合放大縮小, 左右滑動切換圖片

<pre name="code" class="objc">//
//  PhotoView.h
//  Homework_iPhonePhoto
//
//  Created by lanouhn on 14-8-29.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PhotoView : UIView
@property (nonatomic, retain) NSMutableArray *scrollViews;
@property (nonatomic, retain) UIPageControl *pageControl;
- (id)initWithFrame:(CGRect)frame images:(NSMutableArray *)imageArr;
@end
//
//  PhotoView.m
//  Homework_iPhonePhoto
//
//  Created by lanouhn on 14-8-29.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "PhotoView.h"

@interface PhotoView ()
{
    NSMutableArray *_imageArr;
    NSUInteger _count;
}
@end

@implementation PhotoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupScrollView];
        [self setupPageControlView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame images:(NSMutableArray *)imageArr
{
    _imageArr = imageArr;
    _count = _imageArr.count;

    self.scrollViews = [NSMutableArray arrayWithCapacity:_count];
    [self initWithFrame:frame];
    return self;
}

- (void)setupScrollView
{
    UIScrollView *bigScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    bigScrollView.contentSize = CGSizeMake(320 * _count , [UIScreen mainScreen].bounds.size.width);
    bigScrollView.tag = 100;
    bigScrollView.showsHorizontalScrollIndicator = NO;
    bigScrollView.pagingEnabled = YES;
    [self addSubview:bigScrollView];
    [bigScrollView release];
    
    for (int i = 0; i < _count; i++) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(320 * i, 0, bigScrollView.bounds.size.width, bigScrollView.bounds.size.height)];
        scrollView.contentSize = [UIScreen mainScreen].bounds.size;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.maximumZoomScale = 4.0;
        scrollView.minimumZoomScale = 1.0;
        [bigScrollView addSubview:scrollView];
        [self.scrollViews addObject:scrollView];
        [scrollView release];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
        imageView.image = _imageArr[i];
        imageView.tag = 200 + i;
        [scrollView addSubview:imageView];
        [imageView release];
    }
}

- (void)setupPageControlView
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 40, 300, 20)];
    _pageControl.numberOfPages = _count;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self addSubview:_pageControl];
    [_pageControl release];
}

- (void)dealloc
{
    self.scrollViews = nil;
    self.pageControl = nil;
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
//
//  RootViewController.m
//  Homework_iPhonePhoto
//
//  Created by lanouhn on 14-8-29.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "RootViewController.h"
#import "PhotoView.h"
@interface RootViewController ()<UIScrollViewDelegate>
{
    PhotoView *_photoView;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"girl_%d", i + 1] ofType:@"jpg"]];
        [images addObject:image];
    }
    PhotoView *photoView = [[PhotoView alloc] initWithFrame:[UIScreen mainScreen].bounds images:images];
    photoView.backgroundColor = [UIColor whiteColor];
    self.view = photoView;
    [photoView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIScrollView *bigScrollView = (UIScrollView *)[self.view viewWithTag:100];
    bigScrollView.delegate = self;
    
    _photoView = (PhotoView *)self.view;
    NSUInteger count = _photoView.scrollViews.count;
    for (int i = 0; i < count; i++) {
        UIScrollView *scrollView = _photoView.scrollViews[i];
        scrollView.delegate = self;
        scrollView.scrollsToTop = YES;
    }
    
    [_photoView.pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIScrollView *bigScrollView = (UIScrollView *)[_photoView viewWithTag:100];
    if (bigScrollView.contentOffset.x / 320 != _photoView.pageControl.currentPage) {
        [_photoView.scrollViews[_photoView.pageControl.currentPage] setZoomScale:1.0 animated:YES];
    }
    _photoView.pageControl.currentPage = bigScrollView.contentOffset.x / 320;
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"top");
    return YES;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView viewWithTag:200 + _photoView.pageControl.currentPage ];
}

- (void)handlePageControl:(UIPageControl *)pageControl
{
    NSLog(@"%d", pageControl.currentPage);
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];
    [scrollView setContentOffset:CGPointMake(320 * pageControl.currentPage, 0) animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if (!self.view.window && [self isViewLoaded]) {
        self.view = nil;
    }
}

/*
#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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章