我有滚动视图来显示图像。我需要计时器每5秒一个接一个地显示图像。如何使用计时器启动UIScrollView事件以在5秒后移动?
答案 0 :(得分:30)
将变量int h添加到您的接口,将yourScrollView添加为属性。然后:
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
- (void) onTimer {
// Updates the variable h, adding 100 (put your own value here!)
h += 100;
//This makes the scrollView scroll to the desired position
yourScrollView.contentOffset = CGPointMake(0, h);
}
答案 1 :(得分:10)
修改上述代码
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
-(void) onTimer {
// NSLog(@"content offset %f",scrollVw.contentOffset.y);
if (scrollVw.contentOffset.y<MAX_ALLOWED_OFFSET) {
//scroll to desire position
scrollVw.contentOffset = CGPointMake(0, scrollVw.contentOffset.y+1);
}
}
答案 2 :(得分:3)
将当前值作为内容的w(x值)或h(y值)。这有助于使用UIScollView内容中的当前参考点进行手动/半自动滚动。
CGFloat w = scroller.contentOffset.x;
或
CGFloat h = scroller.contentOffset.y;
然后当然:
h += 100; // h-=100 for decrement
yourScrollView.contentOffset = CGPointMake(0, h);
答案 3 :(得分:2)
Swift版
添加以下属性
var offSet: CGFloat = 0
@IBOutlet weak var imagePageControl: UIPageControl!
@IBOutlet weak var imageScrollView: UIScrollView!
let imageArray = [your images]
修改viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.offSet = 0
let timer = Timer.scheduledTimer(timeInterval: 8, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
imagePageControl.numberOfPages = bannerArray.count
imageScrollView.isPagingEnabled = true
imageScrollView.contentSize.height = 200
imageScrollView.contentSize.width = self.view.bounds.width * CGFloat(bannerArray.count)
imageScrollView.showsHorizontalScrollIndicator = false
imageScrollView.delegate = self
for (index, image) in imageArray.enumerated() {
let image = image
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame.size.width = self.view.bounds.size.width
imageView.frame.size.height = 200
imageView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
imageScrollView.addSubview(imageView)
}
}
为自动滚动创建此功能
func autoScroll() {
let totalPossibleOffset = CGFloat(imageArray.count - 1) * self.view.bounds.size.width
if offSet == totalPossibleOffset {
offSet = 0 // come back to the first image after the last image
}
else {
offSet += self.view.bounds.size.width
}
DispatchQueue.main.async() {
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.imageScrollView.contentOffset.x = CGFloat(self.offSet)
}, completion: nil)
}
}
添加此功能以进行手动滚动
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
imagePageControl.currentPage = Int(page)
self.offSet = page * scrollView.frame.size.width // this updates offset value so that automatic scroll begins from the image you arrived at manually
}
注意:虽然问题是特定于自动滚动,但我也添加了手动滚动。这样自动滚动和手动滚动一起工作。
另外,我添加了UIPageControl
。如果您不需要,请删除imagePageControl
。
答案 4 :(得分:0)
也许,[scroll scrollRectToVisibile:animated:]比[scroll setContentOffset]更好。 因为setContentOffset会导致滚出边缘。 试一试!