Ios 如何从右到左而不是从左到右显示UICollectionView的单元格?
这是我现在拥有的(默认情况下): 但我想:Ios 如何从右到左而不是从左到右显示UICollectionView的单元格?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,这是我现在拥有的(默认情况下): 但我想: 我使用NSFetcheResultsController按属性降序显示它。在搜索任何可能的内置功能后,我遇到了同样的问题,这些功能使我能够轻松实现这一点,-不幸的是-我找不到这样的东西。我必须通过做这个把戏来解决这个问题: 为了演示的目的,我将展示这个-Shugh-collectionView,并向您展示它应该如何更改: 这个技巧的主要思想是镜像将scaleX更改为-1 让我们镜像整个集合视图: 附言:Swift 3代码 如果您的collecti
我使用
NSFetcheResultsController
按属性降序显示它。在搜索任何可能的内置功能后,我遇到了同样的问题,这些功能使我能够轻松实现这一点,-不幸的是-我找不到这样的东西。我必须通过做这个把戏来解决这个问题:
为了演示的目的,我将展示这个-Shugh-collectionView,并向您展示它应该如何更改:
这个技巧的主要思想是镜像将scaleX更改为-1
让我们镜像整个集合视图:
附言:Swift 3代码
如果您的collectionView没有IBOutlet,请创建一个。就我而言,我称之为collectionView
在viewController中:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.transform = CGAffineTransform(scaleX: -1, y: 1)
}
现在,它应该是这样的:
class CustomFlowLayout: UICollectionViewFlowLayout {
//MARK: - Private
private func reversedRect(for rect: CGRect) -> CGRect {
let point = reversedPoint(for: rect.origin)
let newPoint = CGPoint(x: point.x - rect.size.width, y: point.y)
return CGRect(x: newPoint.x, y: newPoint.y, width: rect.size.width, height: rect.size.height)
}
private func reversedPoint(for point: CGPoint) -> CGPoint {
return CGPoint(x: collectionViewContentSize.width - point.x, y: point.y)
}
//MARK: - Overridden
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: reversedRect(for: rect)) else {
return nil
}
for attribute in attributes {
attribute.center = reversedPoint(for: attribute.center)
}
return attributes
}
}
“怎么回事?!”
到目前为止还不错,事实上这正是您要寻找的,但您仍然需要为每个单元格内容添加另一个mirroring机制
在自定义单元格的类中:
override func awakeFromNib() {
super.awakeFromNib()
contentView.transform = CGAffineTransform(scaleX: -1, y: 1)
}
现在应该是这样的:
class CustomFlowLayout: UICollectionViewFlowLayout {
//MARK: - Private
private func reversedRect(for rect: CGRect) -> CGRect {
let point = reversedPoint(for: rect.origin)
let newPoint = CGPoint(x: point.x - rect.size.width, y: point.y)
return CGRect(x: newPoint.x, y: newPoint.y, width: rect.size.width, height: rect.size.height)
}
private func reversedPoint(for point: CGPoint) -> CGPoint {
return CGPoint(x: collectionViewContentSize.width - point.x, y: point.y)
}
//MARK: - Overridden
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: reversedRect(for: rect)) else {
return nil
}
for attribute in attributes {
attribute.center = reversedPoint(for: attribute.center)
}
return attributes
}
}
希望能有所帮助。另一个解决方案是将
UICollectionViewFlowLayout
子类化,并按如下方式实现:
class CustomFlowLayout: UICollectionViewFlowLayout {
//MARK: - Private
private func reversedRect(for rect: CGRect) -> CGRect {
let point = reversedPoint(for: rect.origin)
let newPoint = CGPoint(x: point.x - rect.size.width, y: point.y)
return CGRect(x: newPoint.x, y: newPoint.y, width: rect.size.width, height: rect.size.height)
}
private func reversedPoint(for point: CGPoint) -> CGPoint {
return CGPoint(x: collectionViewContentSize.width - point.x, y: point.y)
}
//MARK: - Overridden
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: reversedRect(for: rect)) else {
return nil
}
for attribute in attributes {
attribute.center = reversedPoint(for: attribute.center)
}
return attributes
}
}
只需在Interface Builder中分配它。实现它的最简单方法是更改集合视图枚举实例属性的值: 视图内容的语义描述,用于确定 切换时是否应翻转视图 从左到右和从右到左的布局 通过设置为
.forceRightToLeft
,如下所示:
// 'viewDidLoad' would be a proper method to do it:
override func viewDidLoad() {
// ...
collectionView.semanticContentAttribute = .forceRightToLeft
// ...
}
没错,这不是一种优雅的方式,但至少在解决问题时是这样!正如我所提到的,我找不到更好的方法来做……您好@Bartłomiejsemanńczyk。我想出了更简单的方法,我想和你们分享,希望能有所帮助:)