How to scroll UIcollectionView automatically in Horizontal position using NSTimer
I'm working on my project source below:
- (void)viewDidLoad {
[super viewDidLoad];
self.endPoint = CGPointMake(0, self.collectionView.frame.size.width);
self.scrollingPoint = CGPointMake(0, 0);
self.scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer {
self.collectionView.contentOffset = self.scrollingPoint;
if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) {
[self.scrollingTimer invalidate];
}
self.scrollingPoint = CGPointMake(self.scrollingPoint.x+1, 0);
}
Tried with the above code, but is not working for me.
Swift 4.2
With Runloop and Timer block. Adjust the 0.015 & 0.25 values to match your required speed.
var carousalTimer: Timer?
var newOffsetX: CGFloat = 0.0
func startTimer() {
carousalTimer = Timer(fire: Date(), interval: 0.015, repeats: true) { (timer) in
let initailPoint = CGPoint(x: self.newOffsetX,y :0)
if __CGPointEqualToPoint(initailPoint, self.collectionView.contentOffset) {
if self.newOffsetX < self.collectionView.contentSize.width {
self.newOffsetX += 0.25
}
if self.newOffsetX > self.collectionView.contentSize.width - self.collectionView.frame.size.width {
self.newOffsetX = 0
}
self.collectionView.contentOffset = CGPoint(x: self.newOffsetX,y :0)
} else {
self.newOffsetX = self.collectionView.contentOffset.x
}
}
RunLoop.current.add(carousalTimer!, forMode: .common)
}
// Swift-3
var timr=Timer()
var w:CGFloat=0.0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
configAutoscrollTimer()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(true)
deconfigAutoscrollTimer()
}
func configAutoscrollTimer()
{
timr=Timer.scheduledTimer(timeInterval: 0.03, target: self, selector: #selector(dashboard_ViewController.autoScrollView), userInfo: nil, repeats: true)
}
func deconfigAutoscrollTimer()
{
timr.invalidate()
}
func onTimer()
{
autoScrollView()
}
func autoScrollView()
{
let initailPoint = CGPoint(x: w,y :0)
if __CGPointEqualToPoint(initailPoint, ticker.contentOffset)
{
if w<collection_view.contentSize.width
{
w += 0.5
}
else
{
w = -self.view.frame.size.width
}
let offsetPoint = CGPoint(x: w,y :0)
collection_view.contentOffset=offsetPoint
}
else
{
w=collection_view.contentOffset.x
}
}
// this works excellently
@IBOutlet weak var collection_scroll: UICollectionView! // Outlet
var Scrollinftimer = Timer() // Set Timer
let Image_Scroll : [String] = ["ic_girl1","ic_girl2"] // Set Your Images
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return Image_Scroll.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomcollectionCellCollectionViewCell", for: indexPath) as! CustomcollectionCellCollectionViewCell
cell.img_scrollcoll.image = UIImage(named : Image_Scroll[indexPath.row])
var rowIndex = indexPath.row
let Numberofrecords : Int = Image_Scroll.count - 1
if (rowIndex < Numberofrecords)
{
rowIndex = (rowIndex + 0) // 1
}
else
{
rowIndex = 0
}
Scrollinftimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.startTimer(timersset:)), userInfo: rowIndex, repeats: true)
return cell
}
@objc func startTimer(timersset : Timer)
{
UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseOut, animations:
{
self.collection_scroll.scrollToItem(at: IndexPath(row: timersset.userInfo! as! Int,section:0), at: .centeredHorizontally, animated: false)
}, completion: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With