将反弹效果添加到UIImageView的外观中 [英] adding bounce effect to appearance of UIImageView

查看:94
本文介绍了将反弹效果添加到UIImageView的外观中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要将UIImageView显示为子视图时,如何添加反弹效果?我必须使用CoreAnimation来做到这一点吗?我现在唯一的猜测是使用CAKeyframeAnimation,如果有更好的方法,请告诉我。这是我目前的代码:

How can I add a bounce effect when I am about to show an UIImageView as a subview? Do I have to use CoreAnimation to do this? My only guess right now is to use CAKeyframeAnimation, please let me know if there's a better way. Here's my current code:

 CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    theAnimation.delegate = self;
    theAnimation.duration = 1.0;
    theAnimation.fromValue = [NSNumber numberWithFloat:notif.center.y];
    theAnimation.toValue = [NSNumber numberWithFloat:notif.center.y-20];
    theAnimation.repeatCount = 3;


推荐答案

使用CABasicAnimation的y轴动画:

y-axis animation using CABasicAnimation:

CGPoint origin = self.imageView.center;
CGPoint target = CGPointMake(self.imageView.center.x, self.imageView.center.y+100);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce.duration = 0.5;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 2;
bounce.autoreverses = YES;
[self.imageView.layer addAnimation:bounce forKey:@"position"];

如果你想实现收缩和增长,你必须添加一个CGAffineTransformMakeScale,例如:

If you want to implement shrink and grow you have to add a CGAffineTransformMakeScale, eg:

// grow
CGAffineTransform transform = CGAffineTransformMakeScale(1.3, 1.3);
imageView.transform = transform;

这篇关于将反弹效果添加到UIImageView的外观中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