iOS上的深度页面变换

前端之家收集整理的这篇文章主要介绍了iOS上的深度页面变换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个像Facebook菜单幻灯片动画POP框架的动画,或者完全像 InShorts App. Android Documentation涵盖了这一点.但是在iOS中找不到任何提示.

我试过的是将单元格变换为

  1. func tableView(tableView: UITableView,willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
  2.  
  3. UIView.animateWithDuration(0.1,animations: { () -> Void in
  4. cell.transform = CGAffineTransformMakeScale(0.8,0.8)
  5. })
  6.  
  7. }

但是这并不如预期的那样工作.我发现this一个我想要达到的目标.但它在UIView.如果我们在U​​ITableViewCell中动画,我认为最适合我真的很感激有人愿意帮助这个问题. Here是我正在开展的启动项目

解决方法

这个效果可以使用UICollectionView获得.这是UICollectionViewFlowLayout类.
  1. class UltravisualLayout: UICollectionViewLayout {
  2.  
  3. private var contentWidth:CGFloat!
  4. private var contentHeight:CGFloat!
  5. private var yOffset:CGFloat = 0
  6.  
  7. var maxAlpha:CGFloat = 1
  8. var minAlpha:CGFloat = 0
  9.  
  10. var widthOffset:CGFloat = 35
  11. var heightOffset:CGFloat = 35
  12.  
  13. private var cache = [UICollectionViewLayoutAttributes]()
  14.  
  15. private var itemWidth:CGFloat{
  16. return (collectionView?.bounds.width)!
  17. }
  18. private var itemHeight:CGFloat{
  19. return (collectionView?.bounds.height)!
  20. }
  21. private var collectionViewHeight:CGFloat{
  22. return (collectionView?.bounds.height)!
  23. }
  24.  
  25.  
  26. private var numberOfItems:Int{
  27. return (collectionView?.numberOfItemsInSection(0))!
  28. }
  29.  
  30.  
  31. private var dragOffset:CGFloat{
  32. return (collectionView?.bounds.height)!
  33. }
  34. private var currentItemIndex:Int{
  35. return max(0,Int(collectionView!.contentOffset.y / collectionViewHeight))
  36. }
  37.  
  38. var nextItemBecomeCurrentPercentage:CGFloat{
  39. return (collectionView!.contentOffset.y / (collectionViewHeight)) - CGFloat(currentItemIndex)
  40. }
  41.  
  42. override func prepareLayout() {
  43. cache.removeAll(keepCapacity: false)
  44. yOffset = 0
  45.  
  46. for item in 0 ..< numberOfItems{
  47.  
  48. let indexPath = NSIndexPath(forItem: item,inSection: 0)
  49. let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
  50. attribute.zIndex = -indexPath.row
  51.  
  52. if (indexPath.item == currentItemIndex+1) && (indexPath.item < numberOfItems){
  53.  
  54. attribute.alpha = minAlpha + max((maxAlpha-minAlpha) * nextItemBecomeCurrentPercentage,0)
  55. let width = itemWidth - widthOffset + (widthOffset * nextItemBecomeCurrentPercentage)
  56. let height = itemHeight - heightOffset + (heightOffset * nextItemBecomeCurrentPercentage)
  57.  
  58. let deltaWidth = width/itemWidth
  59. let deltaHeight = height/itemHeight
  60.  
  61. attribute.frame = CGRectMake(0,yOffset,itemWidth,itemHeight)
  62. attribute.transform = CGAffineTransformMakeScale(deltaWidth,deltaHeight)
  63.  
  64. attribute.center.y = (collectionView?.center.y)! + (collectionView?.contentOffset.y)!
  65. attribute.center.x = (collectionView?.center.x)! + (collectionView?.contentOffset.x)!
  66. yOffset += collectionViewHeight
  67.  
  68. }else{
  69. attribute.frame = CGRectMake(0,itemHeight)
  70. attribute.center.y = (collectionView?.center.y)! + yOffset
  71. attribute.center.x = (collectionView?.center.x)!
  72. yOffset += collectionViewHeight
  73. }
  74. cache.append(attribute)
  75. }
  76. }
  77.  
  78. //Return the size of ContentView
  79. override func collectionViewContentSize() -> CGSize {
  80. contentWidth = (collectionView?.bounds.width)!
  81. contentHeight = CGFloat(numberOfItems) * (collectionView?.bounds.height)!
  82. return CGSizeMake(contentWidth,contentHeight)
  83. }
  84.  
  85. //Return Attributes whose frame lies in the Visible Rect
  86. override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  87. var layoutAttributes = [UICollectionViewLayoutAttributes]()
  88. for attribute in cache{
  89. if CGRectIntersectsRect(attribute.frame,rect){
  90. layoutAttributes.append(attribute)
  91. }
  92. }
  93. return layoutAttributes
  94. }
  95.  
  96.  
  97. override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
  98. return true
  99. }
  100.  
  101. override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint,withScrollingVelocity velocity: CGPoint) -> CGPoint {
  102. let itemIndex = round(proposedContentOffset.y / (dragOffset))
  103. let yOffset = itemIndex * (collectionView?.bounds.height)!
  104. return CGPoint(x: 0,y: yOffset)
  105. }
  106. override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
  107.  
  108. // Logic that calculates the UICollectionViewLayoutAttributes of the item
  109. // and returns the UICollectionViewLayoutAttributes
  110. return UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
  111. }
  112.  
  113. }

这是演示项目link

非常感谢这tutorial.

猜你在找的iOS相关文章