【问题标题】:adding bounce effect to appearance of UIImageView为 UIImageView 的外观添加反弹效果
【发布时间】:2011-10-16 21:35:17
【问题描述】:

当我将 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;

【问题讨论】:

  • 嗨阿迪特。你能详细说明一下吗? “反弹效应”到底是什么意思?你有什么例子可以展示吗?
  • 我的意思是,当它显示时,我希望它上下反弹几次。类似entheosweb.com/Flash/bouncing_effect.asp

标签: iphone objective-c ipad core-animation


【解决方案1】:

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

【讨论】:

  • 您实际上在哪里将 imageView 设置为 UIView 动画?你不能用 CABasicAnimation 吗?
  • 您的弹跳动画不起作用。因为视图最终会处于新位置,而不是回到旧位置..这根本不是反弹..
  • @Philip007 CABasicAnimation 使图像下降,然后上升到其原始位置,如上面作为评论发布的 Flash 示例。
  • 效果很好!! :) 确保在项目中添加 QuatzCore 框架。谢谢。
  • 不错的简单解决方案
【解决方案2】:

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

如果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 例子有点慢,实际的动画发生得更快:

                                     

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      @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")
      

      【讨论】:

        【解决方案5】:

        我关注了这个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
            })
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-16
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 2017-04-09
          • 1970-01-01
          相关资源
          最近更新 更多