adding bounce effect to appearance of UIImageView

2019-03-08 08:41发布

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;

4条回答
神经病院院长
2楼-- · 2019-03-08 09:15

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"];

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;
查看更多
男人必须洒脱
3楼-- · 2019-03-08 09:15

@Jano's answer in 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")
查看更多
ら.Afraid
4楼-- · 2019-03-08 09:22

enter image description here

[UIView animateWithDuration:0.8
                      delay:0
     usingSpringWithDamping:0.5
      initialSpringVelocity:0.5
                    options:(UIViewAnimationOptionAutoreverse|
                             UIViewAnimationOptionRepeat)
                 animations:^{
                     CGRect frame = view.frame;
                     frame.origin.y -= 8;
                     view.frame = frame;
                 } completion:nil];

Play with the values to get different effects.

查看更多
We Are One
5楼-- · 2019-03-08 09:27

Bouncy (expand/shrink) animation in Swift:

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

If imageView is correctly added to the view as a subview, toggling between selected = false to selected = true should swap the image with a bouncy animation. SNStockCellSelectionAccessoryViewImage just returns a different image based on the current selection state, see below:

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

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

The GIF example below is a bit slowed down, the actual animation happens faster:

                                       UIImageView bounce animation Gif

查看更多
登录 后发表回答