IOS scrollView 图片浏览
//
// ViewController.m
// 0426
//
// Created by apple on 15/4/26.
// Copyright (c) 2015年 gense. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate>
{
UIPageControl * pageControl; //定义图片名称集合
NSMutableArray * arr ; //主显示图片view
UIImageView * mainImageView ; //
UIImageView * otherImageView; //定义当前显示索引位置
int currentImageIndex ; UIImage * leftImage ; UIImage * rightImage;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //取得屏幕宽度与高度
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height; arr = [NSMutableArray arrayWithCapacity:8]; //添加滚动图片
for (int i=0 ;i < 8; i++) {
[arr addObject:[NSString stringWithFormat:@"0%d.jpg",i+1]];
} currentImageIndex = 0;
[self setImage]; //实例化不显示图片
mainImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:arr[0]]]; mainImageView.frame = CGRectMake(width, 0, width, height);
[_scrollView addSubview:mainImageView]; otherImageView = [[UIImageView alloc] init]; [_scrollView addSubview:otherImageView]; //设置scrollview 滚动区域 _scrollView.contentSize = CGSizeMake(3 * width, 0); //显示中间图片
_scrollView.contentOffset = CGPointMake(width, 0); //隐藏水平滚动格
_scrollView.showsHorizontalScrollIndicator = NO; _scrollView.pagingEnabled = YES; //注册scrollview 的代理对象
_scrollView.delegate = self; //添加分布条
pageControl = [[UIPageControl alloc]init]; //设置分页条位置
pageControl.center = CGPointMake(width/2, height-39); pageControl.bounds = CGRectMake(0, 0, width, 30); //设置分页条 分页项的color
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; pageControl.numberOfPages = 8; pageControl.currentPage = 0; //添加change事件 [pageControl addTarget:self action:@selector(pageControlPageChange:) forControlEvents:UIControlEventValueChanged]; [_scrollView addSubview:pageControl]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int currentPage = scrollView.contentOffset.x/scrollView.frame.size.width ; if(currentPage == 1)
{
return ;
} //向左滑
if(currentPage == 0)
{
if( currentImageIndex == 0)
{
currentImageIndex = 7;
}
else
{
currentImageIndex--;
} mainImageView.image = leftImage;
_scrollView.contentOffset = CGPointMake(mainImageView.frame.origin.x, 0); }
else if(2 == currentPage) //向右滑
{
if(currentImageIndex == 7)
{
currentImageIndex = 0;
}
else
{
currentImageIndex ++;
} mainImageView.image = rightImage;
_scrollView.contentOffset = CGPointMake(mainImageView.frame.origin.x, 0);
} pageControl.currentPage = currentImageIndex ; //NSLog(@"current page index %d",currentImageIndex); //重新设置左右图片 [self setImage]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"end scrolling Animation");
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//调整pageControl bounds size x位置
pageControl.center = CGPointMake( scrollView.contentOffset.x + scrollView.frame.size.width/2, scrollView.frame.size.height-39); //得到主显示图片的位置
CGFloat mainImageX = mainImageView.frame.origin.x; //得到滚动offsetX
CGFloat offsetX = _scrollView.contentOffset.x; //向右滚动
if(offsetX < mainImageX)
{
otherImageView.image = leftImage;
otherImageView.frame = CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
}
else //向左滚动
{
otherImageView.image = rightImage;
otherImageView.frame = CGRectMake(mainImageX*2, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
} } - (void) pageControlPageChange:(UIPageControl *) page
{
[UIView animateWithDuration:0.5 animations:^{
_scrollView.contentOffset = CGPointMake(page.currentPage*_scrollView.frame.size.width, 0);
}]; } #pragma mark 取得下一张图片
- (void) setImage
{ if(currentImageIndex == 0)
{
leftImage = [UIImage imageNamed:arr[7]];
rightImage = [UIImage imageNamed:arr[1]];
}
else if(currentImageIndex == 7)
{
leftImage = [UIImage imageNamed:arr[6]];
rightImage = [UIImage imageNamed:arr[0]]; }
else
{
leftImage = [UIImage imageNamed:arr[currentImageIndex-1]];
rightImage = [UIImage imageNamed:arr[currentImageIndex+1]];
}
} @end
IOS scrollView 图片浏览的更多相关文章
- IOS第六天(3:scrollView 图片轮播器)
IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...
- iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK
iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言: 今天给大家 ...
- iOS开发-图片查看(ScrollView+UIPageControl)
上周没事写了一个简单的图片查看,上次的查看只用到了一个UIImageView,不断的替换背景图片,实现图片之间的切换.通过ScrollView可以很简单的是实现图片之间的查看,设置setPagingE ...
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
由于项目需要,需要用NGUi实现一个图片浏览切换的功能,于是参考官方NGUI例子的ScrollView做了一个例子,初始看上去基本实现了自己想要的功能. 但是测试后发现当隐藏其中一张图片后,后面图片不 ...
- [置顶] ios 一个不错的图片浏览分享框架demo
demo功能:一个不错的图片浏览分享框架demo.iphone6.1 测试通过.可以浏览图片,保存,微博分享到新浪,腾讯,网易,人人等. 注:(由于各个微博的接口有时候会有调整,不一定能分享成功.只看 ...
- 一款基于 Android 开发的离线版的 MM 图片浏览 App
一款离线版的 MM 图片浏览 App,有点类似掌上百度的图片专栏应用.图片采用瀑布流展示方式,点击图片集,支持左右手势滑动切换图片:支持放大缩小功能. 实现功能:1)图片完全离线,不耗个人 GPRS ...
- 23.Quick QML-简单且好看的图片浏览器-支持多个图片浏览、缩放、旋转、滑轮切换图片
之前我们已经学习了Image.Layout布局.MouseArea.Button.GroupBox.FileDialog等控件. 所以本章综合之前的每章的知识点,来做一个图片浏览器,使用的Qt版本为Q ...
- iOS多图片下载
iOS多图片下载.在cell里面下载图片.做了缓存优化. (app.icon是图片地址) // 先从内存缓存中取出图片 UIImage *image = self.images[app.icon]; ...
- 图片浏览(CATransition)转场动画
Main.storyboard ViewController.m // // ViewController.m // 8A04.图片浏览(转场动画) // // Created by huan ...
随机推荐
- python + pytest + allure生成测试报告
pytest结合allure生成测试报告 环境搭建 要安装java环境,版本要是jdk1.8的,配置好java环境变量,不然输入allure命令会报错,JAVA_HOME环境,自行配置 安装allur ...
- iOS微信支付无法直接返回APP的问题
最近新测个项目,发现在IOS手机的APP上使用微信支付无法直接返回APP. 咨询微信客服,了解到无法直接返回APP的原因是收款配置的APPID为合作商家的APPID,而不是公司APP的APPID. 当 ...
- Redis_简介(1)
Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...
- springboot 开启事务回滚
在数据库操作时如果发生异常,回滚的方法 在方法上添加注解@Transactional,作用域是方法级的 参考资料: https://www.cnblogs.com/c2g5201314/p/13163 ...
- linux 查看历史命令 history命令
1.history命令 "history"命令就是历史记录.它显示了在终端中所执行过的所有命令的历史. history //显示终端执行过的命令 history 10 //显示最近 ...
- ch01系统基础信息模块详解
第1章 系统基础信息模块详解 1.1 系统性能信息模块 psutil 解决VMWare在Windows10的安装问题: 安装VC Redistributable 2017 解决虚拟机的上网问题:修改V ...
- 编写Hive的UDF(查询平台数据同时向mysql添加数据)
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6812629187518530052/ 可能会有一些截图中会有错误提示,是因为本地的包一直包下载有问题,截完图已经下 ...
- STC8H开发(三): 基于FwLib_STC8的模数转换ADC介绍和演示用例说明
目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...
- 华为HMS Core全新推出会员转化&留存预测模型
现在,付费学知识,付费听歌,付费看电视剧,付费享受线上购物优惠--等等场景已经成为大部分年轻人的日常. 而对于企业商家来说,付费会员作为企业差异化用户运营的手段,不仅有利于提升用户的品牌忠诚度,在当下 ...
- IntelliJ IDEA最新破解方法
IntelliJ IDEA最新破解方法 首先说下,本人使用idea版本是2021.2.3. 一.下载IDEA(推荐从官网下载) 官网地址:https://www.jetbrains.com/idea/ ...