UIScrollView控制图片滑动 NSTimer UIPageControl

  1 #import "ViewController.h"
  2
  3 @interface ViewController ()<UIScrollViewDelegate>
  4 @property(strong,nonatomic)UIScrollView * ScrollView;
  5 @property(strong,nonatomic)UIImageView * view1;
  6
  7 @property(strong,nonatomic)UIPageControl * PageControl;
  8 @property(strong,nonatomic)UILabel * titltLabel;
  9
 10 @property(strong,nonatomic)NSArray * contentArray;
 11 @property(strong,nonatomic)NSArray * titleArray;
 12
 13 @property(strong,nonatomic)NSTimer * timerView;
 14 -(NSArray *)contentArray;
 15 @end
 16
 17 @implementation ViewController
 18
 19 -(NSArray *)contentArray
 20 {
 21     NSString * path = [[NSBundle mainBundle]pathForResource:@"photo.plist" ofType:nil];
 22     _contentArray = [NSArray arrayWithContentsOfFile:path];
 23     return _contentArray;
 24 }
 25
 26 - (void)viewDidLoad {
 27     [super viewDidLoad];
 28
 29     self.titleArray = @[@"美女1",@"美女2",@"美女3",@"美女4"];
 30
 31     CGFloat width = self.view.bounds.size.width;
 32     self.ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, width, 313)];
 33     self.ScrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
 34     self.ScrollView.contentOffset = CGPointMake(width, 0);
 35     self.ScrollView.contentSize = CGSizeMake(width*6, 313);
 36     self.ScrollView.pagingEnabled = YES;
 37     [self.view addSubview:self.ScrollView];
 38     self.ScrollView.delegate = self;
 39     self.ScrollView.showsHorizontalScrollIndicator = NO;
 40     [self.view addSubview:self.ScrollView];
 41
 42
 43     for (int i; i < 6; i++)
 44     {
 45         NSDictionary * dic = self.contentArray[i];
 46         self.view1 = [[UIImageView alloc]initWithFrame:CGRectMake(width*i, 10, width, 313)];
 47         self.view1.image = [UIImage imageNamed:dic[@"icon"]];
 48         [self.ScrollView addSubview:self.view1];
 49     }
 50
 51
 52
 53     self.titltLabel = [[UILabel alloc]initWithFrame:CGRectMake(width/2, 280, 80, 60)];
 54     self.titltLabel.text = self.titleArray[0];
 55     [self.view addSubview:self.titltLabel];
 56
 57     self.PageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(width/2, 300, 50, 20)];
 58     self.PageControl.numberOfPages = 4;
 59     self.PageControl.currentPage = 0;
 60     //设置小白点颜色
 61     [self.PageControl setPageIndicatorTintColor:[UIColor groupTableViewBackgroundColor]];
 62     [self.PageControl setCurrentPageIndicatorTintColor:[UIColor greenColor]];
 63     [self.view addSubview:self.PageControl];
 64
 65
 66     self.timerView = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(Autoplayphoto) userInfo:nil repeats:YES];
 67
 68 }
 69 -(void)Autoplayphoto
 70 {
 71     CGFloat width = self.view.bounds.size.width;
 72     CGPoint offset = self.ScrollView.contentOffset;
 73
 74     if (offset.x == 5*width)
 75     {
 76         offset.x = offset.x - 5*width;
 77     }else
 78     {
 79         offset.x = offset.x + width;
 80     }
 81
 82     self.ScrollView.contentOffset = offset;
 83
 84     int page = offset.x/width -1;
 85     if (page > 3) {
 86         page = 0;
 87     }
 88     if (page < 0) {
 89         page = 3;
 90     }
 91     self.PageControl.currentPage = page;
 92
 93     self.titltLabel.text = self.titleArray[page];
 94 }
 95
 96
 97 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
 98 {
 99     CGFloat width = self.view.bounds.size.width;
100     CGFloat offset = self.ScrollView.contentOffset.x;
101     if (offset >= 5*width)
102     {
103         self.ScrollView.contentOffset = CGPointMake(width, 0);
104     }
105     if (offset <= 0)
106     {
107         self.ScrollView.contentOffset = CGPointMake(4*width, 0);
108     }
109     int page = offset/width - 1;
110     if (page > 3) {
111         page = 0;
112     }
113     if (page < 0) {
114         page = 3;
115     }
116     self.PageControl.currentPage = page;
117     //NSLog(@"page = %d",page);
118     self.titltLabel.text = self.titleArray[page];
119
120 }
121
122
123 - (void)didReceiveMemoryWarning {
124     [super didReceiveMemoryWarning];
125     // Dispose of any resources that can be recreated.
126 }
127
128 @end

此题目是将循环播放的图片放入plist文件中,然后再读取出来显示。

1、plist文件读取方法

@property(strong,nonatomic)NSArray * contentArray;//contentArray数组用于存储plist文件中的内容

-(NSArray *)contentArray; //声明contentArray的getter方法

//实现contentArray的getter方法

-(NSArray *)contentArray

{

NSString * path = [[NSBundle mainBundle]pathForResource:@"photo.plist" ofType:nil]; //寻找plist文件路径,并保存于字符串path中

_contentArray = [NSArray arrayWithContentsOfFile:path]; //将plist文件中的图片内容保存于属性contentArray中

return _contentArray;

}

2、UIScrollController常用属性和常用方法

//设置偏移量

self.ScrollView.contentOffset = CGPointMake(width, 0);

//设置内容大小

self.ScrollView.contentSize = CGSizeMake(width*6, 313);

//此属性是当图片的中轴到达或超过偏移量的一半的地方时图像会自动滑动,否则图片将返回滑动前的位置

self.ScrollView.pagingEnabled = YES;

