问题
I use collectionView with a transparent background and imageView under the CollectionView. I change the ImageView depending on the cell that is in the middle, but the image are not displayed correctly, how you can solve it, an example of how it should look and looks below
Gif:
how it should work:
how it works:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionViewRecomend.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomPagerViewCell
let m = modals[indexPath.item]
cell?.ImageView.image = m.image
cell?.ImageView.backgroundColor = .white
cell?.Name.text = m.name
cell?.descriptions.text = m.description
cell?.Folowers.text = String(m.folower)
cell?.Publish.text = String(m.publish)
ImageSlider.image = modals[indexPath.row].image
cell?.liking = { (cells) in
if cell?.like.tintColor == UIColor(hex: 0xD1D1D1, alpha: 1) {
cell?.like.tintColor = UIColor.orange
self.favorite(cell: cell ?? UITableViewCell())
} else {
cell?.like.tintColor = UIColor(hex: 0xD1D1D1, alpha: 1)
self.favorite(cell: cell ?? UITableViewCell())
}
}
return cell!
}
CollectionViewCell
class CustomPagerViewCell: UICollectionViewCell {
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var Name: UILabel!
@IBOutlet weak var descriptions: UILabel!
@IBOutlet weak var Publish: UILabel!
@IBOutlet weak var Folowers: UILabel!
@IBOutlet weak var BG: UIView!
@IBOutlet weak var like: UIButton!
@objc var liking: ((UICollectionViewCell) -> Void)?
@IBAction func like(_ sender: Any) {
liking?(self)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
like.tintColor = UIColor(hex: 0xD1D1D1, alpha: 1)
ImageView.contentMode = .scaleAspectFit
ImageView.layer.cornerRadius = 35
ImageView.layer.borderWidth = 1
ImageView.layer.borderColor = UIColor(hex: 0xD4D4D4, alpha: 1).cgColor
ImageView.layer.masksToBounds = true
}
insetForSectionAt
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let cellWidth : CGFloat = self.collectionViewMenu.frame.width/1.2
let numberOfCells = floor(self.view.frame.size.width / cellWidth)
let edgeInsets = (self.view.frame.size.width - (numberOfCells * cellWidth)) / (numberOfCells + 1)
return UIEdgeInsetsMake(collectionViewRecomend.frame.height - 165, edgeInsets, 0, edgeInsets)
}
CollectionViewFlowLayout
class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
if let cv = self.collectionView {
let cvBounds = cv.bounds
let halfWidth = cvBounds.size.width * 0.5;
let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth;
if let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) {
var candidateAttributes : UICollectionViewLayoutAttributes?
for attributes in attributesForVisibleCells {
// == Skip comparison with non-cell items (headers and footers) == //
if attributes.representedElementCategory != UICollectionElementCategory.cell {
continue
}
if let candAttrs = candidateAttributes {
let a = attributes.center.x - proposedContentOffsetCenterX
let b = candAttrs.center.x - proposedContentOffsetCenterX
if fabsf(Float(a)) < fabsf(Float(b)) {
candidateAttributes = attributes;
}
}
else { // == First time in the loop == //
candidateAttributes = attributes;
continue;
}
}
return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y);
}
}
// Fallback
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
}
}
来源:https://stackoverflow.com/questions/47679952/collectionview-with-background-image