2016-11-02 170 views

回答

2

在寻找任何可能的内置功能后,我遇到了同样的问题,让我能够轻松完成这一任务 - 不幸的是,我找不到这样的事情。我有我的自我做这种伎俩来解决这个问题:

出于演示的目的,我将在本-ugly-的CollectionView,并告诉你应该如何变化:

enter image description here

诀窍的主要思想是镜像!将scaleX更改为-1。

让我们的镜子全收观点:

P.S:斯威夫特3码。

如果您的collectionView没有IBOutlet,请创建一个。在我的情况即时调用它collectionView

在的viewController:

override func viewDidLoad() { 
    super.viewDidLoad() 

    collectionView.transform = CGAffineTransform(scaleX: -1, y: 1) 
} 

现在,它应该是这样的:

enter image description here

“什么鬼?”

到目前为止,实际上,这正是您正在寻找的内容,但您仍然需要为每个单元格内容添加另一个镜像机制。

在您的自定义单元格的类:

override func awakeFromNib() { 
    super.awakeFromNib() 

    contentView.transform = CGAffineTransform(scaleX: -1, y: 1) 
} 

现在看起来应该是这样:

enter image description here

希望它帮助。

+0

很酷:)不优雅的方式...但我留下深刻的印象:) –

+0

这是正确的这不是一个优雅的方式,但至少可以解决它的问题!正如我所提到的,我找不到更好的方法来做到这一点...... –

1

另一种解决方案是继承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只需要给它。

1

实现这一目标最简单的方法是改变集合视图semanticContentAttribute枚举实例属性的值:

的视图的内容,用来确定 的语义描述是否认为应该翻转当在从左到右和从右到左布局的 之间切换时。

通过设置到.forceRightToLeft,如下:

// 'viewDidLoad' would be a proper method to do it: 
override func viewDidLoad() { 
    // ... 

    collectionVIew.semanticContentAttribute = .forceRightToLeft 

    // ... 
}