UI2_ScrollView&UIPageControl

时间:2022-12-27 16:07:26
//
// ViewController.h
// UI2_ScrollView&UIPageControl
//
// Created by zhangxueming on 15/7/10.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIScrollViewDelegate> @end
//
// 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