//关闭水平滑动指示器,相对应的有设置垂直滑动指示器

self.ScrollView.showsHorizontalScrollIndicator = NO;

方法:

//只要图片滑动此方法就会被执行

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

//视图将要(开始)拖拽时该方法被执行

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

//拖拽结束时,该方法被执行

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

//自动滑动结束时,此方法被执行

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

3、UIPageControl

常用属性及方法

self.PageControl.numberOfPages = 4; //设置页的张数

self.PageControl.currentPage = 0; //设置初始页

//设置小白点颜色

[self.PageControl setPageIndicatorTintColor:[UIColor groupTableViewBackgroundColor]];

//设置当前页小点的颜色

[self.PageControl setCurrentPageIndicatorTintColor:[UIColor greenColor]];

4、NSTimer

//暂停计时器

- (void)pauseTimer:(NSTimer *)oTimer

{

[oTimer setFireDate:[NSDate distantFuture]];

}

//重启计时器

- (void)restartTimer:(NSTimer *)oTimer

{

[oTimer setFireDate:[NSDate date]];

}

//延时启动定时器

- (void)restartTimerAfterDelay:(NSTimeInterval)interval timer:(NSTimer *)oTimer

{

[oTimer setFireDate:[NSDate dateWithTimeInterval:interval sinceDate:[NSDate date]]];

}

//停止计时器

- (void)stopTimer:(NSTimer *)oTimer

{

[oTimer invalidate];

}

//此种方式创建的timer已经添加至runloop中

self.timerView = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(Autoplayphoto) userInfo:nil repeats:YES];

//此种方式没有将创建的timer添加至runloop中

timer = [NSTimer timerWithTimeInterval:timerInterval target:self selector:@selector(scrollMethod) userInfo:nil repeats:YES];

//因此需要将timer添加至runloop中

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

时间: 2024-08-06 21:04:55

UIScrollView控制图片滑动 NSTimer UIPageControl的相关文章

【javascript/css】Javascript+Css实现图片滑动浏览效果

今天用js+css来做一个能够左右滑动的图片浏览效果. 首先写一个结构,包括需要浏览的两张图,以及能够点击来滑动图片的两个按钮. 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 5 <script type="text/javascrip

UIScrollView实现图片轮播

UIScrollView实现图片轮播 - (UIScrollView *)scrollView { { if (_scrollView == nil) { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)]; _scrollView.backgroundColor = [UIColor redColor]; [self.view addSubview:_scrollView]; // 取消

客户端实现图片滑动加载,资讯详情页面

展示方案:客户端请求资讯详情数据接口获取 富文本数据 客户端使用WebView展示. 当文章详情篇幅长.包含图片多一次性加载全部图片会造成客户端短暂的卡顿 影响用户体验 所以考虑计划做图片滑动加载 默认使用统一的占位图. 本以为使用一个前端jquery插件(jquery.lazyload.js)就可以解决,应用这个插件后,出现问题所有图片还是一次性加载完成,不是滑动加载,分析发现是因为客户端在实现webview的时候要定义页面中的标题.评论.相关文章等不能够给设置webview的高度为固定手机屏

手机端图片滑动切换效果

最近公司要求开发wap版本页面,碰到了个图片滑动切换效果,折腾了半天,自己封装了一个比较通用的小控件,在此分享一下. 大概功能:可以自定义是否自动切换,支持单手滑动图片进行切换,支持左右滑动切换.循环切换等等,具体可以拿demo代码自己本地试试,注意只支持手机端哦 大概思路:通过touchstart.touchmove.touchend 三个事件加上css3的3d变化效果配合,实现滑动切换图片, 开发是基于Zepto框架,当然也支持其他任何一款手机端框架,只需将代码中的美元符号$换为指定框架操作

推荐一款手机端的图片滑动插件iSlider

首先先放出中文官方地址   http://be-fe.github.io/iSlider/index.html 这是demo 众所周知,移动端的图片滑动插件有很多,为什么我要推荐这个iSlider呢? 这个插件吸引我的有两点, 一是它不依赖与jquery,采用原生代码书写.二是它使用起来非常容易,而且除了图片,还支持普通的dom元素,滑动方式多样,效果丰富. 但是它也有些缺点,其一就是它提供的接口太少了. 在为轮播图片提供一些功能按钮时,比如说,上一张.下一张.自动播放等.使用这个插件就有些力不

UIScrollView缩放图片操作

要想ScrollView缩放,必须告诉缩放那个控件,它自身的大小是不会缩放的: 并且ScrollView只能缩放自己内部的子控件: 1:这时就要用到代理,代理告诉ScrollView缩放哪个控件.(设置代理对象) 2:要想成为ScrollView的代理就要遵守协议:UIScrollViewDelegate (遵守协议) 3:成为代理后实现协议里的方法:viewForZoomingInScrollView;这个方法是返回要缩放的子控件,就是代理告诉ScrollView哪个控件要缩放 或者说这个方法

插一张图片,用SeekBar来控制图片的透明度

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent

js鼠标控制图片的特效,滚轮控制放大、缩小、鼠标拖动、聚焦。。。

项目需要做一个js控制图片的特效,滚轮控制放大.缩小.鼠标拖动等效果,网上找方法,各种报错.不兼容...最终自己研究出一套方案如下: 代码直接从项目中拷了,就不整理格式了 <script type="text/javascript"> //图片特效 by jifei_mei //图片大小,记录放大或缩小图片前的大小 var pic_size = { width:0, height:0 }; //绑定滚轮滚动事件 if (window.addEventListener) {

一种支持任意尺寸的图片滑动(上下左右滑动)效果

<! DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>任意尺寸的图片滑动</title> <style> div { margin: 0 auto; overflow: hidden;} .main { width: 100