UIScrollView 的自动滚动
Posted
技术标签:
【中文标题】UIScrollView 的自动滚动【英文标题】:autoscrolling for UIScrollView 【发布时间】:2010-05-05 09:47:23 【问题描述】:我使用 setContentOffset 方法自动滚动到特定点,无需用户交互。
[menuScrollView setContentOffset:CGPointMake(600.0,0) animated:YES]
但是当我尝试以循环方式调用相同的方法以减慢滚动速度时,滚动永远不会发生
for (int i = 1; i<=30; i++)
[menuScrollView setContentOffset:CGPointMake(600.0-i*10,0.0) animated:YES];
NSLog(@"%f",600.0-i*10);
在上面这段代码中,UIScrollview 的滚动只发生了一次(第一次迭代(并且它不会在剩余的 29 次迭代中滚动。这里有什么问题?
【问题讨论】:
【参考方案1】:我认为当它处于这样的循环中时,用户界面不会更新。
尝试使用 NSTimer 而不是像这样将其置于紧密循环中。
scrollTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollView) userInfo:nil repeats:YES];
- (void) scrollView
CGFloat currentOffset = menuScrollView.contentOffset.x;
CGFloat newOffset = currentOffset - 10;
[menuScrollView setContentOffset:CGPointMake(newOffset,0.0) animated:YES];
注意:这是我的想法,所以我不保证它会起作用。
【讨论】:
感谢它的工作。但是有没有其他方法可以使滚动类似于 html 中的选框?这个很慢,但我能感觉到移动的速度不是一致的。我通过减少时间间隔来尝试这个,但它看起来并不好,它看起来像是在两个方向上的一种晃动。如何解决这个问题?或者有没有其他方法可以解决选框问题? 我已将时间间隔一直减少到 0.01 秒;这会在 iPad 3 上产生平滑滚动。如果您在 2010 年的设备上尝试过这个,它可能没有处理能力来平滑地做到这一点。另一种选择是将内容偏移设置到动画块内的内容视图的末尾。这对我来说产生了非常流畅的滚动。但是,使用此方法无法在动画运行时获取当前滚动位置,因为当您设置滚动位置时,它实际上设置为结束位置,尽管您在动画期间在屏幕上看到了什么。【参考方案2】:这里是自动滚动UIScrollView的代码:-
![CGFloat cx1;
int h=0;
int z=0;
int x=1;
bool isdragging=false;
- (void)setupHorizontalScrollView : (UIScrollView *)scrollView
if (arrTopFive.count == 0)
return;
for(UIView *subview in \[scrollView subviews\])
\[subview removeFromSuperview\];
scrollView.delegate = self;
\[scrollView setBackgroundColor:\[UIColor clearColor\]\];
\[scrollView setCanCancelContentTouches:NO\];
// scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = NO;
scrollView.scrollEnabled = YES;
CGFloat cx = 0;
for (int i = 0;i<arrTopFive.count;i++)
clsShows *c=\[arrTopFive objectAtIndex:i\];
UIImageView *imageView = \[\[UIImageView alloc\] init\];
imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tapGesture = \[\[UITapGestureRecognizer alloc\] initWithTarget:self action:@selector(tvShowTopFiveClicked:)\];
tapGesture.numberOfTapsRequired = 1;
\[imageView addGestureRecognizer:tapGesture\];
imageView.tag=i;
\[imageView sd_setImageWithURL:\[NSURL URLWithString:c.imageurl\] placeholderImage:\[UIImage imageNamed:@"img_placeholder.jpg"\]\];
CGRect rect = imageView.frame;
rect.size.height = 250;
rect.size.width = 500;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
\[scrollView addSubview:imageView\];
cx += imageView.frame.size.width+5;
\[scrollView setContentSize:CGSizeMake(cx, \[scrollView bounds\].size.height)\];
cx1=cx-\[scrollView bounds\].size.width;
\[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES\];
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
if (scrollView.dragging)
isdragging=true;
h=ceil((float)scrollView.contentOffset.x / 5) * 5;
z=ceil((float)scrollView.contentOffset.x / 5) * 5;
if (h<0)
h=0;
z=0;
else if (h>cx1)
h=cx1;
z=cx1;
\[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startScroller) object:nil\];
\[self performSelector:@selector(startScroller) withObject:nil afterDelay:2.0\];
-(void) startScroller
if (isdragging == true)
isdragging=false;
- (void) onTimer
if (isdragging)
return;
if (h > cx1)
if (x <= 5)
\[headerView.scrollView setContentOffset:CGPointMake(z, 0) animated:YES\];
x++;
z--;
return;
if (z <= 5)
h=0;
z-=5;
\[headerView.scrollView setContentOffset:CGPointMake(z, 0) animated:YES\];
else
x=1;
if (z<=5)
\[headerView.scrollView setContentOffset:CGPointMake(h, 0) animated:YES\];
z+=1;
else
h += 5;
z=h;
\[headerView.scrollView setContentOffset:CGPointMake(h, 0) animated:YES\];
][1]
【讨论】:
以上是关于UIScrollView 的自动滚动的主要内容,如果未能解决你的问题,请参考以下文章