0

타이머를 사용하여 가로 자동 ​​스크롤을 사용하고 있습니다. 하나씩 스크롤하고 싶습니다.UICollectionView - 타이머가있는 가로 자동 ​​스크롤

필자의 경우에는 왼쪽에서 오른쪽으로 continuos를 스크롤하고있다. 마지막으로 마지막 셀 다음에 공백을 스크롤합니다.

@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

당신의 collectionview 세포 폭이 self.width 같다있는 viewDidLoad에서 변수

var timer:Timer! 

전화를 선언이

처럼 사용하고 ??? 또는 스크롤 셀 3 다음에 처음으로 3 개의 셀을 표시합니까? –

+0

@AgentChocks. 어레이 카운트를 기반으로 한 모든 것이 없습니다. 배열에서 첫 번째 요소부터 끝까지 스크롤 한 다음 역순으로 뒤 스크롤 – ChenSmile

+0

@AgentChocks. 너 어떤 해결책이있어? – ChenSmile

답변

0
당신의 collectionViewCell의 사용자 정의 클래스에서

:

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

및 기타 width 계산을 제거합니다.

0

나는

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)! 
} 
관련 문제