关于图片轮播的重要知识点。(循环滚动、定时器NSTimer)
一、先说说学习到得关于循环滚动的三种实现方式:
摘自 :http://www.cnblogs.com/pengyingh/articles/2413682.html
谢谢大神分享。
1.循环滚动
若本次滚动是向后一页滚动,则把三页都向前放置,第一页放到末尾
初始化UISCrollView
float x,y,width,height;
x = WIDTH_OFF_SET;
y = HEIGHT_OFF_SET;
width = WIDTH_OFF_SET;
height = SCROLLVIEW_HEIGHT;
for (int j=0; j<3; j++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x*(j), y, width, height)];
[view setTag:10+j];//view的tag为10,11,12
[view setBackgroundColor:[UIColor yellowColor]];
for (int i=0; i<count; i++) {
UIButton*pointNButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pointNButton setTitle:[NSString stringWithFormat:@"%d",100+i] forState:UIControlStateNormal];
[pointNButton setBackgroundColor:[UIColor blueColor]];
[pointNButton setTag:100+i];
[pointNButton setFrame:CGRectMake(6.5+(13+65)*i, 8, 65, 65)];
[pointNButton addTarget:self action:@selector(pointBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:pointNButton];
}
[scrollView addSubview:view];
[view release];
}
[scrollView setDelegate:self];
[scrollView setPagingEnabled:NO];
[scrollView setContentSize:CGSizeMake(WIDTH_OFF_SET*3, 80.0)];
[scrollView setShowsHorizontalScrollIndicator:NO];
#define WIDTH_OFF_SET630.0
#define HEIGHT_OFF_SET0
#define SCROLLVIEW_WIDTH390.0
#define SCROLLVIEW_HEIGHT80.0
#define SET_FRAME(ARTICLEX) x = ARTICLEX.frame.origin.x + increase;\
if(x < 0) x = pageWidth * 2;\
if(x > pageWidth * 2) x = 0.0f;\
[ARTICLEX setFrame:CGRectMake(x, \
ARTICLEX.frame.origin.y,\
ARTICLEX.frame.size.width,\
ARTICLEX.frame.size.height)]
#pragma mark -
#pragma mark UIScrollViewDelegate
//将三个view都向右移动,并更新三个指针的指向,article1永远指向当前显示的view,article0是左边的,article2是右边的
- (void)allArticlesMoveRight:(CGFloat)pageWidth {
UIView *view0 = [scrollView viewWithTag:10];
UIView *view1 = [scrollView viewWithTag:11];
UIView *view2 = [scrollView viewWithTag:12];
UIView *tmpView = view2;
view2 = view1;
view1 = view0;
view0 = tmpView;
float increase = pageWidth;
CGFloat x = 0.0f;
SET_FRAME(view2);
SET_FRAME(view0);
SET_FRAME(view1);
}
- (void)allArticlesMoveLeft:(CGFloat)pageWidth {
UIView *view0 = [scrollView viewWithTag:10];
UIView *view1 = [scrollView viewWithTag:11];
UIView *view2 = [scrollView viewWithTag:12];
UIView *tmpView = view0;
view0 = view1;
view1 = view2;
view2 = tmpView;
float increase = -pageWidth;
CGFloat x = 0.0f;
SET_FRAME(view1);
SET_FRAME(view2);
SET_FRAME(view0);
}
- (void)scrollViewDidScroll:(UIScrollView *)theScrollView
{
CGFloat pageWidth = WIDTH_OFF_SET;
// 0 1 2
int page = floor((theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(page == 1) {
//用户拖动了,但是滚动事件没有生效
return;
} else if (page == 0) {
[self allArticlesMoveRight:pageWidth];
} else {
[self allArticlesMoveLeft:pageWidth];
}
CGPoint p = CGPointZero;
p.x = pageWidth;
[theScrollView setContentOffset:p animated:NO];
}
/*
循环滚动
每次滚动后都将scrollview的offset设置为中间的一页
若本次滚动是向前一页滚动,则把三页都向后放置,最后一页放到开头
若本次滚动是向后一页滚动,则把三页都向前放置,第一页放到末尾
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)theScrollView
{
CGFloat pageWidth = WIDTH_OFF_SET;
// 0 1 2
int page = floor((theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(page == 1) {
//用户拖动了,但是滚动事件没有生效
return;
} else if (page == 0) {
[self allArticlesMoveRight:pageWidth];
} else {
[self allArticlesMoveLeft:pageWidth];
}
CGPoint p = CGPointZero;
p.x = pageWidth;
[theScrollView setContentOffset:p animated:NO];
}
2.种方法思想是一样的,运用scrollRectToVisible
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
float currentPage =scrollView.contentOffset.x;
NSLog(@"=======bao===scrollViewDidEndDragging==currentPage=%d",currentPage);
if (currentPage >WIDTH_OFF_SET&¤tPage<WIDTH_OFF_SET*2) {
[scrollView scrollRectToVisible:CGRectMake(0,0,SCROLLVIEW_WIDTH,SCROLLVIEW_HEIGHT) animated:NO];
} else if (currentPage<0) {
[scrollView scrollRectToVisible:CGRectMake(WIDTH_OFF_SET,0,SCROLLVIEW_WIDTH,SCROLLVIEW_HEIGHT) animated:NO];
}
}
3。改变conentOffset
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
int currentPage =(int)scrollView.contentOffset.x;
if (currentPage >WIDTH_OFF_SET) {
scrollView.contentOffset = CGPointMake(abs(currentPage-WIDTH_OFF_SET), HEIGHT_OFF_SET);
} else if (currentPage<0) {
scrollView.contentOffset = CGPointMake(currentPage+WIDTH_OFF_SET,HEIGHT_OFF_SET);
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
int x= (int)scrollView.contentOffset.x/BUTTON_OFFSET*BUTTON_OFFSET;
[scrollView setContentOffset:CGPointMake(x, HEIGHT_OFF_SET) animated:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)_scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
int x = (int)scrollView.contentOffset.y/BUTTON_OFFSET*BUTTON_OFFSET;
[scrollView setContentOffset:CGPointMake(x, HEIGHT_OFF_SET) animated:YES];
}
}
总之要想实现无缝链接就必须准备三屏你的数据,通过改变offset或是view的frame的的横纵坐标进而实现循环滚动
二、NSTimer注意的知识点:
学习自:http://blog.csdn.net/davidsph/article/details/7899483
谢谢大神分享。
以下为总结大神博客的知识点,以便日后回顾。
Creating a Timer
//创建一个定时器 ,以下是便利构造器方法,都懂的
+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:
//初始化方法
– initWithFireDate:interval:target:selector:userInfo:repeats:
Firing a Timer
– fire
//是暂停一个定时器吗,NO ,是Stop ,写的很清楚
Stopping a Timer
– invalidate
//关于定时器的以下信息
Information About a Timer
– isValid //是否在运行
– fireDate //Returns the date at which the receiver will fire.
– setFireDate: //重新设置定时器开始运行的时间
– timeInterval //定时器延时时间
– userInfo //其他信息
先说一下,初始化方法
+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
这两个是创建一个定时器,并加入到当前运行循环中,即我们可以这样去理解,这样初始化一个定时器时,在(NSTimeInterval)seconds 时间之后,自动启动定时器。
而以下两个初始化方法这不一样:
+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:
这两个同样是创建,但没有加入到,运行循环中。class method to create the timer object without scheduling it on a run loop.然后,建立之后,必须(after creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.),这就是与上面两个方法的区别。
示例:
//创建一个定时器
_timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
//必须手动加入到当前循环中去
NSRunLoop *runloop=[NSRunLoop currentRunLoop];
[runloop addTimer:_timer forMode:NSDefaultRunLoopMode];
以上两端代码效果是一样的
关于对象release和对象=nil的区别,网上也有很多讨论,我也实际验证了一下,
个人认为 [对象 release] 后 这个对象就没有指向任何内存空间了,这个时候,它是不能被使用的(可以试试 NSLog 一下 release 后的对象,有惊喜的。。。)
当 对象 = nil 时,这个对象是指向了一个内存空间的( 0x0000,即开始地址),这个时候这个对象是可以被NSLog出来的。。。。。
如果一个对象alloc 一次并release后,以后是不能再使用的,它虽然也指向原来的内存空间,但其内容是不确定的,使用的话,它的内容有时候没事,但有时候会崩溃的。
但当我们在release后一个对象后,将其=nil时,下面再用到的时候就不会出错了。
[string release];
string=nil;
至于NSTimer的暂停方法,API里面NSTimer 是木有暂停继续的方法的,只有fire和invalidate,前者是开工的意思,后者是废掉的意思
谢谢http://blog.csdn.net/chentoo/article/details/8667918的分享
其实NSTimer 有一个属性叫 fireDate ,啥意思呢?fireDate么,就是fire 的开始时间所以我们就有了思路了。
暂停: [timer setFireDate:[NSDate distantFuture]]; distantFuture,就是问你未来有多远呢?好远好远就是无法到达的时间,所以 timer就一直等待不 fire了。也就是暂停了。
继续: [timer setFireDate:[NSDate date]]; 这个当然就是把fire 的时间设置为当前时刻,所以timer就立刻开工啦!