// // ViewController.m // UI2_ScrollView&UIPageControl // // Created by zhangxueming on 15/7/10. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" #define ClipsBundleWidth ((self.view.frame.size.width-300)/2) @interface ViewController () { UIScrollView *_scrollView; UIPageControl *_pageControl;//页码 NSInteger _currentIndex;//记录当前显示的页码 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ClipsBundleWidth, 64, 300, 450)]; _scrollView.contentSize = CGSizeMake(300*6, 450); _scrollView.pagingEnabled = YES; for (int i=0; i<6; i++) { NSString *imageName = [NSString stringWithFormat:@"%d",i]; NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(300*i, 0, 300, 450)]; imageView.image = [UIImage imageWithContentsOfFile:path]; [_scrollView addSubview:imageView]; } _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(ClipsBundleWidth, 64+450, 300, 30)]; _pageControl.numberOfPages = 6; _currentIndex = 0; _pageControl.currentPage = _currentIndex; _pageControl.backgroundColor = [UIColor blackColor]; [self.view addSubview:_pageControl]; UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem]; backBtn.frame = CGRectMake(50, 64+450+30, (self.view.frame.size.width-100)/2, 30); [backBtn setTitle:@"back" forState:UIControlStateNormal]; backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [backBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backBtn]; UIButton *forwardBtn = [UIButton buttonWithType:UIButtonTypeSystem]; forwardBtn.frame = CGRectMake(50+(self.view.frame.size.width-100)/2, 64+450+30, (self.view.frame.size.width-100)/2, 30); [forwardBtn setTitle:@"forward" forState:UIControlStateNormal]; forwardBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [forwardBtn addTarget:self action:@selector(forwardBtnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:forwardBtn]; //代理 _scrollView.delegate = self; [self.view addSubview:_scrollView]; } - (void)backBtnClicked { _currentIndex--; if (_currentIndex>=0) { CGPoint point = CGPointMake(_currentIndex*300 , _scrollView.contentOffset.y); [_scrollView setContentOffset:point animated:YES]; _pageControl.currentPage = _currentIndex; } else { _currentIndex = 5; CGPoint point = CGPointMake(_currentIndex*300 , _scrollView.contentOffset.y); [_scrollView setContentOffset:point animated:YES]; _pageControl.currentPage = _currentIndex; } } - (void)forwardBtnClicked { _currentIndex++; if (_currentIndex<=5) { CGPoint point = CGPointMake(_currentIndex*300, _scrollView.contentOffset.y); [_scrollView setContentOffset:point animated:YES]; _pageControl.currentPage = _currentIndex; } else { _currentIndex = 5; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { _currentIndex = scrollView.contentOffset.x/300; _pageControl.currentPage = _currentIndex; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
转载于:https://www.cnblogs.com/0515offer/p/4638954.html
相关资源:数据结构—成绩单生成器