Sha256: ce854f0e54b4c4714732287e8c85b99f717e1d906701c53bc8b989a3fc8db2a0

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

#
# Special, UI avare events API
#
module UnderOs::UI::Events

  def on(event, *args, &block)
    return event.map{|e,b| self.on(e,&b)}[0] || self if event.is_a?(Hash)

    event, recognizer = find_recognizer_from(event)

    @_.addGestureRecognizer(recognizer.alloc.initWithTarget(self, action: :emit)) if recognizer
    @_.userInteractionEnabled = true

    UnderOs::Events::Listeners.add(self, event, *args, block)
  end

  def off(event)
    UnderOs::Events::Listeners.remove(self, event)
  end

  def emit(*event)
    if event.is_a?(UIGestureRecognizer)
      event = Event.new(self, event)
    else
      event = Event.new(self, *event)
    end

    UnderOs::Events::Listeners.kick(self, event, {})
  end

  def on=(hash)
    on hash
  end

private

  RECOGNIZERS = {
    tap:    UITapGestureRecognizer,
    pinch:  UIPinchGestureRecognizer,
    rotate: UIRotationGestureRecognizer,
    swipe:  UISwipeGestureRecognizer,
    pan:    UIPanGestureRecognizer,
    press:  UILongPressGestureRecognizer
  }

  # tries to figure event name and gesture recognizer
  def find_recognizer_from(event)
    event = event.to_sym if event.is_a?(String)

    if event.is_a?(Class) && event < UIGestureRecognizer
      recognizer = event

      if recognizer.respond_to?(:event_name)
        event = recognizer.event_name
      else
        RECOGNIZERS.each{ |e,r| event = e if r == recognizer }
      end

    elsif RECOGNIZERS[event]
      recognizer = RECOGNIZERS[event]
    end

    [event, recognizer]
  end

  class Event < UnderOs::Events::Event
    attr_reader :target

    def initialize(view, event, params={})
      @target = view
      event, r = view.__send__ :find_recognizer_from, event.class if event.is_a?(UIGestureRecognizer)
      super event, params
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
under-os-1.2.1 lib/under_os/ui/utils/events.rb
under-os-1.2.0 lib/under_os/ui/utils/events.rb
under-os-1.1.0 lib/under_os/ui/utils/events.rb
under-os-1.0.0 lib/under_os/ui/utils/events.rb