Ios 使用CAKeyFrameAnimation(或其他什么?)使用RubyMotion使图像反弹

Ios 使用CAKeyFrameAnimation(或其他什么?)使用RubyMotion使图像反弹,ios,core-animation,cakeyframeanimation,rubymotion,Ios,Core Animation,Cakeyframeanimation,Rubymotion,我不熟悉核心动画,也不熟悉RubyMotion(从一月份开始就在Xcode中使用Obj-C)。我需要有AppLabel(它的png在名为AppAppearance.rb的文件中指定,但它加载的所有动画都在这个文件中)。现在我使用的是animateWithDuration,但当标签加载时,我需要向左反弹一点。任何可能的帮助都将不胜感激,我整天都在兜圈子。我试图使用本文中的代码:以及我在CAKeyFrameAnimation上能找到的任何东西,但我一直在将Obj-C转换为Ruby。谢谢 class

我不熟悉核心动画,也不熟悉RubyMotion(从一月份开始就在Xcode中使用Obj-C)。我需要有AppLabel(它的png在名为AppAppearance.rb的文件中指定,但它加载的所有动画都在这个文件中)。现在我使用的是animateWithDuration,但当标签加载时,我需要向左反弹一点。任何可能的帮助都将不胜感激,我整天都在兜圈子。我试图使用本文中的代码:以及我在CAKeyFrameAnimation上能找到的任何东西,但我一直在将Obj-C转换为Ruby。谢谢

class AppLabel < UILabel
#-------------------------


  DefaultHeight =                 45
  DefaultWidth =                  170               


  def initWithFrame( frame )
  #-------------------------

    if ( super( frame ) )


      @showing = false
      @hiding = false


      self.size.width = 170 if self.size.width == 0
      self.size.height = 46 if self.size.height == 0

      self.backgroundColor = AppAppearance.appLabelBackgroundColor
      self.font = AppAppearance.fontWithSize( 14 )
      self.textColor = AppAppearance.appLabelTextColor      

      self.numberOfLines = 2


    end

    self

  end


  #
  # method. drawTextInRect
  # 
  def drawTextInRect( rect )
  #-------------------------


    rect.origin.x += 10
    rect.origin.y += 2    
    rect.size.width -= 30

    super( rect )

  end

  #
  # method. show
  #
  def show
  #-------

    if ( ( self.hidden? || self.alpha < 1 ) && !@showing )

      if self.hidden?

          self.alpha = 0.0
          self.hidden = false

      end

      @showing = true

      UIView.animateWithDuration(
         1.0,
         animations: lambda do
           self.alpha = 1.0
         end,
         completion: lambda do | finished |
           @showing = false
         end
      )          

    end

  end

  #
  # method. hide
  #
  def hide
  #-------

    unless ( self.hidden? || self.alpha == 0 || @hiding )

      log( 'hiding' )

      @hiding = true

      UIView.animateWithDuration(
        1.0,
        animations: lambda do
          self.alpha = 0.0
        end,
        completion: lambda do | finished |
          self.hidden = true
          @hiding = false
        end
      )          

    end

  end

end
class AppLabel
尝试类似的方法,过程可能有点过火,但它可以工作!时间安排还远远不够完美,但我相信你能做到;)

#视图:要设置动画的视图
#属性:
#次数:视图应反弹多少次
#偏移:到最远(第一次)反弹左侧的距离
#更改:每次反弹的偏移量减少多少
#持续时间:最远(第一次)反弹的持续时间
def反弹(视图,属性={次:4,偏移量:20,更改:5,持续时间:0.25})
#要为视图设置动画的点
@点数=[]
属性[:次]。次做| n|
new_x=view.position.x-(attrs[:offset]-n*attrs[:change])
#向左移动点,然后返回到原始位置

@点试试这样的方法,这个过程可能有点过分,但它是有效的!时间安排还远远不够完美,但我相信你能做到;)

#视图:要设置动画的视图
#属性:
#次数:视图应反弹多少次
#偏移:到最远(第一次)反弹左侧的距离
#更改:每次反弹的偏移量减少多少
#持续时间:最远(第一次)反弹的持续时间
def反弹(视图,属性={次:4,偏移量:20,更改:5,持续时间:0.25})
#要为视图设置动画的点
@点数=[]
属性[:次]。次做| n|
new_x=view.position.x-(attrs[:offset]-n*attrs[:change])
#向左移动点,然后返回到原始位置
@要点
# view: The view to animate
# attrs:
#   times:    How many times the view should bounce
#   offset:   The distance to the left of the furthest (first) bounce
#   change:   How much to decrement the offset each bounce
#   duration: The duration of the furthest (first) bounce
def bounce(view, attrs={times: 4, offset: 20, change: 5, duration: 0.25})
  # The points to animate the view to
  @points = []

  attrs[:times].times do |n|
    new_x = view.position.x - (attrs[:offset] - n * attrs[:change])
    # Move the point left, then back to the original position
    @points << [new_x, view.position.y]
    @points << view.position
  end

  # An array to hold the blocks containing the animations
  @animations = [];

  # Needs an animation to move away then back
  (attrs[:times] * 2).times do |n|
    new_dur = (Float(attrs[:change]) / Float(attrs[:offset])) * (attrs[:times] * 2 - n) * attrs[:duration]
    # Add a new movement to the animations array
    @animations << Proc.new {
      UIView.animateWithDuration(new_dur,
        delay: 0,
        #Ease out when moving away and in when moving back
        options: (n % 2 == 0 ? UIViewAnimationCurveEaseOut : UIViewAnimationCurveEaseIn),
        animations: lambda {
          # Animate the view to the position at the start of the points array
          view.position = @points.first
        },
        completion: lambda { |completed|
          # Remove the first point and first animation from their corresponding
          # arrays then run the next animation until there are none left
          @animations.shift
          @points.shift
          @animations.first.call if @animations.length != 0
        })
    }
  end
  # Initiate the chain of animations
  @animations.first.call
end