I want my banners in my application to auto scroll using timer. I can perform scrolling if the collection view is alone but i don't know how to perform it when my collection view is inside a tableview.
var r = 0
let t = Timer.init(timeInterval: 2, repeats: true) { (timer) in
let index = IndexPath(item: r, section: 0)
cell.cltView.scrollToItem(at: index, at: .left, animated: true)
if r == 6 {
r = 0
}else {
r = r+1
}
}
i am using this code inside cellForRowAt indexPath but it does not seem to work.
The timer has only initialised now, not used yet. To begin the process you need to add :-
t.fire()
I have achieved my goal by creating a function and then calling it on cellForItemAt and it solved my problem.
func scrollCollection(cltView: UICollectionView) {
var r = 0
let t = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { (timer) in
print(r)
let index = IndexPath(item: r, section: 0)
cltView.scrollToItem(at: index, at: .left, animated: true)
if r == 6 {
r = 0
}else {
r = r+1
}
}
t.fire()
}
this is the function i used and i just called it
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let mainImg = cell.viewWithTag(20) as! UIImageView
mainImg.image = UIImage(named: banners[indexPath.item])
if op {
scrollCollection(cltView: collectionView)
op = false
}
return cell
i put a check marker so that i don't call it again and again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.