概述

移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限.当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容,普通的UIView不具备滚动功能,不能显示过多的内容。UIScrollView是一个能够滚动的视图控件,可以用来展示大量的内容,并且可以通过滚动查看所有的内容
  • UIScrollView的常见属性
  • UIScrollView的常用代理方法
  • UIScrollView的缩放

UIScrollView使用

基本使用

UIScrollView的用法很简单,将需要展示的内容添加到UIScrollView中

设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉它滚动的范围

界面设计

点击滚动,图片往下移。实现代码:

#import "MJViewController.h"

@interface MJViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *minionView;
- (IBAction)scroll;
@end @implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad]; // 设置scrollView内容的尺寸(滚动的范围)
// self.scrollView.contentSize = CGSizeMake(892, 480);
// self.scrollView.contentSize = self.minionView.image.size;
self.scrollView.contentSize = self.minionView.frame.size; // 总体内容的范围(滚动范围)
} - (IBAction)scroll {
CGPoint offset = self.scrollView.contentOffset;
offset.x += ;
offset.y += ;
[self.scrollView setContentOffset:offset animated:YES];
}
@end

无法滚动的解决办法

如果UIScrollView无法滚动,可能是以下原因:
  • 没有设置contentSize
  • scrollEnabled = NO
  • 没有接收到触摸事件:userInteractionEnabled = NO
  • 没有取消autolayout功能(要想scrollView滚动,必须取消autolayout)

常见属性

@property(nonatomic) CGPoint contentOffset;
这个属性用来表示UIScrollView滚动的位置
 
@property(nonatomic) CGSize contentSize;
这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远)
 
@property(nonatomic) UIEdgeInsets contentInset;
这个属性能够在UIScrollView的4周增加额外的滚动区域
 
@property(nonatomic) BOOL bounces;
设置UIScrollView是否需要弹簧效果
 
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
设置UIScrollView是否能滚动
 
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
是否显示水平滚动条
 
@property(nonatomic) BOOL showsVerticalScrollIndicator;
是否显示垂直滚动条

代理delegate

很多时候,我们想在UIScrollView正在滚动 或 滚动到某个位置 或者 停止滚动 时做一些特定的操作
要想完成上述功能,前提条件就是能够监听到UIScrollView的整个滚动过程
当UIScrollView发生一系列的滚动操作时, 会自动通知它的代理(delegate)对象,给它的代理发送相应的消息,让代理得知它的滚动情况
也就是说,要想监听UIScrollView的滚动过程,就必须先给UIScrollView设置一个代理对象,然后通过代理得知UIScrollView的滚动过程

实现代理分三步:

第一步:就设置UIScrollView所在的控制器 为 UIScrollView的delegate

通过代码(self就是控制器)
self.scrollView.delegate = self;
或者 通过storyboard拖线(右击UIScrollView)
 
 
第二步:控制器应该遵守UIScrollViewDelegate协议

第三步:实现协议中定义的相关方法

常用的代理方法有:

// 用户开始拖拽时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// 滚动到某个位置时调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// 用户结束拖拽时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

内容缩放

缩放实现步骤

  1. 设置UIScrollView的id<UISCrollViewDelegate> delegate代理对象
  2. 设置minimumZoomScale :缩小的最小比例
  3. 设置maximumZoomScale :放大的最大比例
  4. 让代理对象实现下面的方法,返回需要缩放的视图控件
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
 
跟缩放相关的其他代理方法
缩放完毕的时候调用
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
正在缩放的时候调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView

实例代码

#import "MJViewController.h"

@interface MJViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *minionView;
@end
// 代理 \ 委托 @implementation MJViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 设置内容尺寸
self.scrollView.contentSize = self.minionView.frame.size; // 设置
self.scrollView.delegate = self; // 设置最大和最小的缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.2;
} /**
* 当用户开始拖拽scrollView时就会调用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"开始拖拽-----");
} /**
* 只要scrollView正在滚动,就会调用
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"----正在滚动--%@", NSStringFromCGPoint(scrollView.contentOffset));
} /**
* 当用户使用捏合手势的时候会调用
*
* @return 返回的控件就是需要进行缩放的控件
*/
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"----开始缩放");
return self.minionView;
} /**
* 正在缩放的时候会调用
*/
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"----正在缩放");
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

