解决点击cell执行动画导致的重用问题

时间:2023-03-09 08:09:50
解决点击cell执行动画导致的重用问题

解决点击cell执行动画导致的重用问题

解决点击cell执行动画导致的重用问题

说明:

动画的细节都是裸露的,并没有封装,靠看官来优化了。

效果:

解决点击cell执行动画导致的重用问题

源码:

https://github.com/YouXianMing/UITableViewSelectedAnimation

核心:

//
// YouXianMingCell.h
// SelectedAnimation
//
// Created by YouXianMing on 15/4/17.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface YouXianMingCell : UITableViewCell @property (nonatomic, strong) UILabel *name; - (void)showIconAnimated:(BOOL)animated;
- (void)hideIconAnimated:(BOOL)animated; - (void)showSelectedAnimation; @end
//
// YouXianMingCell.m
// SelectedAnimation
//
// Created by YouXianMing on 15/4/17.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "YouXianMingCell.h" @interface YouXianMingCell () @property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *rectView; @end @implementation YouXianMingCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) { _rectView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
_rectView.layer.borderWidth = .f;
_rectView.layer.borderColor = [UIColor grayColor].CGColor;
[self addSubview:_rectView]; // 图标
_iconView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
_iconView.image = [UIImage imageNamed:@"icon"];
_iconView.alpha = .f;
[self addSubview:_iconView]; // 文字
_name = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_name.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:];
_name.textColor = [UIColor grayColor];
[self addSubview:_name]; _lineView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
_lineView.alpha = .f;
_lineView.backgroundColor = [UIColor redColor];
[self addSubview:_lineView];
} return self;
} - (void)showIconAnimated:(BOOL)animated {
if (animated) {
_iconView.transform = CGAffineTransformMake(, , , , , ); [UIView animateWithDuration:0.5
delay:
usingSpringWithDamping:
initialSpringVelocity:
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_iconView.alpha = .f;
_iconView.transform = CGAffineTransformMake(, , , , , ); _lineView.alpha = .f;
_lineView.frame = CGRectMake(, , , ); _name.frame = CGRectMake( + , , , ); _rectView.layer.borderColor = [UIColor redColor].CGColor;
_rectView.transform = CGAffineTransformMake(0.8, , , 0.8, , );
_rectView.layer.cornerRadius = .f;
}
completion:^(BOOL finished) { }];
} else {
_iconView.transform = CGAffineTransformMake(, , , , , );
_iconView.alpha = .f; _lineView.alpha = .f;
_lineView.frame = CGRectMake(, , , ); _name.frame = CGRectMake( + , , , ); _rectView.layer.borderColor = [UIColor redColor].CGColor;
_rectView.transform = CGAffineTransformMake(0.8, , , 0.8, , );
_rectView.layer.cornerRadius = .f;
}
} - (void)hideIconAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:0.5
delay:
usingSpringWithDamping:
initialSpringVelocity:
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_iconView.alpha = .f;
_iconView.transform = CGAffineTransformMake(0.5, , , 0.5, , ); _lineView.alpha = .f;
_lineView.frame = CGRectMake(, , , ); _name.frame = CGRectMake(, , , ); _rectView.layer.borderColor = [UIColor grayColor].CGColor;
_rectView.transform = CGAffineTransformMake(, , , , , );
_rectView.layer.cornerRadius = ;
}
completion:^(BOOL finished) { }];
} else {
_iconView.alpha = .f; _lineView.alpha = .f;
_lineView.frame = CGRectMake(, , , ); _name.frame = CGRectMake(, , , ); _rectView.layer.borderColor = [UIColor grayColor].CGColor;
_rectView.transform = CGAffineTransformMake(, , , , , );
_rectView.layer.cornerRadius = ;
}
} - (void)showSelectedAnimation {
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
tmpView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.30];
tmpView.alpha = .f; [self addSubview:tmpView]; [UIView animateWithDuration:0.20 delay: options:UIViewAnimationOptionCurveEaseIn animations:^{
tmpView.alpha = 0.8f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.20 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
tmpView.alpha = .f;
} completion:^(BOOL finished) {
[tmpView removeFromSuperview];
}];
}];
} @end

细节:

解决点击cell执行动画导致的重用问题