UIScrollView
// 声明
@property(nonatomic,retain)UIScrollView *scrollView;
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(100, 100, 200, 400)];
self.scrollView.backgroundColor = [UIColor whiteColor];
//1. contentSize 定义内容区域大小 当contentsize 比frame.size大时,才可实现滚动
self.scrollView.contentSize = CGSizeMake(400, 800);
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.png"]];
imageView.tag = 100;
imageView.frame = CGRectMake(0, 0, 200, 350);
[self.scrollView addSubview:imageView];
//2. contentOffset 视图左上角距离坐标原点的偏移量
self.scrollView.contentOffset = CGPointMake(-20, -50);
//3. scrollsToTop 滑动到顶部
self.scrollView.scrollsToTop = YES; // default YES.
// 4. pagingEnabled 是否整屏滑动
self.scrollView.pagingEnabled = YES; // default NO.
// 5. bounces边界是否回弹
self.scrollView.bounces = YES;// default YES.
// 6. scroll 设置是否能够滚动 (不常用)
self.scrollView.scrollEnabled = YES;
// 7. 显示水平或竖直方向进度条
self.scrollView.showsHorizontalScrollIndicator = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
// 8. 设置水平或竖直方向的边框是否反弹
self.scrollView.alwaysBounceHorizontal = YES;
self.scrollView.alwaysBounceVertical = YES;
// 缩放相关
// 要实现缩放,还需要实现delegate,指定缩放的视图是谁
// 指定最大缩放 和 最小缩放
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 2.0;
// 设置缩放变化比例
self.scrollView.zoomScale = 1.5;
// 控制缩放是否反弹
self.scrollView.bouncesZoom = YES;
[self addSubview:self.scrollView];
}
#pragma mark UIScrollViewDelegate
// 1. 遵守协议
@interface RootViewController : UIViewController<UIScrollViewDelegate>
// 2.设置代理
self.rootV.scrollView.delegate = self;
// 3.实现 指定scrollView设置代理的方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
// 1.利用Tag值
// return [scrollView viewWithTag:100];
// 2. 利用子视图
return scrollView.subviews[0];
}
// ScrollView 滚动流程
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖拽");
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"正在滚动");
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView{
NSLog(@"结束拖拽");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"开始减速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"结束减速");
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
NSLog(@"我已经完成缩放了");
}
UIPageControl
@property(nonatomic,retain)UIPageControl *page;
- (void)p_setupView{
self.backgroundColor = [UIColor yellowColor];
self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame) - 50, CGRectGetWidth(self.frame), 50)];
self.page.backgroundColor = [UIColor blackColor];
[self addSubview:self.page];
// 1. 一共多少页
self.page.numberOfPages = 5;
// 2. 获取当前页
// NSInteger *i = self.page.currentPage;
// 3. 设置页面指示器 非当前页小圆点 的颜色
self.page.pageIndicatorTintColor = [UIColor redColor];
// 4. 设置页面指示器 当前页小圆点 的颜色
self.page.currentPageIndicatorTintColor = [UIColor greenColor];
// 不常用且不明所以
// 5. 跳转页面 关闭小圆点跳转
//self.page.defersCurrentPageDisplay = YES;
self.page.hidesForSinglePage = YES;
[self.page updateCurrentPageDisplay];
[self.page sizeForNumberOfPages:2];
}
// 使用 添加action
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 添加action
[self.rootV.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)pageAction:(UIPageControl *)sender{
NSLog(@"这是第 %ld 页",sender.currentPage);
}
UIScrollView和UIPageControl 结合使用
// 遵循协议
@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)RootView *rootV;
@property(nonatomic,retain)NSMutableArray *nameArr;
@end
@implementation RootViewController
-(void)loadView{
self.rootV = [[RootView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.view = self.rootV;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 1. 处理数据
[self p_date];
// 2. 显示图片
[self p_show];
// 3.设置代理
self.rootV.scrollV.delegate = self;
// 4. 设置图片随 currentpage移动而换
[self.rootV.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
// Do any additional setup after loading the view.
}
- (void)pageAction:(UIPageControl *)sender{
[UIView animateWithDuration:0.5 animations:^{
self.rootV.scrollV.contentOffset = CGPointMake(self.rootV.pageC.currentPage * self.rootV.frame.size.width, 0);
}];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
self.rootV.pageC.currentPage = scrollView.contentOffset.x / self.rootV.frame.size.width;
}
- (void)p_date{
self.nameArr = [NSMutableArray array];
for (int i = 0; i < 7; i ++) {
NSString *name = [NSString stringWithFormat:@"%d.jpeg",i];
[self.nameArr addObject:name];
}
}
- (void)p_show{
for (int i = 0; i < self.nameArr.count; i ++) {
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.nameArr[i]]];
image.frame = CGRectMake(self.rootV.frame.size.width * i, 0, self.rootV.frame.size.width, self.rootV.frame.size.height);
[self.rootV.scrollV addSubview:image];
}
self.rootV.scrollV.contentSize = CGSizeMake(self.rootV.frame.size.width * self.nameArr.count, self.rootV.frame.size.height);
self.rootV.pageC.currentPage = 0;
self.rootV.pageC.numberOfPages = self.nameArr.count;
self.rootV.scrollV.pagingEnabled = YES;
}