- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前正在尝试向用户的 xp 栏添加动画。
如果我在 collectionViewController 中有动画,则动画会很好地加载(一次)。但是,如果我在 headerView 中有动画(因为我想在个人资料图片上添加栏),栏会多次启动:
这是我的代码(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/
我试图“锚定” ListView 的标题 View ,这意味着当我向下滚动列表时,我不希望标题随之滚动。我希望 header 始终保持静止。众所周知,这是一个小代码 fragment ,用于将标题放置
我有两个不同的 header xml 布局,想以编程方式在它们之间切换。我的问题是旧布局没有被新布局取代,而是我最终得到了两个标题布局。我读了很多Answers1 , Answer2但它不起作用。 这
我遇到了 UITableView 及其属性 tableHeaderView 的问题。 我想让 tableHeaderView 表现得像 UISearchBar,即内容偏移量应该是 tableHeade
我对 EGORefreshTableHeaderView 的使用有疑问: 如果我使用此代码,就可以了: - (void)egoRefreshTableHeaderDidTriggerRefresh:
我正在尝试在我的 UITableView headerView 上应用一个大小,但似乎第一个部分的标题总是比其余部分的 headerViews 小一点。我已经说过 UITableView 的约束,所以
当我向 UITableView(使用 CALayer)添加边框时,它还会向 UITableView.tableHeaderView 添加边框 但我只需要表格周围的边框而不是标题。 由于我的表中只有一个
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我想从当时位于顶部的表 HeaderView 中获取一个 NSString。因为我想在 ScrollViewDidScroll 中实现它,所以当标题 View 更改时,一个标签也会更改。 没有在网上找
我需要获取在我选择的行的页眉 View 中设置的字符串。我需要此信息来决定将行发送到哪个 View 。我已经有了行名称。但我还需要在 Headerview 中使用的字符串的名称。 最佳答案 按照步骤
我为 UITableView 创建了一个自定义标题 View 。标题 View 的类型是 UITableViewCell。我在 Storyboard 中实现了它,并将其添加到 View 层次结构中,如
我有一个带有按钮的原型(prototype)标题 View ,我希望能够单击该按钮以转到另一个 View Controller (TargetViewController)。 但是,由于某种原因,我无
CollectionView 很新,所以希望在这里有一个方向。 我有一个启用了 header 的 CollectionView。一切正常,但我希望 HeaderView 在滚动主要部分时保持静止。 问
这是我将列表添加到列表 fragment 的代码: public void onAttach(Activity activity) { super.onAttach(activity);
我试图调整 View 大小或添加约束但不起作用。 这是无效的代码: - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSec
我在 viewDidLoad 中使用从我的 tableview 中删除了分隔符左边距: self.tableView.layoutMargins = UIEdgeInsetsZero self.tab
所以我是新手。 我有一个 nib 文件,其中包含多个兄弟 View 。 ViewController 的 View 包含一个 tableView,我有另一个 View 将被添加到 tableHeade
我目前正在尝试向用户的 xp 栏添加动画。 如果我在 collectionViewController 中有动画,则动画会很好地加载(一次)。但是,如果我在 headerView 中有动画(因为我想在
我有一个 UITableViewController。我在 loadView 方法中为其 tableView 创建了一个自定义 headerView,如下所示: (void)loadView {
我在我的 UIViewController 中有一个 UITableView 并且已经添加了一个 UIRefreshControl: lazy var refreshControl: UIRefres
我目前正在尝试为 UITableView 偏移 HeaderView 以获得一些存折 View 的感觉,但不需要表现得像存折,只是上部 View 偏移某些像素以覆盖部分。 下面是我一直在研究的几个例子
我是一名优秀的程序员,十分优秀!