Ios 具有背景图像的CollectionView
我使用带有透明背景的collectionView和collectionView下的imageView。我根据中间的单元格改变图像视图,但是图像没有正确显示,如何解决它,它应该是怎样的一个例子,看起来低于 Gif: 工作原理: 工作原理: CollectionViewCellIos 具有背景图像的CollectionView,ios,swift,uiimageview,uicollectionview,uicollectionviewcell,Ios,Swift,Uiimageview,Uicollectionview,Uicollectionviewcell,我使用带有透明背景的collectionView和collectionView下的imageView。我根据中间的单元格改变图像视图,但是图像没有正确显示,如何解决它,它应该是怎样的一个例子,看起来低于 Gif: 工作原理: 工作原理: CollectionViewCell class CustomPagerViewCell: UICollectionViewCell { @IBOutlet weak var ImageView: UIImageView! @IBOutlet weak
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
}
插入部分位于
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)
}
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)
}
}
CollectionViewFlowLayout
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)
}
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)
}
}
class CenterCellCollectionViewFlowLayout:UICollectionViewFlowLayout{
覆盖func targetContentOffset(forProposedContentOffset proposedContentOffset:CGPoint,使用CrollingVelocity:CGPoint)->CGPoint{
如果让cv=self.collectionView{
设cvBounds=cv.bounds
设半宽=cvBounds.size.width*0.5;
设proposedContentOffsetCenterX=proposedContentOffset.x+半宽;
如果let attributesForVisibleCells=self.layouttributesforElements(in:cvBounds){
var CandidateAttribute:UICollectionViewLayoutAttribute?
对于attributesForVisibleCells中的属性{
//==跳过与非单元格项(页眉和页脚)的比较==//
如果attributes.representedElementCategory!=UICollectionElementCategory.cell{
持续
}
如果让candAttrs=candidateAttribute{
设a=attributes.center.x-proposedContentOffsetCenterX
设b=candAttrs.center.x-proposedContentOffsetCenterX
如果fabsf(浮动(a))