UIScrollView是可以滚动的
view,UIView本身不能滚动,子类
UIScrollview拓展了滚动方面的功能。UIScrollView是所有滚动视图的基类。以后的UITableView,
UITextView等视图都是继承于该类。
使用场景:显示不下(单张大图);内容太多(图文混排);滚动头条(图片);相册等
UIScrollview主要专长于两个方面:
滚动:contentSize大于frame.size的时候,能够滚动。缩放:自带缩放,可以指定缩放倍数。
RootView.h
#import <UIKit/UIKit.h>
@interface RootView : UIView
@property(nonatomic,strong)UILabel *textLabel;
//定义一个主滚动视图
@property(nonatomic,strong)UIScrollView *mainScrollView;
//小点
@property(nonatomic,strong)UIPageControl *pageControl;
@end
RootView.m
//
// RootView.m
// UI_lesson7_class
//
// Created by lanou3g on 15/10/30.
// Copyright (c) 2015年 lirui. All rights reserved.
//
#import "RootView.h"
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createMainScrollView];
// [self createTextLabel];
[self createScrollImageView];
[self createPageControl];
}
return self;
}
#pragma mark 创建 pageControl
-(void)createPageControl{
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 180, 337, 20)];
//几个小点
self.pageControl.numberOfPages = 5;
//设置小点的默认位置
self.pageControl.currentPage = 2;
//设置小点未选中的颜色
self.pageControl.pageIndicatorTintColor = [UIColor redColor];
//设置小点选中的颜色
self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
[self addSubview:self.pageControl];
}
#pragma mark 创建 label
-(void)createTextLabel{
self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 1200)];
self.textLabel.numberOfLines = 0; //自动换行
// name.txt 的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"name" ofType:@"txt"];
NSString *contentString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.textLabel.text = contentString;
//
[self.mainScrollView addSubview:self.textLabel];
}
#pragma mark 创建 ScrollImageView
-(void)createScrollImageView{
for (int i = 0; i < 5; i++) {
//找到图片
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"h%d.jpg",i + 1]];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//设置 imageView 的 frame
imageView.frame = CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, 200);
//把 imageView 添加到 mainScrollView
[self.mainScrollView addSubview:imageView];
}
}
#pragma mark 创建 MainScrollView
-(void)createMainScrollView{
self.mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
self.mainScrollView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.mainScrollView];
//1.属性
//①如何让 scrollView 滑动起来
// contentSize ---滑动范围 (CGSizeMake 设置值要比 frame 要大)
// 单方向滑动:如果只想上下滑动,把宽度滑动值设为0,或比frame值小
// self.mainScrollView.contentSize = CGSizeMake(0, 1200);
self.mainScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 5 , 0);
//2.设置偏移量 contentOffset 最大设置的偏移量是 contentSize - frame
self.mainScrollView.contentOffset = CGPointMake(0, 0);
//3.控制滚动条的显示
//控制垂直方向的滚动指示条
self.mainScrollView.showsVerticalScrollIndicator = NO;
//控制水平方向的滚动指示条(self.mainScrollView.showsHorizontalScrollIndicator)
//4.回弹效果
self.mainScrollView.bounces = YES; //默认是YES。 NO关闭回弹效果
//5.控制 scrollView 是否可以滑动
self.mainScrollView.scrollEnabled = YES; //设置NO,用户不能滑动
//6.设置 整页翻动(*)
self.mainScrollView.pagingEnabled = YES;
}
@end
RootViewController.m
//
// RootViewController.m
// UI_lesson7_class
//
// Created by lanou3g on 15/10/30.
// Copyright (c) 2015年 lirui. All rights reserved.
//
#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)RootView *rootView;
@end
@implementation RootViewController
-(void)loadView{
self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//给 scrollView 设置代理
self.rootView.mainScrollView.delegate = self;
}
#pragma mark scrollView 结束减速的时候 触发的代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollView 结束减速的时候 触发的代理方法");
}
#pragma mark scrollView 开始减速的时候 触发的代理方法
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollView 开始减速的时候 触发的代理方法");
}
#pragma mark 结束拖拽 scrollView 触发的代理方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"结束拖拽 scrollView 触发的代理方法");
}
#pragma mark 开始拖拽 scrollView 触发的代理方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//一次拖拽 打印一次
NSLog(@"开始拖拽 scrollView 触发的代理方法");
}
#pragma mark 滚动 scrollView 就会触发的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// NSLog(@"触发");
// [self.view endEditing:YES]; // 所有控件 结束编辑状态!!!(让键盘回收)
}
- (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