iphone - 动画化 UITableView 部分标题的自定义 View

标签 iphone ios objective-c uitableview

在我的应用程序中,我在一个动态改变宽度的场景中有一个 UITableView。当宽度改变时,我为场景的扩展设置动画 - 包括 UITableView

UITableView 包含带有自定义 View 的节标题,其中包含锚定到 View 右侧的 UILabel

当我的场景宽度发生变化时,我会像这样调整表格的大小:

[UIView animateWithDuration:0.22
                      delay:0.02
                    options:UIViewAnimationOptionCurveLinear
                 animations:^
 {
     //Change width to 320 from 220
     [menuTable setFrame:CGRectMake(menuTable.frame.origin.x, menuTable.frame.origin.y, 320, menuTable.frame.size.height)];
 }
                 completion:^(BOOL finished)
 {

 }];

这会平滑调整表格大小的动画,但节标题内的标签会弹出到最终目的地 - 它不会动画。

我试过调用 reloadData - 这具有相同的效果,没有动画。我尝试调用 beginUpdatesendUpdates 但没有任何效果。

我还应该尝试什么?

最佳答案

在我的标签扩展实验中,我发现 Ryan Poolos 在他的评论中所说的是正确的——即使标签用动画扩展,文本也会跳到它的新位置(我已经用文本做到了这一点设置为居中对齐)。

所以,我发现唯一可行的方法是使用计时器,如下所示:

-(IBAction)expandLabel:(id)sender {

    [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(expand:) userInfo:nil repeats:YES];
}

-(void)expand:(NSTimer *) aTimer {
    self.widthCon.constant += 1;
    if (self.widthCon.constant >= 200) [aTimer invalidate];
}

widthCon 是我放在标签上的宽度约束的 IBOutlet。如果您正在使用自动布局,则应该通过更改约束而不是更改框架来制作任何动画。我没有在节标题中使用标签尝试过此操作,但我认为如果您使用此方法为表格的宽度设置动画,并且标签的约束按照表格扩展的方式设置,它会起作用。

编辑后:

经过更多的实验,似乎计时器方法是使其也适用于移动标签(而不是展开标签)的唯一方法。在那种情况下,宽度约束将是表格 View 本身。

第二次编辑后:

更多实验。我发现问题出在调用 layoutSubviews(你不应该直接调用)而不是 layoutIfNeeded。因此,您可以在没有计时器的情况下使标签位置具有动画效果。这是我用来制作标题并在单击按钮时进行扩展的代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *view = [[UITableViewHeaderFooterView alloc] init];
    view.tintColor = [UIColor yellowColor];
    UILabel *label = [[UILabel alloc] init];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    label.backgroundColor = [UIColor redColor];
    label.text = @"Test";
    [view.contentView addSubview:label];
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[label]-|" options:0 metrics:nil views:@{@"label":label}]];
    [view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    return view;
}

-(IBAction)expandTable:(id)sender {
    self.widthCon.constant = 300;
    [UIView animateWithDuration:1 animations:^{
        [self.view layoutIfNeeded];
    }];
}

关于iphone - 动画化 UITableView 部分标题的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16568631/

相关文章:

ios - 如何将方法调用延迟 1 秒?

ios - 如何在uicollectionview中获取当前的cell.contentview。如何在 uicollectionview 单元格中添加 subview

iphone - 即使没有与 UIViewController 关联的 Nib ,也会调用 _loadViewFromNibNamed

iphone - 使用selector方法调用C Function并在iPhone的c方法中访问self

ios - UIPageControl 似乎并不总是在滚动时发生变化

iphone - iOS 乐谱和播放的最佳 API

iphone - iOS 应用的兼容设备列表

iphone - 当我没有互联网连接并通过 Appstore 测试时,我可以在 IOS 上使用 Exit(0) 功能吗?

ios - iPhone - SQLite 数据库读取

objective-c - iOS中获取AVPlayer的音量