效果:

实践-图片轮播

UIPageControl分页

只要将UIScrollView的pageEnabled属性设置为YES,UIScrollView会被分割成多个独立页面,里面的内容就能进行分页展示
 
一般会配合UIPageControl增强分页效果,UIPageControl常用属性如下:
一共有多少页
property(nonatomic) NSInteger numberOfPages;
 
当前显示的页码
@property(nonatomic) NSInteger currentPage;
只有一页时,是否需要隐藏页码指示器
 
@property(nonatomic) BOOL hidesForSinglePage;
其他页码指示器的颜色
 
@property(nonatomic,retain) UIColor *pageIndicatorTintColor;
 
当前页码指示器的颜色
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor;
 

NSTimer定时器

NSTimer叫做“定时器”,它的作用如下
  • 在指定的时间执行指定的任务
  • 每隔一段时间执行指定的任务
 
调用下面的方法就会开启一个定时任务
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget

  selector:(SEL)aSelector

  userInfo:(id)userInfo

  repeats:(BOOL)yesOrNo;

每隔ti秒,调用一次aTarget的aSelector方法,yesOrNo决定了是否重复执行这个任务

通过invalidate方法可以停止定时器的工作,一旦定时器被停止了,就不能再次执行任务。只能再创建一个新的定时器才能执行新的任务
- (void)invalidate; //时钟停止

实例代码:

