iOS抖音双击点赞动画,代码如下
//记录时间处理单击双击
@property (nonatomic, assign) NSTimeInterval lastTapTime;
//记录最后的点赞位置
@property (nonatomic, assign) CGPoint lastTapPoint;
//初始化
- (instancetype)init {
self = [super init];
if (self) {
//初始化
self.lastTapTime = 0;
_lastTapPoint = CGPointZero;
//点击手势
UITapGestureRecognizer * singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self addGestureRecognizer:_singleTapGesture];
}
return self;
}
//点击手势
- (void)handleGesture:(UITapGestureRecognizer *)sender {
//获取点击坐标,用于设置爱心显示位置
CGPoint point = [gestureControl.singleTap locationInView:self];
//获取当前时间
NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];
//判断当前点击时间与上次点击时间的时间间隔
if(time - self.lastTapTime > 0.25f) {
//推迟0.25秒执行单击方法
[self performSelector:@selector(singleTapAction) withObject:nil afterDelay:0.25f];
}else {
//取消执行单击方法
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapAction) object: nil];
//执行双击动画
[self showLikeViewAnim:point oldPoint:_lastTapPoint AndSubView:self];
}
//更新上一次点击位置
_lastTapPoint = point;
//更新上一次点击时间
self.lastTapTime = time;
}
//单击手势
-(void)singleTapAction{
}
/**
抖音双击连击爱心动画
@param newPoint 新的坐标位置
@param oldPoint 旧的坐标位置
@param subView 添加到的父视图view
*/
- (void)showLikeViewAnim:(CGPoint)newPoint oldPoint:(CGPoint)oldPoint AndSubView:(UIView *)subView{
UIImageView *likeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_home_like_after"]];//点赞图片
CGFloat k = ((oldPoint.y - newPoint.y)/(oldPoint.x - newPoint.x));
k = fabs(k) < 0.5 ? k : (k > 0 ? 0.5f : -0.5f);
CGFloat angle = M_PI_4 * -k;
likeImageView.frame = CGRectMake(newPoint.x, newPoint.y, 80, 80);
likeImageView.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(angle), 0.8f, 1.8f);
[subView addSubview:likeImageView];
[UIView animateWithDuration:0.2f
delay:0.0f
usingSpringWithDamping:0.5f
initialSpringVelocity:1.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
likeImageView.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(angle), 1.0f, 1.0f);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f
delay:0.5f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
likeImageView.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(angle), 3.0f, 3.0f);
likeImageView.alpha = 0.0f;
}
completion:^(BOOL finished) {
[likeImageView removeFromSuperview];
}];
}];
}
GitHub地址:https://github.com/sshiqiao/douyin-ios-objectc
网友评论