// ViewController.h
// UIScrollView
//
// Created by PXD on 15-5-1.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
@end
//
// ViewController.m
// UIScrollView
//
// Created by PXD on 15-5-1.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "ViewController.h"
typedef enum{
kScrollDirectionRight,
kScrollDirectionLeft
}kScrollDirection;
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrooView;
@property (nonatomic, assign) kScrollDirection direction;
@property (nonatomic, strong) UIPageControl *pageController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_direction = kScrollDirectionRight;
//frame 滚动视图的大小 看得到的界面的大小
self.scrooView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
_scrooView.backgroundColor = [UIColor lightGrayColor];
//添加内容
for (int i = 1; i <= 5; i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*320, 0, 320, 200)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i]];
[_scrooView addSubview:imageView];
}
//设置内容视图的宽高
//width > scrollView.width 横向滚动
//height > scrollView.height 纵向滚动
//都超出scrooView的范围 双向滚动
//都小于scrollView的范围 不滚动
_scrooView.contentSize = CGSizeMake(5 * 320, 200);
//设置按页显示
_scrooView.pagingEnabled = YES;
//隐藏横向的滚动条
_scrooView.showsHorizontalScrollIndicator = NO;
_scrooView.delegate = self;
[self.view addSubview:_scrooView];
UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[changeButton setFrame:CGRectMake(100, 250, 120, 50)];
[changeButton setTitle:@"滚动" forState:UIControlStateNormal];
[changeButton addTarget:self action:@selector(buttonDidClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:changeButton];
//添加页码
self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 320, 20)];
//设置总共多少页
_pageController.numberOfPages = 5;
//当前第几页
_pageController.currentPage = 0;
[self.view addSubview:_pageController];
}
- (void)buttonDidClicked{
//切换到下一张图片 contentOffset
//NSLog(@"1---%.0f", _scrooView.contentOffset.x);
CGFloat offsetX = 0;
if (_direction == kScrollDirectionRight) {
offsetX = _scrooView.contentOffset.x + 320;
if (offsetX >= _scrooView.contentSize.width) {
_direction = kScrollDirectionLeft;
}
}
if (_direction ==kScrollDirectionLeft){
offsetX = _scrooView.contentOffset.x - 320;
if (offsetX <= 0) {
_direction =kScrollDirectionRight;
}
}
//NSLog(@"2--%.0f", offsetX);
[_scrooView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
#pragma mark -- ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//获取当前的偏移值-> 获取当前的页数
int page = scrollView.contentOffset.x / 320;
if (page != _pageController.currentPage) {
//设置页码
[_pageController setCurrentPage:page];
}
}
即将滚动
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
// NSLog(@"将要拖动");
//}
//
正在滚动
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// NSLog(@"正在滚动");
//}
//
滚动结束了
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
// NSLog(@"拖动结束");
//}
//
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
// NSLog(@"手放开 惯性运动");
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// NSLog(@"惯性运动结束 停下来了");
//}
@end