zoukankan      html  css  js  c++  java
  • 如何给TableView、CollectionView添加动效

    //
    //  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 5;
    }
    
    - (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(0, 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:0 options:nil animations:^{
                cell.transform = CGAffineTransformMakeTranslation(0, 0);
            } 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
  • 相关阅读:
    751时尚公开课(三)宋杰林:时尚大跃进_豆瓣
    第九课堂-经验与技能分享交易网站
    751D·PARK北京时尚设计广场_百度百科
    2014马上有乐趣 每周六免费服装缝纫体验课_豆瓣
    设计工作室寻求合作
    【基础穿搭法】关于穿衣显高和好看的三个小技巧。(请深爱)
    给快播指一条生路:转型会员付费吧
    美华美依 | 创业谱
    服装配饰_MAVIN MARVY 高级服装定制_西服定制_衬衫定制_西装定制
    探索者系列_百度百科
  • 原文地址:https://www.cnblogs.com/fengmin/p/8567107.html
Copyright © 2011-2022 走看看