gpt4 book ai didi

ios - Swift 中 headerView 中的动画

转载 作者:可可西里 更新时间:2023-11-01 01:59:08 25 4
gpt4 key购买 nike

我目前正在尝试向用户的 xp 栏添加动画。

如果我在 collectionViewController 中有动画,则动画会很好地加载(一次)。但是,如果我在 headerView 中有动画(因为我想在个人资料图片上添加栏),栏会多次启动:

enter image description here

这是我的代码(headerViewCell):

let shapeLayerXp = CAShapeLayer()

override func layoutSubviews() {
super.layoutSubviews()
showUserXp()
self.animateXp(toValue: 1)
}

func showUserXp() {
let center = profileImage.center
let circularPath = UIBezierPath(arcCenter: center, radius: 40, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
shapeLayerXp.path = circularPath.cgPath

let color = UIColor(red: 122 / 255, green: 205 / 255, blue: 186 / 255, alpha: 1)

shapeLayerXp.strokeColor = color.cgColor
shapeLayerXp.lineWidth = 4
shapeLayerXp.fillColor = UIColor.clear.cgColor
shapeLayerXp.lineCap = kCALineCapRound

shapeLayerXp.strokeEnd = 0

self.contentView.layer.addSublayer(shapeLayerXp)


}

func animateXp(toValue: Int) {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = toValue
basicAnimation.duration = 2

basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.isRemovedOnCompletion = false

shapeLayerXp.add(basicAnimation, forKey: "urSoBasic")
}

headerViewCell 是这样启动的:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

let headerViewCell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! UserHeaderView

// ....

return cell

}

最佳答案

假设 Controller 中只有一个标题单元格,除非数据发生更改(例如,不同的用户),然后您可以在 View Controller 上设置一个属性以指示是否已显示动画。

像这样的事情会做:

var animatedHeader = false // Starts false because we want an animation the first time.

然后在第一次获取标题单元格时,您可以像这样决定是否触发动画:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

let headerViewCell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! UserHeaderView

if !self.animatedHeader {
cell.showUserXp()
cell.animateXp()

self.animatedHeader = true
}

return cell
}

当然,showUserXp()animateXp() 方法需要公开。

使用此方法,标题单元格只会在第一次出列并因此显示时显示动画。

如果您确实想再次为其设置动画,您只需重置 animateHeader 属性并重新加载 Collection View (或仅加载标题)。

如果有多个 header ,则您需要分别跟踪每个 header 。

编辑:由于 showXP 和 animateXP 函数的定义方式,这确实需要(意外地)使用相同的单元格。如果我自己这样做,我可能会使用更像这样的方法:

func showUserXp(animated: Bool) {
let center = profileImage.center
let circularPath = UIBezierPath(arcCenter: center, radius: 40, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
shapeLayerXp.path = circularPath.cgPath

let color = UIColor(red: 122 / 255, green: 205 / 255, blue: 186 / 255, alpha: 1)

shapeLayerXp.strokeColor = color.cgColor
shapeLayerXp.lineWidth = 4
shapeLayerXp.fillColor = UIColor.clear.cgColor
shapeLayerXp.lineCap = kCALineCapRound

shapeLayerXp.strokeEnd = 0

self.contentView.layer.addSublayer(shapeLayerXp)

if animated {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = toValue
basicAnimation.duration = 2

basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.isRemovedOnCompletion = false

shapeLayerXp.add(basicAnimation, forKey: "urSoBasic")
} else {
shapeLayerXp.strokeEnd = 1
}
}

然后你会像这样使用它:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

let headerViewCell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! UserHeaderView

if self.animatedHeader {
cell.showUserXp(animated: false)
} else {
cell.showUserXp(animated: true)

self.animatedHeader = true
}

return cell
}

现在您可以使用动画或不使用动画来显示标题单元格,是否使用动画由 animatedHeader 属性控制。现在,这不再依赖于出列的特定单元格。

关于ios - Swift 中 headerView 中的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47981934/

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