当我要将 UIImageView 作为 subview 显示时,如何添加弹跳效果?我是否必须使用 CoreAnimation 来执行此操作?我现在唯一的猜测是使用 CAKeyframeAnimation,如果有更好的方法请告诉我。这是我当前的代码:
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 轴动画:
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,例如:
// grow
CGAffineTransform transform = CGAffineTransformMakeScale(1.3, 1.3);
imageView.transform = transform;
关于iphone - 为 UIImageView 的外观添加弹跳效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787494/