我使用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次迭代。这里有什么问题?
答案 0 :(得分:6)
我认为当它处于这样的循环中时,UI将不会更新。
尝试使用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];
}
注意:这是我的头脑,所以我不保证它会起作用。
答案 1 :(得分:1)
以下是使用选框效果自动滚动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]