Iphone UIScrollView的自动滚动
我使用setContentOffset方法自动滚动到特定点,而无需用户交互Iphone UIScrollView的自动滚动,iphone,iphone-sdk-3.0,uiscrollview,Iphone,Iphone Sdk 3.0,Uiscrollview,我使用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) anima
[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);
}
for(inti=1;i我认为当它处于这样的循环中时,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];
}
注意:这是从我的头顶开始的,所以我不能保证它会工作。以下是自动滚动带字幕效果的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]
![CGFloat cx1;
int h=0;
int z=0;
int x=1;
bool isdragging=false;
-(无效)设置水平滚动视图:(UIScrollView*)滚动视图
{
如果(arrTopFive.count==0){
返回;
}
对于(UIView*子视图在\[scrollView子视图\]中){
\[子视图从超级视图中移除\];
}
scrollView.delegate=self;
\[scrollView setBackgroundColor:\[UIColor clearColor\]\];
\[滚动视图设置CancelContentTouches:否\];
//scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds=否;
scrollView.scrollEnabled=是;
cgcx=0;
对于(int i=0;i cx1){
如果(x)谢谢,它起作用了。但是有没有其他方法可以使卷轴像HTML中的字幕一样移动呢?这个方法很慢,但我能感觉到移动的速度不一致。我通过缩短时间间隔尝试了这个方法,但看起来不太好,它看起来像是在两个方向上都在晃动。如何解决这个问题?或者有什么方法吗还有什么方法可以解决选框问题?我已经将时间间隔一直减小到了.01秒;这会在iPad 3上产生平滑的滚动。如果在2010设备上尝试此方法,它可能没有处理能力来平滑地进行此操作。另一个选项是将内容偏移设置到内容视图的末尾,在动画b中锁定。这对我来说产生了非常平滑的滚动。但是,使用这种方法不可能在动画运行时获得当前滚动位置,因为当您设置滚动位置时,滚动位置实际上被设置为结束位置,不管您在动画期间在屏幕上看到什么。