//
// UYViewController.m
// 图片轮播器
//
// Created by jiangys on 15/5/23.
// Copyright (c) 2015年 uxiaoyuan. All rights reserved.
// #import "UYViewController.h"
#define kImageCount 5 @interface UYViewController ()<UIScrollViewDelegate> @property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl; @property (nonatomic, strong) NSTimer *timer; @end @implementation UYViewController -(UIScrollView *)scrollView
{
if (_scrollView==nil) {
//如果为空,创建一个
_scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
_scrollView.backgroundColor=[UIColor redColor]; //取消弹簧效果
_scrollView.bounces=NO; // 取消水平滚动条
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO; // 要分页
_scrollView.pagingEnabled = YES; // contentSize
_scrollView.contentSize = CGSizeMake(kImageCount * _scrollView.bounds.size.width, ); // 设置代理
_scrollView.delegate = self; [self.view addSubview:_scrollView]; }
return _scrollView; } - (UIPageControl *)pageControl
{
if (_pageControl == nil) {
// 分页控件,本质上和scrollView没有任何关系,是两个独立的控件
_pageControl = [[UIPageControl alloc] init];
// 总页数
_pageControl.numberOfPages = kImageCount;
// 控件尺寸
CGSize size = [_pageControl sizeForNumberOfPages:kImageCount]; _pageControl.bounds = CGRectMake(, , size.width, size.height);
_pageControl.center = CGPointMake(self.view.center.x, ); // 设置颜色
_pageControl.pageIndicatorTintColor = [UIColor redColor];
_pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; [self.view addSubview:_pageControl]; // 添加监听方法
/** 在OC中,绝大多数"控件",都可以监听UIControlEventValueChanged事件,button除外" */
[_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
}
return _pageControl;
} // 分页控件的监听方法
- (void)pageChanged:(UIPageControl *)page
{
// 根据页数,调整滚动视图中的图片位置 contentOffset
CGFloat x = page.currentPage * self.scrollView.bounds.size.width;
[self.scrollView setContentOffset:CGPointMake(x, ) animated:YES];
} - (void)viewDidLoad
{
[super viewDidLoad];
//设置图片
for (int i=; i<kImageCount; i++) {
NSString *imageName=[NSString stringWithFormat:@"img_%02d",i+];
UIImage *image=[UIImage imageNamed:imageName]; UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.scrollView.bounds];
imageView.image=image; [self.scrollView addSubview:imageView];
}
//计算imageView
[self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
// 调整x => origin => frame
CGRect frame = imageView.frame;
frame.origin.x = idx * frame.size.width; imageView.frame = frame;
}]; // 分页初始页数为0
self.pageControl.currentPage = ; // 启动时钟
[self startTimer]; } - (void)startTimer
{
self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
// 添加到运行循环
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
} - (void)updateTimer
{
// 页号发生变化
// (当前的页数 + 1) % 总页数
int page = (self.pageControl.currentPage + ) % kImageCount;
self.pageControl.currentPage = page; // 调用监听方法,让滚动视图滚动
[self pageChanged:self.pageControl];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.s } #pragma mark - ScrollView的代理方法
// 滚动视图停下来,修改页面控件的小点(页数)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 计算页数
int page = scrollView.contentOffset.x / scrollView.bounds.size.width; self.pageControl.currentPage = page;
} /**
修改时钟所在的运行循环的模式后,抓不住图片 解决方法:抓住图片时,停止时钟,松手后,开启时钟
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 停止时钟,停止之后就不能再使用,如果要启用时钟,需要重新实例化
[self.timer invalidate];
} /**
* 停止滚动,开启时钟
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self startTimer];
} @end

效果:

源码下载:点击下载

iOS UI基础-7.0 UIScrollView的更多相关文章

  1. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  2. iOS UI基础-17.0 UILable之NSMutableAttributedString

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

  3. iOS UI基础-10.0 QQ聊天布局之键盘及文本使用

    要实现的效果:   这里只说用到的几个知识点 1.图片包含文字 在设置文字的Frame的时候,使用背景(按钮)的尺寸,文字使用了内边距 背景图片,使用拉伸 /** * 返回一张可以随意拉伸不变形的图片 ...

  4. iOS UI基础-4.0应用程序管理

    功能与界面 功能分析: 以九宫格的形式展示应用信息 点击下载按钮后,做出相应的操作 步骤分析: 加载应用信息 根据应用的个数创建对应的view 监听下载按钮点击 整个应用界面: 程序实现 思路 UI布 ...

  5. iOS UI基础-19.0 UICollectionView

    直接上代码,说明请看注释吧 1.继承三个代理 UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateF ...

  6. iOS UI基础-16.0 UIButton

    回归自然,UIButton是我们使用最频烦的一个控件.下面,对该控件的一些常用方法进行一些总结. UIButton *payStateBtn = [UIButton buttonWithType:UI ...

  7. iOS UI基础-15.0 UIWebView

    WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...

  8. iOS UI基础-13.0 数据存储

    应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...

  9. iOS UI基础-12.0 Storyboard

    storyboard创建控制器 1.先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UIStoryboard stor ...

随机推荐

  1. d7

    小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 欧洲 16位 ...

  2. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

  3. CCPC-Wannafly Winter Camp Day3 Div1 - 排列

    题目链接:https://zhixincode.com/contest/14/problem/A?problem_id=203 time limit per test: 1 secondmemory ...

  4. LeetCode 155 - 最小栈 - [数组模拟栈]

    题目链接:https://leetcode-cn.com/problems/min-stack/description/ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的 ...

  5. n个元素的数组向左循环移动i个位置

    算法的完美 时间:2012-03-19 / 分类:学习园地,网络文摘 / 浏览:1797 / 0个评论 发表评论 考虑一个问题:将一个具有n个元素的数组向左循环移动i个位置.有许多应用程序会调用这个问 ...

  6. 分区实践 注意分区名 p2018-01 p2018-02 被解释为同一分区名

    # https://dev.mysql.com/doc/refman/5.6/en/partitioning-columns-range.html'''CREATE TABLE employees ( ...

  7. [dpdk] dpdk启动几个线程

    看别人的代码搞得有点晕,突然有点不确定,再确认一次. 使用 helloworld程序测试一下. /root/dpdk-16.07/examples/helloworld 一:  只启动一个核心. [r ...

  8. [dpdk] SDK编译-简单扼要版

    0. 前提: 环境是CentOS7,archlinux编译有问题,不知道却什么. 1. 解压: [root@dpdk dpdk]# tar Jxf dpdk-2.2.0.tar.xz 2. 设置环境变 ...

  9. Flink – SlotSharingGroup

      SlotSharingGroup 表示不同的task可以共享slot,但是这是soft的约束,即也可以不在一个slot 默认情况下,整个StreamGraph都会用一个默认的"defau ...

  10. linux学习:【第2篇】常用命令

    狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第2篇]常用命令 基本命令 //打开终端: CentOS:在任何地方,右键-- ...