一个UIScrollview,自动滚动并在每个页面停止至少2秒
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个UIScrollview,自动滚动并在每个页面停止至少2秒相关的知识,希望对你有一定的参考价值。
我正在使用NSTimer来浏览我的UIScrollview,但是我无法让它停在每个页面上,就像让我们说一两秒钟所以用户可以预览并点击它们是否需要或何时滚动UIScrollview中的图像我的代码如下....:
-(void) onTimer {
CGPoint rightOffset = CGPointMake(responseScroll.contentSize.width - responseScroll.bounds.size.width, 0);
[responseScroll setContentOffset:rightOffset animated:YES];
}
-(void)viewDidLoad {
/-------/
[NSTimer scheduledTimerWithTimeInterval:0.008 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
答案
使计时器间隔为2.0:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
这意味着计时器将每2秒触发一次。
如果要将一页翻转,则需要调整onTimer方法:
CGPoint rightOffset = CGPointMake(responseScroll.contentOffset.x + responseScroll.frame.size.width, 0);
[responseScroll setContentOffset:rightOffset animated:YES];
另一答案
请考虑使用UIAnimations
-(void)viewDidLoad{
[self moveScrollView];
}
-(void)moveScrollView{
CGPoint rightOffset = CGPointMake(responseScroll.contentOffset.x+responseScroll.bounds.size.width, 0);
if(rightOffset.x+responseScroll.bounds.size.width<=responseScroll.contentSize.width){
[UIView animateWithDuration:1 animations:^{
[responseScroll setContentOffset:rightOffset animated:NO];
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self moveScrollView];
});
}];
}
}
另一答案
这是我的答案,这很快。这将无限滚动滚动视图中的页面。
private func startBannerSlideShow()
{
UIView.animate(withDuration: 6, delay: 0.1, options: .allowUserInteraction, animations: {
scrollviewOutlt.contentOffset.x = (scrollviewOutlt.contentOffset.x == scrollviewOutlt.bounds.width*2) ? 0 : scrollviewOutlt.contentOffset.x+scrollviewOutlt.bounds.width
}, completion: { (status) in
self.startBannerSlideShow()
})
}
以上是关于一个UIScrollview,自动滚动并在每个页面停止至少2秒的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 不能使用情节提要垂直滚动(使用自动布局)?