简体   繁体   English

Swift-水平滚动收集视图单元(自动和手动)

[英]Swift - horizontal scroll collection view cells (automatically and manually)

In my app I have used AVPlayer to play video. 在我的应用程序中,我使用AVPlayer播放视频。 By using the following function I am getting frames from the video in each second (if video length is 2 minutes(120 seconds) I am getting 120 frames). 通过使用以下功能,我每秒获得视频中的帧(如果视频长度为2分钟(120秒),则得到120帧)。 This frames are stored in an array videoFrames 此帧存储在数组videoFrames中

var videoFrames = [UIImage]()
 func generateFrames(url: NSURL, fromTime: Float64) {
        let asset: AVAsset = AVAsset(url: url as URL)
        let assetImgGenerate: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
        assetImgGenerate.maximumSize = CGSize(width: 300, height: 300)
        assetImgGenerate.appliesPreferredTrackTransform = true
        let time: CMTime = CMTimeMakeWithSeconds(fromTime, 600)
        var img: CGImage?
        do {
            img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
        } catch {
        }
        if img != nil {
            let frameImg: UIImage = UIImage(cgImage: img!)
            videoFrames.append(frameImg)
        } else {
//            return nil
        }
    }

This video frames are placed in a horizontal scrolling UICollectionView like this 将此视频帧放置在这样的水平滚动UICollectionView中 在此处输入图片说明

Now I need to move the UICollectionView cells automatically from right to left and also allow the user(if wishes) to scroll manually. 现在,我需要将UICollectionView单元格从右向左自动移动,并且还允许用户(如果愿意)手动滚动。 (Exactly the same functionality when you play video in iPhone/iPod etc) (在iPhone / iPod等中播放视频时,功能完全相同)

I found this code... 我找到了这段代码...

func autoScroll () {
    let co = collectionView.contentOffset.x
    let no = co + 1

    UIView.animate(withDuration: 0.01, delay: 0, options: .curveEaseInOut, animations: { [weak self]() -> Void in
        self?.collectionView.contentOffset = CGPoint(x: no, y: 0)
    }) { [weak self](finished) -> Void in
        self?.autoScroll()
    }
}

but before finishing video the auto scroll finishes. 但在完成视频之前,自动滚动完成。 I can't determine what should be the value of withDuration: parameter in UIView.animate() . 我无法确定UIView.animate()withDuration:参数的值是什么。 Also if I add this autoScroll () function I can't scroll the UICollectionView cells manually. 另外,如果添加此autoScroll()函数,则无法手动滚动UICollectionView单元格。 But I need both automatically & manually. 但是我需要自动和手动。 Any assistance would be appreciated. 任何援助将不胜感激。

What you can do is initially create a variable called index with value 0. Now create a Timer function that will fire every 1 second and repeat itself, inside the selector for the Timer just use scrollToItem method of the collection view and keep incrementing the index variable. 您可以做的是首先创建一个名为index的变量,其值为0。现在创建一个Timer函数,它将每1秒触发一次并重复一次,在Timer的选择器内部,只需使用集合视图的scrollToItem方法并保持索引变量递增。

Now when the user begins scrolling in the collectionView just invalidate the timer and calculate the index based on the scroll amount. 现在,当用户开始在collectionView滚动时,只需使计时器无效并根据滚动量计算索引 After the scrolling, restart the timer again. 滚动后,请重新启动计时器。 Remember to move the video to the appropriate location as well! 切记也将视频移到适当的位置!

Edit: 编辑:

var timer : Timer!

var index = 0

override func viewDidAppear(_ animated:Bool){

     //call super here

     timer = Timer(timeInterval: 1, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: true)
}

func handleTimer(){

    index += 1

    let indexPath = IndexPath(row: index, section: 0)

    main.collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM