首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用UICollectionView实现NSTimer自动滚动?

如何使用UICollectionView实现NSTimer自动滚动?
EN

Stack Overflow用户
提问于 2016-02-17 12:39:09
回答 8查看 12.3K关注 0票数 7

如何使用UIcollectionView在水平位置自动滚动NSTimer

我的项目来源如下:

代码语言:javascript
运行
复制
- (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);
}

用上面的代码试过了,但不适合我。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2016-02-17 13:27:45

// Swift-3

代码语言:javascript
运行
复制
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
        }
    }

//这工作得很好

票数 6
EN

Stack Overflow用户

发布于 2019-01-15 19:03:26

Swift 4.2

运行环和计时器块。调整0.015 & 0.25值,使其与所需速度相匹配。

代码语言:javascript
运行
复制
    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)
    }
票数 8
EN

Stack Overflow用户

发布于 2018-08-12 07:24:04

代码语言:javascript
运行
复制
 @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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35456988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档