【问题标题】:UICollectionView - Horizontal AutoScroll with TimerUICollectionView - 带计时器的水平自动滚动
【发布时间】:2017-01-02 16:14:36
【问题描述】:

我正在使用计时器使用水平自动滚动。我希望它一一滚动。

在我的例子中,它是从左到右连续滚动。最后它还在最后一个单元格之后滚动空白。

@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;
 }
}

【问题讨论】:

  • 你的collectionview单元格宽度等于你的self.width???还是先显示 3 个单元格,然后滚动 3 个单元格?
  • @AgentChocks。不,它全部基于数组计数。从数组中的第一个元素开始滚动到结束然后反向
  • @AgentChocks。你有什么解决办法吗
  • @imran 你成功了吗?空白到底是什么问题?

标签: objective-c uicollectionview uicollectionviewcell horizontal-scrolling


【解决方案1】:

我是这样用的

声明变量

var timer:Timer!

从 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)!
}

【讨论】:

    【解决方案2】:

    在您的collectionViewCell 的自定义类中:

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

    并删除任何其他width 计算。

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 2013-10-18
      • 2014-11-15
      • 2015-09-27
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多