gpt4 book ai didi

ios - 动画 UITableView 索引,像 Apple Music 那样?

转载 作者:行者123 更新时间:2023-11-29 01:47:15 24 4
gpt4 key购买 nike

简单问题:

Apple Music 的表格 View 只会在向下滚动超过某个阈值后在右边缘显示 A-Z 部分索引,并且该索引可以很好地动画进出。

我已经能够使用下面的代码触发出现/消失的行为,但索引只是弹出和弹出,没有动画,而且我找不到任何方法让一个出现。

func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
if tableView.contentOffset.y > 88 {
return DataManager.sharedManager.frc!.sectionIndexTitles
} else {
return []
}
}

func scrollViewDidScroll(scrollView: UIScrollView) {
tableView.reloadSectionIndexTitles()
}

这基本上意味着每次 ScrollView 滚动时,它都会重新加载部分索引,然后根据表格的偏移量有条件地隐藏或显示索引。正如我所说,它有效,但它不会为索引设置动画,如果可能的话,我真的很喜欢这个功能。

最佳答案

似乎不可能做到这一点,至少苹果没有提供任何 API 来为部分索引 View 设置动画。我能够滑入/滑出索引,但它不会调整单元格的 contentView 的大小。

当您为节索引 View 设置动画时,它会在动画结束后立即返回到其初始位置。所以我基本上是在隐藏/可见时将颜色设置为 clearColor/black。

我不确定苹果是否批准这段代码,因为它使用了未记录的 API

- (UIView *)indexTitlesView {
NSArray *subViews = [self.tableView subviews];
for (UIView *view in subViews) {
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewIndex"]) {
return view;
}
}
return nil;
}

- (void)slideIndexTitlesViewIn {
UIView *indexTitlesView = [self indexTitlesView];

CABasicAnimation *contentPositionAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
contentPositionAnimation.fromValue = @(CGRectGetWidth(indexTitlesView.frame));
contentPositionAnimation.toValue = @(0);
contentPositionAnimation.additive = YES;
contentPositionAnimation.duration = 0.3;
contentPositionAnimation.delegate = self;
contentPositionAnimation.removedOnCompletion = NO;
contentPositionAnimation.fillMode = kCAFillModeForwards;

[indexTitlesView.layer removeAllAnimations];
[indexTitlesView.layer addAnimation:contentPositionAnimation forKey:@"slideInAnimation"];

self.tableView.sectionIndexColor = [UIColor blackColor];
}

- (void)slideIndexTitlesViewOut {
UIView *indexTitlesView = [self indexTitlesView];

CABasicAnimation *contentPositionAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
contentPositionAnimation.fromValue = @(0);
contentPositionAnimation.toValue = @(CGRectGetWidth(indexTitlesView.frame));
contentPositionAnimation.additive = YES;
contentPositionAnimation.duration = 0.3;
contentPositionAnimation.delegate = self;
contentPositionAnimation.removedOnCompletion = NO;
contentPositionAnimation.fillMode = kCAFillModeForwards;

[indexTitlesView.layer removeAllAnimations];
[indexTitlesView.layer addAnimation:contentPositionAnimation forKey:@"slideOutAnimation"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
UIView *indexTitlesView = [self indexTitlesView];
NSArray *keys = [indexTitlesView.layer animationKeys];
for (NSString *key in keys) {
if ([indexTitlesView.layer animationForKey:key] == anim) {
if ([key isEqualToString:@"slideOutAnimation"] && flag == YES) {
self.tableView.sectionIndexColor = [UIColor clearColor];
}
[indexTitlesView.layer removeAllAnimations];
return;
}
}
}

关于ios - 动画 UITableView 索引,像 Apple Music 那样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769665/

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