优惠券swift版马甲包

ZCycleLayout.swift 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // ZCycleLayout.swift
  3. // ZCycleView
  4. //
  5. // Created by mengqingzheng on 2017/11/24.
  6. // Copyright © 2017年 MQZHot. All rights reserved.
  7. //
  8. import UIKit
  9. class ZCycleLayout: UICollectionViewFlowLayout {
  10. var scale: CGFloat = 1 {
  11. didSet {
  12. if scale >= 1 {
  13. invalidateLayout()
  14. }
  15. }
  16. }
  17. override func prepare() {
  18. super.prepare()
  19. if let collectionView = collectionView {
  20. if scrollDirection == .horizontal {
  21. let offset = (collectionView.frame.size.width-itemSize.width)/2
  22. sectionInset = UIEdgeInsetsMake(0, offset, 0, 0)
  23. } else {
  24. let offset = (collectionView.frame.size.height-itemSize.height)/2
  25. sectionInset = UIEdgeInsetsMake(offset, 0, 0, 0)
  26. }
  27. }
  28. }
  29. override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
  30. return true
  31. }
  32. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  33. if let attributes = super.layoutAttributesForElements(in: rect),
  34. let collectionView = collectionView {
  35. let attris = NSArray(array: attributes, copyItems:true) as! [UICollectionViewLayoutAttributes]
  36. for attri in attris {
  37. var scale: CGFloat = 1
  38. var absOffset: CGFloat = 0
  39. let centerX = collectionView.bounds.size.width*0.5 + collectionView.contentOffset.x
  40. let centerY = collectionView.bounds.size.height*0.5 + collectionView.contentOffset.y
  41. if scrollDirection == .horizontal {
  42. absOffset = abs(attri.center.x-centerX)
  43. let distance = itemSize.width+minimumLineSpacing
  44. if absOffset < distance {///当前index
  45. scale = (1-absOffset/distance)*(self.scale-1) + 1
  46. }
  47. } else {
  48. absOffset = abs(attri.center.y-centerY)
  49. let distance = itemSize.height+minimumLineSpacing
  50. if absOffset < distance {
  51. scale = (1-absOffset/distance)*(self.scale-1) + 1
  52. }
  53. }
  54. attri.zIndex = Int(scale * 1000)
  55. attri.transform = CGAffineTransform(scaleX:scale,y: scale)
  56. }
  57. return attris
  58. }
  59. return nil
  60. }
  61. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
  62. var minSpace = CGFloat.greatestFiniteMagnitude
  63. var offset = proposedContentOffset
  64. if let collectionView = collectionView {
  65. let centerX = offset.x+collectionView.bounds.size.width/2
  66. let centerY = offset.y+collectionView.bounds.size.height/2
  67. var visibleRect: CGRect
  68. if scrollDirection == .horizontal {
  69. visibleRect = CGRect(origin: CGPoint(x: offset.x, y: 0), size: collectionView.bounds.size)
  70. } else {
  71. visibleRect = CGRect(origin: CGPoint(x: 0, y: offset.y), size: collectionView.bounds.size)
  72. }
  73. if let attris = layoutAttributesForElements(in: visibleRect) {
  74. for attri in attris {
  75. if scrollDirection == .horizontal {
  76. if abs(minSpace) > abs(attri.center.x-centerX) {
  77. minSpace = attri.center.x-centerX
  78. }
  79. } else {
  80. if abs(minSpace) > abs(attri.center.y-centerY) {
  81. minSpace = attri.center.y-centerY
  82. }
  83. }
  84. }
  85. }
  86. if scrollDirection == .horizontal {
  87. offset.x+=minSpace
  88. } else {
  89. offset.y+=minSpace
  90. }
  91. }
  92. return offset
  93. }
  94. }