Sha256: 8410a3a55f88f51a2abe48fdaabafc2b40cade5b3af7f4cf8309113bb9838d2f

Contents?: true

Size: 1.98 KB

Versions: 15

Compression:

Stored size: 1.98 KB

Contents

# Additions to UIControl to support jQuery-style `on` and `off` methods.
class UIControl

  # Add event handlers to UIControls.  See symbol.rb for the uicontrolevent
  # constant aliases.
  #
  # @example
  #   button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
  #   button.on(:touch) { my_code }
  #   button.on(:touchupoutside, :touchcancel) { my_code }
  #   # up to two arguments can be passed in
  #   button.on(:touch) { |sender,touch_event| my_code }
  def on(*events, &block)
    handler = SugarCube::UIControlCallbackHelper.new
    handler.callback = block

    events.each do |event|
      event = event.uicontrolevent unless event.is_a? Fixnum

      sugarcube_callbacks[event].push(handler)
      addTarget(handler, action:'call:event:', forControlEvents:event)
    end

    self
  end

  # Removes all events that were bound with `on`.  See symbol.rb for the
  # uicontrolevent constant aliases.
  #
  # @example
  #   button.off(:touch)
  #   button.off(:touchupoutside, :touchcancel)
  #   button.off  # all events
  def off(*events)
    if events.length == 0
      events = sugarcube_callbacks.keys
    end

    events.each do |event|
      event = event.uicontrolevent unless event.is_a? Fixnum

      sugarcube_callbacks[event].each do |handler|
        self.removeTarget(handler, action:'call:event:', forControlEvents:event)
      end
      sugarcube_callbacks.delete(event)
    end
    self
  end

# private
  # event blocks need to be retained, and the addTarget method explicitly does
  # *not* retain `target`.  This makes sure that callbacks are retained by
  # pushing the block onto a stack.
  def sugarcube_callbacks
    @sugarcube_callbacks ||= Hash.new { |hash, key| hash[key] = [] }
  end

end


module SugarCube
  class UIControlCallbackHelper
    attr_accessor :callback

    def call(sender, event:event)
      case callback.arity
      when 0
        callback.call
      when 1
        callback.call(sender)
      else
        callback.call(sender, event)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sugarcube-0.20.12 lib/sugarcube/uicontrol.rb
sugarcube-0.20.11 lib/sugarcube/uicontrol.rb
sugarcube-0.20.10 lib/sugarcube/uicontrol.rb
sugarcube-0.20.9 lib/sugarcube/uicontrol.rb
sugarcube-0.20.8 lib/sugarcube/uicontrol.rb
sugarcube-0.20.7 lib/sugarcube/uicontrol.rb
sugarcube-0.20.6 lib/sugarcube/uicontrol.rb
sugarcube-0.20.5 lib/sugarcube/uicontrol.rb
sugarcube-0.20.4 lib/sugarcube/uicontrol.rb
sugarcube-0.20.3 lib/sugarcube/uicontrol.rb
sugarcube-0.20.1 lib/sugarcube/uicontrol.rb
sugarcube-0.20.0 lib/sugarcube/uicontrol.rb
sugarcube-0.19.5 lib/sugarcube/uicontrol.rb
sugarcube-0.19.4 lib/sugarcube/uicontrol.rb
sugarcube-0.19.2 lib/sugarcube/uicontrol.rb