UI 運用滾動視圖創建相冊的方法

UI 運用滾動視圖創建相冊的方法:



#import "mainViewController.h"


@interface mainViewController ()


@end


@implementation mainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

   //總的

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 450)];

    scrollView.backgroundColor = [UIColor redColor];

    scrollView.tag = 1000;

    scrollView.contentSize = CGSizeMake(1400, 0);

    scrollView.scrollEnabled = YES;

    scrollView.pagingEnabled = YES;

    //第一個

    UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 240, 450)];

    scrollView1.contentSize = CGSizeMake(240, 0);

    scrollView1.tag = 111;

    UIImageView *image0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)];

    image0.image = [UIImage imageNamed:@"1.JPG"];

    [scrollView1 addSubview:image0];

    [image0 release];

    scrollView1.delegate = self;

    scrollView1.minimumZoomScale = 0.5;

    scrollView1.maximumZoomScale = 2;

    [scrollView addSubview:scrollView1];

    [scrollView1 release];

    //第二個

    UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(300, 20, 240, 450)];

    scrollView2.contentSize = CGSizeMake(280, 0);

    scrollView2.tag = 222;

    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)];

    image1.image = [UIImage imageNamed:@"2.JPG"];

    [scrollView2 addSubview:image1];

    [image1 release];

    

    scrollView2.delegate = self;

    scrollView2.minimumZoomScale = 0.5;

    scrollView2.maximumZoomScale = 2;

    [scrollView addSubview:scrollView2];

    [scrollView2 release];

    //第三個

    UIScrollView *scrollView3 = [[UIScrollView alloc] initWithFrame:CGRectMake(580, 20, 240, 450)];

    scrollView3.contentSize = CGSizeMake(280, 0);

    scrollView3.tag = 333;

    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)];

    image2.image = [UIImage imageNamed:@"3.JPG"];

    [scrollView3 addSubview:image2];

    [image2 release];


    scrollView3.delegate = self;

    scrollView3.minimumZoomScale = 0.5;

    scrollView3.maximumZoomScale = 2;

    [scrollView addSubview:scrollView3];

    [scrollView3 release];

    //第四個

    UIScrollView *scrollView4 = [[UIScrollView alloc] initWithFrame:CGRectMake(860, 20, 240, 450)];

    scrollView4.contentSize = CGSizeMake(280, 0);

    scrollView4.tag = 444;

    UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)];

    image3.image = [UIImage imageNamed:@"4.JPG"];

    [scrollView4 addSubview:image3];

    [image3 release];

    scrollView4.delegate = self;

    scrollView4.minimumZoomScale = 0.5;

    scrollView4.maximumZoomScale = 2;

    [scrollView addSubview:scrollView4];

    [scrollView4 release];

    //第五個

    UIScrollView *scrollView5 = [[UIScrollView alloc] initWithFrame:CGRectMake(1140, 20, 240, 450)];

    scrollView5.contentSize = CGSizeMake(280, 0);

    scrollView5.tag = 555;

    UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)];

    image4.image = [UIImage imageNamed:@"5.JPG"];

    [scrollView5 addSubview:image4];

    [image4 release];

    scrollView5.delegate = self;

    scrollView5.minimumZoomScale = 0.5;

    scrollView5.maximumZoomScale = 2;

    [scrollView addSubview:scrollView5];

    [scrollView5 release];


    scrollView.delegate = self;

    [self.view addSubview:scrollView];

    [scrollView release];

    

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(110, 400, 100,30)];

    pageControl.numberOfPages = 5;

    pageControl.pageIndicatorTintColor = [UIColor blackColor];

    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

    [pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:pageControl];

    [pageControl release];

    

    self.page = pageControl;

    

    

}

- (void)pageControlAction:(UIPageControl *)pageControl

{

    UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:1000];

    scrollView.contentOffset = CGPointMake(280 * self.page.currentPage, 0);

    for (int i = 1; i < 6; i++) {

        UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111 * i];

        scrollView.zoomScale = 1;

    }

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return [scrollView.subviews firstObject];

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    for (int i = 1; i < 6; i ++) {

        UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111 * i];

        scrollView.zoomScale = 1;

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#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


發佈了27 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章