#pragma mark 点击开始触发点击事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
#pragma mark 获取现在的点击位置
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"x ===== %f y ===== %f",touchPoint.x,touchPoint.y);
[self beginAnimationWithX:touchPoint.x withY:touchPoint.y];
}
#pragma mark 点击的一瞬间创建图片,对图片进行动画处理
- (void)beginAnimationWithX:(CGFloat)x withY:(CGFloat)y {
#pragma mark 创建图片
UIImageView *iconLove = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 26, 22)];
[iconLove setImage:[UIImage imageNamed:@"player_icon_yodou"]];
[self.view addSubview:iconLove];
CGFloat topY = 0 - CGRectGetMinY(iconLove.frame) - 40;
float moveX = (arc4random() % 10) + 14;
if ((arc4random() % 2) == 0) {
moveX *= -1;
}
float repertMoveX = moveX * -2;
float scaleFrom = 0.3f;
float scaleDurTimes = 0.2f;
float beginTime = 0.3f;
float moveXDurTimes = (arc4random() % 2) + 2;
float repMoveXDurTime = (arc4random() % 5) / 10.0f + 4.0f;
float opacityTimes = moveXDurTimes * 0.8;
[iconLove.layer addAnimation:[self scaleFrom:[NSNumber numberWithFloat:scaleFrom] durTimes:scaleDurTimes] forKey:nil];
[iconLove.layer addAnimation:[self moveX:[NSNumber numberWithFloat:moveX] durTimes:0.5f beginTime:beginTime] forKey:nil];
[iconLove.layer addAnimation:[self repertMoveX:[NSNumber numberWithFloat:repertMoveX] durTimes:repMoveXDurTime beginTime:beginTime] forKey:nil];
[iconLove.layer addAnimation:[self moveY:[NSNumber numberWithFloat:topY] durTimes:moveXDurTimes beginTime:0] forKey:nil];
[iconLove.layer addAnimation:[self opacity:opacityTimes beginTime:beginTime] forKey:nil];
[self performSelector:@selector(removeLove:) withObject:iconLove afterDelay:7.0f];
}
#pragma mark 改变图片的尺寸
- (CABasicAnimation *)scaleFrom:(NSNumber *)fromValue durTimes:(float)time
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue = fromValue;
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.duration = time;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
return animation;
}
#pragma mark x方向的动画设置
- (CABasicAnimation *)moveX:(NSNumber *)x durTimes:(float)time beginTime:(float)beginTime
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animation.byValue = x;
animation.duration = time;
animation.beginTime = CACurrentMediaTime() + 0.3;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
return animation;
}
- (CABasicAnimation *)repertMoveX:(NSNumber *)x durTimes:(float)time beginTime:(float)beginTime
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animation.byValue = x;
animation.duration = time;
animation.beginTime = CACurrentMediaTime() + beginTime;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.autoreverses = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
#pragma mark y方向的动画设置
- (CABasicAnimation *)moveY:(NSNumber *)y durTimes:(float)time beginTime:(float)beginTime
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.toValue = y;
animation.duration = time;
animation.beginTime = CACurrentMediaTime() + beginTime;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
return animation;
}
- (CABasicAnimation *)opacity:(float)time beginTime:(float)beginTime
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];
animation.duration = time;
animation.beginTime = CACurrentMediaTime() + beginTime;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
return animation;
}
#pragma mark 根据动画时间,移除
- (void)removeLove:(UIImageView *)iconLove {
[iconLove removeFromSuperview];
}