如何给TableView、CollectionView添加动效

时间:2022-11-08 06:20:22
//
// ViewController.m
// tableViewAnimation
//
// Created by 冯敏 on 2018/3/13.
// Copyright © 2018年 FengMin. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * mainTableView;
@property (nonatomic, strong) NSArray * cells;
@end @implementation ViewController - (UITableView *)mainTableView {
if (!_mainTableView) {
_mainTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
}
return _mainTableView;
} - (void)viewDidLoad {
[super viewDidLoad];
self.cells = [NSArray array];
[self.view addSubview:self.mainTableView]; } //- (void)viewWillAppear:(BOOL)animated {
// [super viewWillAppear:animated];
// [self animationTable];
//} - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self animationTable];
} #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0f;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
return cell;
} #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (void)animationTable {
_cells = [_mainTableView.visibleCells copy];
[_mainTableView indexPathsForVisibleRows];
CGFloat tableHeight = _mainTableView.bounds.size.height; CGFloat duration = 0.1; for (UITableViewCell * cell in _cells) {
cell.transform = CGAffineTransformMakeTranslation(, tableHeight);
// [UIView animateWithDuration:3.0f animations:^{
// cell.transform = CGAffineTransformMakeTranslation(0, 0);
// }]; // cell.transform = CGAffineTransformMakeTranslation([UIScreen mainScreen].bounds.size.width, 0);
// [UIView animateWithDuration:duration delay:0 options:0 animations:^{
// cell.transform = CGAffineTransformIdentity;
// } completion:^(BOOL finished) {
//
// }];
duration += 0.1; [UIView animateWithDuration:3.0f delay:duration usingSpringWithDamping:0.8 initialSpringVelocity: options:nil animations:^{
cell.transform = CGAffineTransformMakeTranslation(, );
} completion:^(BOOL finished) { }];
} } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
} - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:0.2];
// cell.transform = CGAffineTransformMakeScale(0.9, 0.9);
// [UIView commitAnimations]; [UIView animateWithDuration:1.0f animations:^{
cell.transform = CGAffineTransformMakeScale(1.2, 1.2);
}];
} - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
cell.transform = CGAffineTransformMakeScale(1.0, 1.0);
[UIView commitAnimations];
} //ios(8.0)
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction * action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// [self.titleArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }]; return @[action];
} //ios(11.0) 头部 左侧按钮自定义、右侧按钮自定义、自定义图片、背景颜色,通过 UIContextualAction 来设置
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction * deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// [self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor]; UISwipeActionsConfiguration * config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
} // 尾部
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction * deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// [self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor]; UISwipeActionsConfiguration * config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end