【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html
iOS开发UI篇—UIScrollView控件实现图片轮播
一、实现效果
实现图片的自动轮播

二、实现代码
storyboard中布局

代码:
#import "YYViewController.h" @interface YYViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
/**
* 页码
*/
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @property (nonatomic, strong) NSTimer *timer;
@end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 图片的宽
CGFloat imageW = self.scrollview.frame.size.width;
// CGFloat imageW = 300;
// 图片高
CGFloat imageH = self.scrollview.frame.size.height;
// 图片的Y
CGFloat imageY = ;
// 图片中数
NSInteger totalCount = ;
// 1.添加5张图片
for (int i = ; i < totalCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 图片X
CGFloat imageX = i * imageW;
// 设置frame
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
// 设置图片
NSString *name = [NSString stringWithFormat:@"img_0%d", i + ];
imageView.image = [UIImage imageNamed:name];
// 隐藏指示条
self.scrollview.showsHorizontalScrollIndicator = NO; [self.scrollview addSubview:imageView];
} // 2.设置scrollview的滚动范围
CGFloat contentW = totalCount *imageW;
//不允许在垂直方向上进行滚动
self.scrollview.contentSize = CGSizeMake(contentW, ); // 3.设置分页
self.scrollview.pagingEnabled = YES; // 4.监听scrollview的滚动
self.scrollview.delegate = self; [self addTimer];
} - (void)nextImage
{
int page = (int)self.pageControl.currentPage;
if (page == ) {
page = ;
}else
{
page++;
} // 滚动scrollview
CGFloat x = page * self.scrollview.frame.size.width;
self.scrollview.contentOffset = CGPointMake(x, );
} // scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"滚动中");
// 计算页码
// 页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
CGFloat scrollviewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollviewW / ) / scrollviewW;
self.pageControl.currentPage = page;
} // 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
// [self.timer invalidate];
[self removeTimer];
} - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 开启定时器
[self addTimer];
} /**
* 开启定时器
*/
- (void)addTimer{ self.timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
/**
* 关闭定时器
*/
- (void)removeTimer
{
[self.timer invalidate];
}
@end
补充:
1.先介绍下UIScrollView的常见属性
@property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置
@property(nonatomic) CGSize contentSize; // 内容尺寸(能滚动的范围)
@property(nonatomic) UIEdgeInsets contentInset; // 额外增加的滚动区域(在上下左右4个边缘)
@property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 代理对象
@property(nonatomic) BOOL bounces; // 是否有弹簧效果
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否显示水平滚动条
@property(nonatomic) BOOL showsVerticalScrollIndicator; // 是否显示垂直滚动条
CGFloat w = self.view.frame.size.width;
CGFloat h = self.view.frame.size.height;
for (int i = ; i< kCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init]; // 1.设置frame
imageView.frame = CGRectMake(i * w, , w, h); // 2.设置图片
NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + ];
imageView.image = [UIImage imageNamed:imgName]; [_scrollView addSubview:imageView];
2.2.设置UIScrollView的相关属性
属性在文章的开头有介绍
// height == 0 代表 禁止垂直方向滚动
_scrollView.contentSize = CGSizeMake(kCount * w, );
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
2.3.设置UIPageControl的相关属性,计算页码
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.center = CGPointMake(w * 0.5, h - );
pageControl.bounds = CGRectMake(, , , );
pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)
// 设置非选中页的圆点颜色
pageControl.pageIndicatorTintColor = [UIColor redColor];
// 设置选中页的圆点颜色
pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 禁止默认的点击功能
pageControl.enabled = NO; [self.view addSubview:pageControl];
_pageControl = pageControl;
2.4.滚动时切换页码
#pragma mark - UIScrollView的代理方法
#pragma mark 当scrollView正在滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
// NSLog(@"%d", page); // 设置页码
_pageControl.currentPage = page;
}
【转】 iOS开发UI篇—UIScrollView控件实现图片轮播的更多相关文章
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- UIScrollView控件实现图片轮播
http://www.cnblogs.com/dyf520/p/3805297.html 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: 1 ...
- iOS开发-UI (一)常用控件
从这里开始是UI篇 知识点: 1.常用IOS基本控件 2.UITouch ======================= 常用基本控件 1.UISegmentedControl:分段控制器 1)创建方 ...
- iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)
一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...
随机推荐
- ClassNotFoundException
在 java socket 通信,使用 ObjectInputStream 的 readObject 时, 出现了问题. ServerProject 问题描述是这样的: java.lang.Class ...
- poj2752 bzoj3670
2752这是一道关于next函数的题(其实好像也可以用后缀数组暴力搞搞,但大概会超时)根据next[i]=max{j} (s[0..j]=s[i-j..i] j<i)不难发现这正是某个串既是前缀 ...
- 数据结构(平衡树,树分治,暴力重构):WC 2014 紫荆花之恋
[题目描述] 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来. 仔细看看的话,这棵大树实际上是一个带权 ...
- implicitly_wait()隐式等待
# -*- coding:utf-8 -*- """ implicitly_wait():隐式等待 当使用了隐士等待执行测试的时候,如果 WebDriver没有在 DOM ...
- Asp.net 字体样式
在页面中使用到字体样式 “微软雅黑“,在静态页面中显示正常,但在asp.net页面中字体无效,此时只需把字体修改为“Microsoft YaHei";
- 数据库的存储引擎和SQL语言
数据库的存储引擎就是管理数据存储的东西,它完成下面的工作: 1)存储机制 2)索引方式 3)锁 4)等等 SQL语言:-----关系型数据库所使用的数据管理语言 1)数据定义语言(DDL):DROP. ...
- Hadoop yarn配置参数
参照site:http://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-common/yarn-default.xml 我们在配置yar ...
- Fzu Problem 2082 过路费 LCT,动态树
题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528 Submit: 1654Time Limit ...
- poj 1696 Space Ant 极角排序
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...
- UVA 10106 (13.08.02)
Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input T ...