子视图从集合视图单元格中消失并滚动
Subview Disappeas from Collection View Cell with scrolling
我有一个来自故事板的 UIImageView
,在我的 UICollectionViewController
中有一个出口:
@IBOutlet var playButtonView: UIImageView!
Storyboard 不允许我在除 View Controller 之外的任何地方创建 outlet,因为我在 VC 外部创建了视图 canvas 所以我可以动态定位它:
因此,我尝试将视图设置为单元格的子视图,如下所示:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "metadataCell", for: indexPath) as? MetaDataCustomCell
else { fatalError("Unexpected cell in collection view") }
if !cell.subviews.contains(playButtonView) {
let playButtonX = cell.thumbnailImageView.frame.width/2 - playButtonWidthAndHeight/2
let playButtonY = cell.thumbnailImageView.frame.height/2 - playButtonWidthAndHeight/2
playButtonView.frame = CGRect(x: playButtonX, y: playButtonY, width: playButtonWidthAndHeight, height: playButtonWidthAndHeight)
cell.addSubview(playButtonView)
} else {
cell.sendSubviewToBack(playButtonView)
}
cell.representedAssetIdentifier = asset.localIdentifier
DispatchQueue.global(qos: .userInitiated).async {
let requestOption = PHImageRequestOptions()
requestOption.isSynchronous = true
self.imageManager.requestImage(for: asset, targetSize: self.photoSize, contentMode: .aspectFit, options: requestOption, resultHandler: { image, _ in
if cell.representedAssetIdentifier == asset.localIdentifier {
DispatchQueue.main.async {
cell.thumbnailImageView.image = image
}
self.getInfoForAsset(asset: asset) {(assetURL,urlAsset) in
if let url = assetURL {
if asset.mediaType == .image {
self.getDataFromImageURL(imageURL: url, imageAsset: asset, handler: { (metadataDict) in
DispatchQueue.main.async {
cell.dateLabel.text = metadataDict["date"]
}
})
} else if asset.mediaType == .video {
DispatchQueue.main.async {
self.playButtonView.isHidden = false
cell.bringSubviewToFront(self.playButtonView)
}
}
}
}
}
})
}
return cell
现在,问题是 playButtonView 仅在加载第一个单元格时出现,在我再次向前或向后滚动到同一单元格后,它消失了。有什么想法吗?
您的 playButtonView
是视图控制器的 属性 而不是 MetaDataCustomCell
。除非您只有一个 MetaDataCustomCell
,否则这不会起作用,因为您只有一个 playButtonView
,您将始终从一个单元格中删除并添加到下一个单元格。将 属性 移至您的单元格。
我有一个来自故事板的 UIImageView
,在我的 UICollectionViewController
中有一个出口:
@IBOutlet var playButtonView: UIImageView!
Storyboard 不允许我在除 View Controller 之外的任何地方创建 outlet,因为我在 VC 外部创建了视图 canvas 所以我可以动态定位它:
因此,我尝试将视图设置为单元格的子视图,如下所示:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "metadataCell", for: indexPath) as? MetaDataCustomCell
else { fatalError("Unexpected cell in collection view") }
if !cell.subviews.contains(playButtonView) {
let playButtonX = cell.thumbnailImageView.frame.width/2 - playButtonWidthAndHeight/2
let playButtonY = cell.thumbnailImageView.frame.height/2 - playButtonWidthAndHeight/2
playButtonView.frame = CGRect(x: playButtonX, y: playButtonY, width: playButtonWidthAndHeight, height: playButtonWidthAndHeight)
cell.addSubview(playButtonView)
} else {
cell.sendSubviewToBack(playButtonView)
}
cell.representedAssetIdentifier = asset.localIdentifier
DispatchQueue.global(qos: .userInitiated).async {
let requestOption = PHImageRequestOptions()
requestOption.isSynchronous = true
self.imageManager.requestImage(for: asset, targetSize: self.photoSize, contentMode: .aspectFit, options: requestOption, resultHandler: { image, _ in
if cell.representedAssetIdentifier == asset.localIdentifier {
DispatchQueue.main.async {
cell.thumbnailImageView.image = image
}
self.getInfoForAsset(asset: asset) {(assetURL,urlAsset) in
if let url = assetURL {
if asset.mediaType == .image {
self.getDataFromImageURL(imageURL: url, imageAsset: asset, handler: { (metadataDict) in
DispatchQueue.main.async {
cell.dateLabel.text = metadataDict["date"]
}
})
} else if asset.mediaType == .video {
DispatchQueue.main.async {
self.playButtonView.isHidden = false
cell.bringSubviewToFront(self.playButtonView)
}
}
}
}
}
})
}
return cell
现在,问题是 playButtonView 仅在加载第一个单元格时出现,在我再次向前或向后滚动到同一单元格后,它消失了。有什么想法吗?
您的 playButtonView
是视图控制器的 属性 而不是 MetaDataCustomCell
。除非您只有一个 MetaDataCustomCell
,否则这不会起作用,因为您只有一个 playButtonView
,您将始终从一个单元格中删除并添加到下一个单元格。将 属性 移至您的单元格。