JXSegmentedRTLLayout.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // JXSegmentedRTLLayout.swift
  3. // JXSegmentedView
  4. //
  5. // Created by blue on 2020/6/18.
  6. // Copyright © 2020 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol JXSegmentedViewRTLCompatible: AnyObject {
  10. func segmentedViewShouldRTLLayout() -> Bool
  11. func segmentedView(horizontalFlipForView view: UIView?)
  12. }
  13. public extension JXSegmentedViewRTLCompatible {
  14. /// 根据当前系统布局方式返回是否需要RTL布局
  15. func segmentedViewShouldRTLLayout() -> Bool {
  16. return UIView.userInterfaceLayoutDirection(for: UIView.appearance().semanticContentAttribute) == .rightToLeft
  17. }
  18. /// 在RTL布局下水平翻转当前视图
  19. /// - Parameter view: 需要翻转的视图
  20. func segmentedView(horizontalFlipForView view: UIView?) {
  21. view?.transform = CGAffineTransform(scaleX: -1, y: 1)
  22. }
  23. }
  24. class JXSegmentedRTLCollectionCell: UICollectionViewCell, JXSegmentedViewRTLCompatible {
  25. override init(frame: CGRect) {
  26. super.init(frame: frame)
  27. commonInit()
  28. }
  29. required init?(coder: NSCoder) {
  30. super.init(coder: coder)
  31. commonInit()
  32. }
  33. func commonInit() {
  34. if segmentedViewShouldRTLLayout() {
  35. segmentedView(horizontalFlipForView: self)
  36. segmentedView(horizontalFlipForView: contentView)
  37. }
  38. }
  39. }