mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-05-01 06:02:28 +08:00
Merge pull request #236 from infinitered/debounce
Debounce feature for RMQ events
This commit is contained in:
@@ -98,6 +98,10 @@ class MainController < UIViewController
|
|||||||
rmq.append(UIButton, :present_button).on(:touch_up) do
|
rmq.append(UIButton, :present_button).on(:touch_up) do
|
||||||
rmq.view_controller.presentModalViewController(PresentedController.new, animated:true)
|
rmq.view_controller.presentModalViewController(PresentedController.new, animated:true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
rmq.append(UIButton, :debounce_button).on(:tap, debounce: 1) do
|
||||||
|
puts "I can only be pressed every second."
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def init_popup_section
|
def init_popup_section
|
||||||
|
|||||||
@@ -62,7 +62,14 @@ class MainControllerStylesheet < ApplicationStylesheet
|
|||||||
button_set_button st
|
button_set_button st
|
||||||
st.background_color = color.purple
|
st.background_color = color.purple
|
||||||
st.font = font.small
|
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
|
end
|
||||||
|
|
||||||
def section(st)
|
def section(st)
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ module RubyMotionQuery
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_gesture_or_event
|
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
|
case @block.arity
|
||||||
when 2
|
when 2
|
||||||
@block.call(@sender, self)
|
@block.call(@sender, self)
|
||||||
@@ -40,6 +47,11 @@ module RubyMotionQuery
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set_options(opts)
|
def set_options(opts)
|
||||||
|
if opts[:debounce]
|
||||||
|
@debounce_length = opts[:debounce]
|
||||||
|
@debounce_stamp = Time.now
|
||||||
|
end
|
||||||
|
|
||||||
if gesture?
|
if gesture?
|
||||||
@recognizer.tap do |o|
|
@recognizer.tap do |o|
|
||||||
o.cancelsTouchesInView = opts[:cancels_touches_in_view] if opts.include?(:cancels_touches_in_view)
|
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
|
end
|
||||||
end
|
end #set_options
|
||||||
|
|
||||||
def gesture?
|
def gesture?
|
||||||
@gesture
|
@gesture
|
||||||
|
|||||||
@@ -65,7 +65,16 @@ describe 'event' do
|
|||||||
@control.allTargets.count.should == 0
|
@control.allTargets.count.should == 0
|
||||||
end
|
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 = RubyMotionQuery::Event.new(@control, :tap, lambda {|sender| ;})
|
||||||
event.gesture?.should == true
|
event.gesture?.should == true
|
||||||
event.set_options cancels_touches_in_view: true, taps_required: 2, fingers_required: 3
|
event.set_options cancels_touches_in_view: true, taps_required: 2, fingers_required: 3
|
||||||
|
|||||||
Reference in New Issue
Block a user