Merge pull request #236 from infinitered/debounce

Debounce feature for RMQ events
This commit is contained in:
Todd Werth
2015-04-07 11:46:42 -07:00
4 changed files with 35 additions and 3 deletions

View File

@@ -98,6 +98,10 @@ class MainController < UIViewController
rmq.append(UIButton, :present_button).on(:touch_up) do
rmq.view_controller.presentModalViewController(PresentedController.new, animated:true)
end
rmq.append(UIButton, :debounce_button).on(:tap, debounce: 1) do
puts "I can only be pressed every second."
end
end
def init_popup_section

View File

@@ -62,7 +62,14 @@ class MainControllerStylesheet < ApplicationStylesheet
button_set_button st
st.background_color = color.purple
st.font = font.small
st.text = 'Present controller'
st.text = 'Present Controller'
end
def debounce_button(st)
button_set_button st
st.background_color = color.battleship_gray
st.font = font.small
st.text = "Debounce Button"
end
def section(st)

View File

@@ -29,6 +29,13 @@ module RubyMotionQuery
end
def handle_gesture_or_event
# Handle debounce logic
if @debounce_length
return if (@debounce_stamp + @debounce_length > Time.now)
# update timestamp
@debounce_stamp = Time.now
end
case @block.arity
when 2
@block.call(@sender, self)
@@ -40,6 +47,11 @@ module RubyMotionQuery
end
def set_options(opts)
if opts[:debounce]
@debounce_length = opts[:debounce]
@debounce_stamp = Time.now
end
if gesture?
@recognizer.tap do |o|
o.cancelsTouchesInView = opts[:cancels_touches_in_view] if opts.include?(:cancels_touches_in_view)
@@ -67,7 +79,7 @@ module RubyMotionQuery
end
end
end
end
end #set_options
def gesture?
@gesture

View File

@@ -65,7 +65,16 @@ describe 'event' do
@control.allTargets.count.should == 0
end
it 'should set various options on a event if it\s a gesture' do
it 'should have the option to debounce events' do
event = RubyMotionQuery::Event.new(@control, :tap, lambda {|sender| ;})
event.gesture?.should == true
event.set_options debounce: 5
event.instance_variable_get('@debounce_length').should == 5
event.instance_variable_get('@debounce_stamp').should.not == nil
end
it 'should set various options on a event if it\'s a gesture' do
event = RubyMotionQuery::Event.new(@control, :tap, lambda {|sender| ;})
event.gesture?.should == true
event.set_options cancels_touches_in_view: true, taps_required: 2, fingers_required: 3