【问题标题】:How to create a "Secret"like table view animation?如何创建类似“秘密”的表格视图动画?
【发布时间】:2014-11-17 23:25:25
【问题描述】:

当您单击设置按钮时,我想获得类似秘密的表格视图动画:

当然在现实生活中它更平滑..在 gif 中也有点笨拙,细胞从底部出来并弹到顶部......

这是我的表格视图控制器类:

#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
#import "CoreDataStack.h"

@interface StackTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultController;

@end

@implementation StackTableViewController

- (id)init {

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) {
        // Do something
        [self.fetchedResultController performFetch:nil];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
        self.currentTarget = current.body;
    }
    return self;
}


- (void)viewDidLoad {

    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
    self.currentTarget = current.body;
}


- (void)back {

}


- (void)viewWillLayoutSubviews {

    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated {
       NSLog(@"did go back");

    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

// just to ignor a warning
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return self.fetchedResultController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultController sections][section];
    return [sectionInfo numberOfObjects];
}




- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
    CoreDataStack *stack = [CoreDataStack defaultStack];
    [[stack managedObjectContext] deleteObject:target];
    [stack saveContext];

    NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];

    Target *newTarget = [self.fetchedResultController objectAtIndexPath:path];

    self.currentTarget = newTarget.body;

    if ([_delegate respondsToSelector:@selector(didDeleteObject)]) {
        [_delegate didDeleteObject];
    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *cellIdentifier = @"StackTableViewCell";

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];

    StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    cell.cellLabel.text = target.body;

    cell.cellLabel.font = [UIFont fontWithName:@"Candara-Bold" size:20];

    // Configure the cell...

    return cell;
}


- (NSFetchRequest *)targetsFetchRequest {

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Target"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    return fetchRequest;
}


- (NSFetchedResultsController *)fetchedResultController {

    if (_fetchedResultController != nil) {
        return _fetchedResultController;
    }

    CoreDataStack *stack = [CoreDataStack defaultStack];

    NSFetchRequest *fetchRequest = [self targetsFetchRequest];

    _fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    _fetchedResultController.delegate = self;

    return _fetchedResultController;

}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];

        default:
            break;
    }
}


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        default:
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

    [self.tableView endUpdates];
}

【问题讨论】:

  • 您将不得不在willDisplayRowAtIndexPath 方法中添加自定义动画。除此之外,这将是一个复杂的算法和大量的工作(超出 SO 答案的范围)才能让它像那样动画。
  • 这基本上是对您在消息中看到的弹性滚动气泡的相同技术的改编。 Apple 在 WWDC 2013 的 Scroll Views 会议中展示了如何做到这一点。
  • @rickster 能否附上该会话的链接。
  • 这里还有一些关于集合视图动画的有用信息:objc.io/issue-5/collection-views-and-uidynamics.html
  • @rickster 谢谢。您所说的会话链接是什么意思?

标签: ios objective-c uitableview animation core-animation


【解决方案1】:

在 Swift 中动画 UITableViewCells

也许这样的事情会有所帮助?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.reloadData()
    animateRows()
}

func animateRows() {
    let cells = tableView.visibleCells
    
    for cell in cells {
        cell.transform = CGAffineTransform(translationX: 0, y: tableView.frame.height)
    }
    
    var delay = 0.0
    for cell in cells {
        UIView.animate(withDuration: 1, delay: delay, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
            cell.transform = .identity
        })
        delay += 0.05
    }
}

这是实际效果

【讨论】:

    【解决方案2】:

    您需要在 tableView 的“更新块”内对模型进行更改。在这个更新块的开始和结束时,模型必须与表同步,否则你会得到一个 NSInternalInconsistencyException(我的名字可能有点错误..)并且你的构建会崩溃。

    因此,在实践中,您需要一个实例变量来存储正在发生的事情,并且您在更新块内更改此变量并指示表格插入/删除单元格和/或部分以反映这些更改。请参考我对这个最近的问题的回答,它是一个完整的 UIViewController 子类,但请查看三个 tableView #pragma 标记中的方法以了解它是如何工作的。祝你好运。
    Expand and Collapse All Table View Cells

    【讨论】:

    • 这是基本的独创性、坚持和对内部情况的了解。在我看来,你的答案是完美的。
    【解决方案3】:

    如果你能保证所有可见的东西都在那里,表格外没有单元格,你可以对 cellForRow:atIndexPath:loadTime 应用动画。但是你不能在没有 Apple 声明的情况下为表格外的单元格设置动画。我会使用这种方法,成本最低,然后为动画完成设置一个处理程序,以便您可以重新调整表格的大小。

    否则你将不得不做那行动画的东西。

    【讨论】:

    • 表格外的单元格是什么意思?像这样你需要滚动才能看到它们?
    • 您不能将单元格动画化进/出它们各自的数据模型 viewForIndex 的东西,它会崩溃。我已经做到了。但是当表格被加载时,所有单元格都可以从随机区域进行动画处理。我在某个应用程序中为某个控制台使用了这个技巧,不幸的是我不能说。只知道它被使用过。我很确定我们都被同一个人否决了,那个人一定认为我错了。在我开发应用程序之前,我个人认为这是可能的
    • 我不在乎投反对票的仇恨者。问题是我很想根据我的代码获得实用的细节(代码),这就是为什么我提供 50 便士的赏金......谢谢兄弟
    • 只是基本的动画。我阅读了核心动画指南,你会想要使用图层而不是视图动画。您需要的一切都在其中。
    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多