gpt4 book ai didi

ios - iOS 上的深度页面转换

转载 作者:IT王子 更新时间:2023-10-29 05:17:19 24 4
gpt4 key购买 nike

我正在尝试创建一个类似于 POP 框架的 Facebook 菜单滑动动画或完全类似于 InShorts App 的动画. Android Documentation覆盖这个..但在 iOS 中找不到任何提示。

我尝试的是将单元格转换为

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

UIView.animateWithDuration(0.1, animations: { () -> Void in
cell.transform = CGAffineTransformMakeScale(0.8, 0.8)
})

}

但这并没有像预期的那样工作..谷歌搜索我找到了this一个正是我想要实现的目标。但它在 UIView 上。我认为如果我们在 UITableViewCell 中制作动画是最合适的?我真的很感激有人愿意帮助解决这个问题。 Here是我正在做的入门项目

最佳答案

这个效果可以使用UICollectionView来实现,这里是UICollectionViewFlowLayout类。

class UltravisualLayout: UICollectionViewLayout {

private var contentWidth:CGFloat!
private var contentHeight:CGFloat!
private var yOffset:CGFloat = 0

var maxAlpha:CGFloat = 1
var minAlpha:CGFloat = 0

var widthOffset:CGFloat = 35
var heightOffset:CGFloat = 35

private var cache = [UICollectionViewLayoutAttributes]()

private var itemWidth:CGFloat{
return (collectionView?.bounds.width)!
}
private var itemHeight:CGFloat{
return (collectionView?.bounds.height)!
}
private var collectionViewHeight:CGFloat{
return (collectionView?.bounds.height)!
}


private var numberOfItems:Int{
return (collectionView?.numberOfItemsInSection(0))!
}


private var dragOffset:CGFloat{
return (collectionView?.bounds.height)!
}
private var currentItemIndex:Int{
return max(0, Int(collectionView!.contentOffset.y / collectionViewHeight))
}

var nextItemBecomeCurrentPercentage:CGFloat{
return (collectionView!.contentOffset.y / (collectionViewHeight)) - CGFloat(currentItemIndex)
}

override func prepareLayout() {
cache.removeAll(keepCapacity: false)
yOffset = 0

for item in 0 ..< numberOfItems{

let indexPath = NSIndexPath(forItem: item, inSection: 0)
let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attribute.zIndex = -indexPath.row

if (indexPath.item == currentItemIndex+1) && (indexPath.item < numberOfItems){

attribute.alpha = minAlpha + max((maxAlpha-minAlpha) * nextItemBecomeCurrentPercentage, 0)
let width = itemWidth - widthOffset + (widthOffset * nextItemBecomeCurrentPercentage)
let height = itemHeight - heightOffset + (heightOffset * nextItemBecomeCurrentPercentage)

let deltaWidth = width/itemWidth
let deltaHeight = height/itemHeight

attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.transform = CGAffineTransformMakeScale(deltaWidth, deltaHeight)

attribute.center.y = (collectionView?.center.y)! + (collectionView?.contentOffset.y)!
attribute.center.x = (collectionView?.center.x)! + (collectionView?.contentOffset.x)!
yOffset += collectionViewHeight

}else{
attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.center.y = (collectionView?.center.y)! + yOffset
attribute.center.x = (collectionView?.center.x)!
yOffset += collectionViewHeight
}
cache.append(attribute)
}
}

//Return the size of ContentView
override func collectionViewContentSize() -> CGSize {
contentWidth = (collectionView?.bounds.width)!
contentHeight = CGFloat(numberOfItems) * (collectionView?.bounds.height)!
return CGSizeMake(contentWidth, contentHeight)
}

//Return Attributes whose frame lies in the Visible Rect
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attribute in cache{
if CGRectIntersectsRect(attribute.frame, rect){
layoutAttributes.append(attribute)
}
}
return layoutAttributes
}


override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let itemIndex = round(proposedContentOffset.y / (dragOffset))
let yOffset = itemIndex * (collectionView?.bounds.height)!
return CGPoint(x: 0, y: yOffset)
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {

// Logic that calculates the UICollectionViewLayoutAttributes of the item
// and returns the UICollectionViewLayoutAttributes
return UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
}

}

这是演示项目 link ...

非常感谢这个tutorial .

关于ios - iOS 上的深度页面转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com