将反弹效果添加到UIImageView的外观中

时间:2011-10-16 21:35:18

标签: iphone objective-c ipad core-animation

当我要将UIImageView显示为子视图时,如何添加反弹效果?我必须使用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;

5 个答案:

答案 0 :(得分:29)

使用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;

答案 1 :(得分:7)

Swift 中的Bouncy(扩展/缩小)动画:

var selected: Bool {
  willSet(selected) {
    let expandTransform:CGAffineTransform = CGAffineTransformMakeScale(1.2, 1.2);
    if (!self.selected && selected) {
      self.imageView.image = SNStockCellSelectionAccessoryViewImage(selected)
      self.imageView.transform = expandTransform
      UIView.animateWithDuration(0.4,
        delay:0.0,
        usingSpringWithDamping:0.40,
        initialSpringVelocity:0.2,
        options: .CurveEaseOut,
        animations: {
          self.imageView.transform = CGAffineTransformInvert(expandTransform)
        }, completion: {
          //Code to run after animating
          (value: Bool) in
      })

    }
  }
}

var imageView:UIImageView

如果imageView作为子视图正确添加到视图中,则在selected = falseselected = true之间切换应该使用有弹性的动画交换图像。 SNStockCellSelectionAccessoryViewImage只根据当前选择状态返回不同的图像,如下所示:

private let SNStockCellSelectionAccessoryViewPlusIconSelected:UIImage = UIImage(named:"PlusIconSelected")!
private let SNStockCellSelectionAccessoryViewPlusIcon:UIImage = UIImage(named:"PlusIcon")!

private func SNStockCellSelectionAccessoryViewImage(selected:Bool) -> UIImage {
  return selected ? SNStockCellSelectionAccessoryViewPlusIconSelected : SNStockCellSelectionAccessoryViewPlusIcon
}

下面的GIF示例有点慢,实际动画发生得更快:

UIImageView bounce animation Gif

答案 2 :(得分:3)

enter image description here

[self.navigationController popViewControllerAnimated:YES];

使用这些值来获得不同的效果。

答案 3 :(得分:0)

@ Jano在Swift中的回答

let origin:CGPoint = self.image.center
let target:CGPoint = CGPointMake(self.image.center.x, self.image.center.y+100)
let bounce = CABasicAnimation(keyPath: "position.y")
bounce.duration = 1
bounce.fromValue = origin.y
bounce.toValue = target.y
bounce.repeatCount = 2
bounce.autoreverses = true
self.image.layer.addAnimation(bounce, forKey: "position")

答案 4 :(得分:0)

我关注了这个link

func imageViewBounceEffect() {

    yourImageView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    
    UIView.animate(withDuration: 1.35, delay: 0,
                               usingSpringWithDamping: 0.25,
                               initialSpringVelocity: 5,
                               options: .curveEaseOut,
                               animations: {
                                
        self.yourImageView.transform = .identity
    })
}