iOS之UIScrollView循环滚动
#import "ViewController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define IMAGEVIEW_COUNT 3
@interface ViewController () <UIScrollViewDelegate> {
UIScrollView *_scrollView;
UIImageView *_leftImageView;
UIImageView *_centerImageView;
UIImageView *_rightImageView;
UIPageControl *_pageControl;
UILabel *_label;
NSMutableDictionary *_imageData;//图片数据
int _currentImageIndex;//当前图片索引
int _imageCount;//图片总数
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载数据
[self loadImageData];
//添加滚动控件
[self addScrollView];
//添加图片控件
[self addImageViews];
//添加分页控件
[self addPageControl];
//添加图片信息描述控件
[self addLabel];
//加载默认图片
[self setDefaultImage];
}
#pragma mark 加载图片数据
-(void)loadImageData {
//读取程序包路径中的资源文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"imageInfo" ofType:@"plist"];
_imageData = [NSMutableDictionary dictionaryWithContentsOfFile:path];
_imageCount = (int)_imageData.count;
}
#pragma mark 添加控件
-(void)addScrollView {
// _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20+20, SCREEN_WIDTH, 200)];
_scrollView.backgroundColor = [UIColor lightGrayColor];
//设置代理
_scrollView.delegate = self;
//设置滚动范围
_scrollView.contentSize = CGSizeMake(IMAGEVIEW_COUNT*SCREEN_WIDTH, 0);
//设置当前显示的位置为中间图片(设置scrollView偏移量)
[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
//设置分页
_scrollView.pagingEnabled = YES;
//隐藏水平条 竖向条
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
//关闭弹簧效果
// _scrollView.bounces = NO;
[self.view addSubview:_scrollView];
}
#pragma mark 添加图片三个控件
-(void)addImageViews {
_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];
//会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白
_leftImageView.contentMode=UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_leftImageView];
_centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];
_centerImageView.contentMode = UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_centerImageView];
_rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];
_rightImageView.contentMode = UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_rightImageView];
}
#pragma mark 设置默认显示图片
-(void)setDefaultImage {
//加载默认图片
_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_imageCount-1]];
_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",0]];
_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",1]];
_currentImageIndex = 0;
//设置当前页
_pageControl.currentPage = _currentImageIndex;
NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];
_label.text = _imageData[imageName];
}
#pragma mark 添加分页控件
-(void)addPageControl {
_pageControl = [[UIPageControl alloc] init];
// _pageControl.backgroundColor = [UIColor orangeColor];
//注意此方法可以根据页数返回UIPageControl合适的大小
CGSize size = [_pageControl sizeForNumberOfPages:_imageCount];
_pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
_pageControl.center = CGPointMake(SCREEN_WIDTH*2/3, _scrollView.frame.origin.y+_scrollView.frame.size.height-10);
//设置颜色
_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];
//设置当前页颜色
_pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
//设置总页数
_pageControl.numberOfPages = _imageCount;
[self.view addSubview:_pageControl];
}
#pragma mark 添加信息描述控件
-(void)addLabel {
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 20)];
// _label.backgroundColor = [UIColor orangeColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
[self.view addSubview:_label];
}
#pragma mark 滚动停止事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//重新加载图片
[self reloadImage];
//移动到中间
[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
//设置分页
_pageControl.currentPage = _currentImageIndex;
//设置描述
NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];
_label.text = _imageData[imageName];
}
#pragma mark 重新加载图片
-(void)reloadImage {
int leftImageIndex,rightImageIndex;
CGPoint offset = [_scrollView contentOffset];
if (offset.x > SCREEN_WIDTH) { //向右滑动
_currentImageIndex = (_currentImageIndex+1)%_imageCount;
}else if(offset.x < SCREEN_WIDTH) { //向左滑动
_currentImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;
}
_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]];
//重新设置左右图片
leftImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;
rightImageIndex = (_currentImageIndex+1)%_imageCount;
_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",leftImageIndex]];
_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",rightImageIndex]];
}
@end
iOS之UIScrollView循环滚动的更多相关文章
- IOS实现自动循环滚动广告--ScrollView的优化和封装
一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- UIScrollView 循环滚动,代码超简单
如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...
- iOS判断UIScrollView的滚动方向
- (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = scrollView.contentOffset.y; ...
- IOS无限自动循环滚动banner(源码)
本文转载至 http://blog.csdn.net/iunion/article/details/19080259 目前有很多APP都开始使用一些滚动banner,我自己也做了一个,部分算法没有深 ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
- ios UIPickerView 技巧集锦(包括循环滚动)
摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...
- Cocos2dx中利用双向链表实现无限循环滚动层
[Qboy原创] 在Cocos2dX 3.0 中已经实现一些牛逼的滚动层,但是对于有一些需要实现循环滚动的要求确没有实现,笔者在前段时间的一个做了一个游戏,需求是实现在少有的(13个)英雄中进行循环滚 ...
随机推荐
- [汇编与C语言关系]3. 变量的存储布局
以下面C程序为例: #include <stdio.h> ; ; ; int c; int main(void) { ; char b[] = "Hello World" ...
- 一起学微软Power BI系列-官方文档-入门指南(7)发布与共享-终结篇+完整PDF文档
接触Power BI的时间也只有几个月,虽然花的时间不多,但通过各种渠道了解收集,谈不上精通,但对一些重要概念和细节还是有所了解.在整理官方文档的过程中,也熟悉和了解了很多概念.所以从前到后把微软官方 ...
- NFS Volume Provider(Part III) - 每天5分钟玩转 OpenStack(64)
今天我们将前一小节创建的 NFS volume “nfs-vol-1” attach 到 instance “c2”上. 这里我们重点关注 nova-compute 如何将“nfs-vol-1” at ...
- EntityFramework之一对一关系(二)
前言 关于表关系园中文章也是数不胜收,但是个人觉得最难攻克的是一对一,对其配置并非无道理可循,只要掌握了原理方可,且听我娓娓道来! 共享主键关系 概念:就是两个表共享相同的主键值,也就是说一表的主键值 ...
- 从零开始编写自己的C#框架(10)——项目实施计划与甘特图
不知不觉本系列已经写了一个月,编码前的各项工作到此也终于结束了.回头看看这一个月走过来,白天上班晚上码字查资料,写写改改,挺不容易的.很多时候有些知识会用,知道是怎么回事,但并不等于能写出来.错别字. ...
- iOS开发之SQLite-C语言接口规范(二) —— Prepared Your SQL Statements
在<SQLite的C语言接口规范(一)>中介绍了如何去连接打开数据库,本篇博客就介绍如何操作数据库,本篇主要给出了如何执行数据库查询语句(Select), 然后遍历结果集.本篇博客就直接使 ...
- 用十条命令在一分钟内检查Linux服务器性能
转自:http://www.infoq.com/cn/news/2015/12/linux-performance 如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出L ...
- 如何用Perl截取报文
在实际生产环境中,常常需要从后台日志中截取报文,报文的形式类似于 <InterBOSS> ... ... ... </InterBOSS> 一个后台日志有多个报文,每个报文可由 ...
- EasyUI+MVC+EF简单用户管理Demo(问题及解决)
写在前面 iframe-src EntityFramework版本 connectionStrings View.Action.页面跳转 EasyUI中DataGrid绑定 新增.修改和删除数据 效果 ...
- T-Sql(六)触发器(trigger)
不知不觉讲到触发器了,一般我们做程序的很少接触到触发器,触发器的操作一般是DB人员来完成. 然而有的时候一些简单的业务需要我们自己去完成,不能每次都去麻烦DB人员,所以说,编程人员要全才,除了编程以为 ...