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