autoscrolling for UIScrollView
I used the setContentOffset method to scroll to a particular point automatically without user interaction.
[menuScrollView setContentOffset:CGPointMake(600.0,0) animated:YES]
but when i try to call the same 开发者_JAVA技巧method in a looping fashion inorder to slow down the speed of scrolling the scrolling never happens
for (int i = 1; i<=30; i++) {
[menuScrollView setContentOffset:CGPointMake(600.0-i*10,0.0) animated:YES];
NSLog(@"%f",600.0-i*10);
}
During the above piece of code the scrolling of UIScrollview happens only once (1st iteration( and it does not scroll for remaining 29 interations. What is the problem here ?
I think that when it's in a loop like this, the UI won't be updated.
Try using an NSTimer instead of putting it in a tight loop like this.
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];
}
Note: this is from the top of my head, so I don't guarantee that it will work.
Here is the code that automatically scroll UIScrollView with marquee effect:-
![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]
精彩评论