0

Ich benutze Horizontal Auto Scroll mit Timer. Ich möchte, dass es eins nach dem anderen blättert.UICollectionView - Horizontaler AutoScroll mit Timer

In meinem Fall ist es Scrollen kontinuierlich von links nach rechts. und zuletzt scrollt es auch Leerraum nach der letzten Zelle.

@interface AccountsVC()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> 
{ 
CGFloat width_Cell; 
NSTimer *autoScrollTimer; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
width_Cell = 0.0; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self configAutoscrollTimer]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
[self deconfigAutoscrollTimer]; 
} 

- (void)configAutoscrollTimer 
{ 
autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 
} 

- (void)deconfigAutoscrollTimer 
{ 
[autoScrollTimer invalidate]; 
autoScrollTimer = nil; 
} 

- (void)onTimer 
{ 
[self autoScrollView]; 
} 

- (void)autoScrollView 
{ 
CGPoint initailPoint = CGPointMake(width_Cell, 0); 
if (CGPointEqualToPoint(initailPoint, self.collectionView.contentOffset)) 
{ 
    if (width_Cell < self.collectionView.contentSize.width) 
    { 
     width_Cell += 0.5; 
    }else 
    { 
     width_Cell = -self.view.frame.size.width; 
    } 
    CGPoint offsetPoint = CGPointMake(width_Cell, 0); 
    self.collectionView.contentOffset = offsetPoint; 
}else 
{ 
    width_Cell = self.collectionView.contentOffset.x; 
} 
} 
+0

Ihre Kollektionzellenbreite zu Ihrem self.width gleich ??? oder zeigen Sie zuerst 3 Zellen, dann nach Scroll 3 Zellen? –

+0

@AgentChocks. nein, alles basiert auf Array-Anzahl. Scrolling Start vom ersten Element in Array bis zum Ende und dann umgekehrt – ChenSmile

+0

@AgentChocks. hast du irgendeine lösung – ChenSmile

Antwort

0

in der benutzerdefinierten Klasse Ihres collectionViewCell:

self.contentView.frame = self.bounds; 
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
            UIViewAutoresizingFlexibleHeight; 

und entfernen Sie alle anderen width Berechnungen.

0

Ich verwende wie diese

Variable deklarieren

var timer:Timer! 

Anruf von viewDidLoad

self.addTimer() 


func addTimer() { 
    let timer1 = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.nextPage), userInfo: nil, repeats: true) 
    RunLoop.main.add(timer1, forMode: RunLoopMode.commonModes) 
    self.timer = timer1 
} 


func resetIndexPath() -> IndexPath { 
    let currentIndexPath = self.collectionView.indexPathsForVisibleItems.last 
    let currentIndexPathReset = IndexPath(item: (currentIndexPath?.item)!, section: 0) 
    self.collectionView.scrollToItem(at: currentIndexPathReset, at: UICollectionViewScrollPosition.left, animated: true) 
    return currentIndexPath! 
} 

func removeTimer() { 
    if self.timer != nil { 
     self.timer.invalidate() 
    } 
    self.timer = nil 
} 


func nextPage() { 
    let currentIndexPathReset:IndexPath = self.resetIndexPath() 
    var nextItem = currentIndexPathReset.item + 1 
    let nextSection = currentIndexPathReset.section 
    if nextItem == productImage.count{ 
     nextItem = 0 
    } 
    var nextIndexPath = IndexPath(item: nextItem, section: nextSection) 
    if nextItem == 0 { 
     self.collectionView.scrollToItem(at: nextIndexPath, at: UICollectionViewScrollPosition.left, animated: false) 
    } 
    self.collectionView.scrollToItem(at: nextIndexPath, at: UICollectionViewScrollPosition.left, animated: true) 
} 



func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { 
    self.addTimer() 

} 

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { 
    self.removeTimer() 
} 

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    var visibleRect = CGRect() 
    visibleRect.origin = collectionView.contentOffset 
    visibleRect.size = collectionView.bounds.size 
    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY) 
    let visibleIndexPath: IndexPath? = collectionView.indexPathForItem(at: visiblePoint) 
    pageControlView.currentPage = (visibleIndexPath?.row)! 
}