基于ScrollView的图片播放

  • ScrollView的方法
  • NSTime的循环
  • UIPageControl的运用
  • 委托方法

基于iphone5 未做屏幕的适配

import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scrollView; //声明一个UIScrollView
@property(nonatomic, strong)UIPageControl *pageControl; //声明一个UIPageControl
@property (nonatomic, strong) NSTimer *timer;
@end @implementation ViewController
//设置ScrollView
-(UIScrollView*)scrollView{
if(_scrollView==nil){
//初始化位置
_scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 530)];
self.scrollView.contentSize=CGSizeMake(4*_scrollView.bounds.size.width, 0);
self.scrollView.pagingEnabled=YES;
self.scrollView.bounces=NO;
self.scrollView.showsHorizontalScrollIndicator=NO;
self.scrollView.showsVerticalScrollIndicator=NO;
//委托
self.scrollView.delegate = self;
[self.view addSubview:_scrollView];
}
return _scrollView;
} //分页设置
-(UIPageControl*)pageControl{
if(_pageControl==nil){
_pageControl=[[UIPageControl alloc]init];
_pageControl.numberOfPages=4;
_pageControl.bounds=CGRectMake(0, 0, 150, 20);
//设置中心
_pageControl.center=CGPointMake(self.view.center.x, self.scrollView.frame.size.height+30);
_pageControl.pageIndicatorTintColor=[UIColor redColor];
_pageControl.currentPageIndicatorTintColor=[UIColor blackColor];
[self.view addSubview:_pageControl];
//页数改变
[_pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
}
return _pageControl;
} //页数改变事件
-(void)pageChange:(UIPageControl*)pageCtrl{
//页数改变同时变更scrollView
[self.scrollView setContentOffset:CGPointMake(pageCtrl.currentPage*self.scrollView.bounds.size.width, 0) animated:YES];
} //scrollView的contentOffset 改变
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scrollviewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollviewW / 2) / scrollviewW;
self.pageControl.currentPage=page;
} - (void)viewDidLoad {
[super viewDidLoad];
for (int i=0; i<4; i++) {
NSString *imageName= [NSString stringWithFormat:@"%02d",i+1] ;
UIImage *image=[UIImage imageNamed:imageName];
UIImageView *imageView =[[UIImageView alloc]initWithFrame:self.scrollView.bounds];
imageView.image=image;
[self.scrollView addSubview:imageView ];
}
//循环
[self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
CGRect frame = imageView.frame;
//索引变化的时候改变坐标位置
frame.origin.x=frame.size.width*idx;
imageView.frame=frame; }];
//默认从第一个
self.pageControl.currentPage=0;
[self startTimer];
} //绑定上timer
- (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)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} //更新timer
- (void)updateTimer
{
int page = (self.pageControl.currentPage + 1) % 4;
self.pageControl.currentPage = page;
[self pageChange:self.pageControl];
} @end

最新文章

  1. C#操作XML的通用方法总结
  2. node.js学习(1)
  3. Myeclipse 添加server library
  4. Java-马士兵设计模式学习笔记-观察者模式-AWT简单例子
  5. Finalization
  6. Install-Package 那点事儿
  7. UIWebView与JavaScript(JS) 回调交互 -备
  8. ACM计算几何题目推荐
  9. Solr学习笔记2(V7.2)---导入自己的数据
  10. Python Selenium + phantomJS 模拟登陆教务管理系统 “抢课”
  11. node03
  12. win10下安装vs2013无法安装解决方案
  13. DB2表被锁,如何解锁
  14. Vue2---父子组件之间的访问
  15. C++ 的那些坑 (Day 1)
  16. CSS深入理解之absolute(HTML/CSS)
  17. webstrom 里面使用github
  18. easyUI datagrid表格添加“暂无记录”显示
  19. 关于函数传参的其他问题(const形参实参/可变形参)
  20. jdbcType DATE和TIMESTAMP区别

热门文章

  1. mysql数据库备份及还原
  2. C语言mktime()
  3. Linux终端连接Linux服务器
  4. php通过ini_set调用output_compression压缩网页
  5. Linux apache的运行用户和用户组
  6. 修真院java后端工程师学习课程--任务1(day one)
  7. jsp页面中的$(param.xxx)
  8. Java 几种动态代理实现及其性能比较
  9. mybatis_SQL映射(1)
  10. volatile特性及内存语义