Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView对动画无响应_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios UICollectionView对动画无响应

Ios UICollectionView对动画无响应,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我似乎不知道我做错了什么。我有一个UICollectionView,它可以在线加载图像。当图像加载时,我想给细胞设置动画。以下是使细胞对触摸没有反应(我似乎仍然能够使用细胞之间的间隙滚动)。有什么想法吗 class GridViewCell: UICollectionViewCell { var imageViewContent = UIImageView() var animatedView = UIView() required init?(coder a

我似乎不知道我做错了什么。我有一个UICollectionView,它可以在线加载图像。当图像加载时,我想给细胞设置动画。以下是使细胞对触摸没有反应(我似乎仍然能够使用细胞之间的间隙滚动)。有什么想法吗

class GridViewCell: UICollectionViewCell {

    var imageViewContent = UIImageView()
    var animatedView = UIView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        animatedView.frame = CGRect(x: 0, y: 0, width: gridItemSize, height: gridItemSize)
        contentView.addSubview(animatedView)
        
        imageViewContent.frame = CGRect(x: 0, y: 0, width: gridItemSize, height: gridItemSize)
        imageViewContent.contentMode = .scaleAspectFill
        imageViewContent.autoresizingMask = [.flexibleWidth, .flexibleWidth]
        imageViewContent.clipsToBounds = true
        contentView.addSubview(imageViewContent)
    }
    
    override func prepareForReuse() {
        imageViewContent.image = nil
        self.layer.removeAllAnimations()
        super.prepareForReuse()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        animatedView.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1)
        UIView.animate(withDuration: 1.0, delay: TimeInterval(CGFloat.random(in: 0.0...2.0)), options:[.repeat, .autoreverse, .curveEaseInOut], animations: {
            self.animatedView.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1)
        }, completion:nil)
    }
}

回答我自己的问题,以防有人碰到。您需要将.allowUserInteraction添加到UIView.animate的选项中:

UIView.animate(withDuration: 1.0, delay: TimeInterval(CGFloat.random(in: 0.0...2.0)), options:[.repeat, .autoreverse, .curveEaseInOut, .allowUserInteraction], animations: {
            self.animatedView.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1)
        }, completion:nil)

虽然可以非常频繁地调用
layoutSubviews()
方法,但您可能不希望每次都启动另一个动画。例如,如果旋转设备,则可能会为集合视图中的每个单元格调用LayoutSubView(),即使图像已加载。@VadimBelyaev谢谢,我将转到init
UIView.animate(withDuration: 1.0, delay: TimeInterval(CGFloat.random(in: 0.0...2.0)), options:[.repeat, .autoreverse, .curveEaseInOut, .allowUserInteraction], animations: {
            self.animatedView.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1)
        }, completion:nil)