Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for UIScreenEdgePanGestureRecognizer via existing on_pan helper #211

Merged
merged 7 commits into from Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -440,6 +440,9 @@ view.on_pan(2) # minimum and maximum fingers required
view.on_pan(fingers: 2)
view.on_pan(min_fingers: 2, max_fingers: 3)

# If present, overrides fingers options and instead handles gestures originating at specified screen edges (UIScreenEdgePanGestureRecognizer)
view.on_pan(edges: [<list>]) # Some combination of [:left, :right, :top, :bottom, :all].

# `on_press` is a continuous event (it uses UILongPressGestureRecognizer), so
# you need to check the `gesture`:
view.on_press do |gesture|
Expand Down
19 changes: 19 additions & 0 deletions lib/ios/sugarcube-constants/symbol.rb
Expand Up @@ -248,6 +248,12 @@ def cglinejoin
def uigesturerecognizerstate
SugarCube.look_in(self, Symbol.uigesturerecognizerstate)
end

def uirectedge
SugarCube.look_in(self, Symbol.uirectedge)
end


alias uigesturestate uigesturerecognizerstate

class << self
Expand Down Expand Up @@ -324,6 +330,8 @@ class << self

attr :uigesturerecognizerstate

attr :uirectedge

end

@uidevice = {
Expand Down Expand Up @@ -892,4 +900,15 @@ class << self
recognized: UIGestureRecognizerStateRecognized,
}


@uirectedge = {
left: UIRectEdgeLeft,
right: UIRectEdgeRight,
top: UIRectEdgeTop,
bottom: UIRectEdgeBottom,
none: UIRectEdgeNone,
all: UIRectEdgeAll
}


end
26 changes: 19 additions & 7 deletions lib/ios/sugarcube-gestures/gestures.rb
Expand Up @@ -121,28 +121,40 @@ def on_swipe(direction_or_options=nil, &proc)
# @option options [Fixnum] :min_fingers Minimum number of fingers for gesture to be recognized
# @option options [Fixnum] :max_fingers Maximum number of fingers for gesture to be recognized
# @option options [Fixnum] :fingers If min_fingers or max_fingers is not assigned, this will be the default.
# @option options [Array] :edges Some combination of [:left, :right, :top, :bottom, :all]. If present, overrides fingers options and instead handles gestures originating at specified screen edges.
def on_pan(fingers_or_options=nil, &proc)
fingers = nil
edge_options = [:none]
min_fingers = nil
max_fingers = nil
recognizer = nil

if fingers_or_options
if fingers_or_options.is_a? Hash
fingers = fingers_or_options[:fingers] || fingers
edge_options = fingers_or_options[:edges] || edge_options
min_fingers = fingers_or_options[:min_fingers] || min_fingers
max_fingers = fingers_or_options[:max_fingers] || max_fingers
else
fingers = fingers_or_options
end
end

# if fingers is assigned, but not min/max, assign it as a default
min_fingers ||= fingers
max_fingers ||= fingers

recognizer = UIPanGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:')
recognizer.maximumNumberOfTouches = min_fingers if min_fingers
recognizer.minimumNumberOfTouches = max_fingers if max_fingers
if edge_options.uniq == [:none] # full view pan, possibly with finger options
# if fingers is assigned, but not min/max, assign it as a default
min_fingers ||= fingers
max_fingers ||= fingers
recognizer = UIPanGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:')
recognizer.maximumNumberOfTouches = min_fingers if min_fingers
recognizer.minimumNumberOfTouches = max_fingers if max_fingers
else #edges option, finger options ignored
edges = :none.uirectedge
edge_options.each do | edge |
edges |= (edge.uirectedge || 0)
end
recognizer = UIScreenEdgePanGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:')
recognizer.edges = edges
end
sugarcube_add_gesture(proc, recognizer)
end

Expand Down
21 changes: 20 additions & 1 deletion spec/ios/gestures_spec.rb
Expand Up @@ -69,14 +69,33 @@
@view.gestureRecognizers.should.include?(gesture)
end

it 'should work with options' do
it 'should work with fingers options' do
gesture = @view.on_pan(min_fingers: 2, max_fingers: 2) do |gesture|
end
gesture.should.be.kind_of(UIPanGestureRecognizer)
gesture.maximumNumberOfTouches.should == 2
gesture.minimumNumberOfTouches.should == 2
@view.gestureRecognizers.should.include?(gesture)
end

it 'should work with edges option' do
gesture = @view.on_pan(edges: [:top, :left]) do |gesture|
end
gesture.should.be.kind_of(UIScreenEdgePanGestureRecognizer)
gesture.edges.should == (:left.uirectedge + :top.uirectedge)
@view.gestureRecognizers.should.include?(gesture)
end

it 'should ignore :none edges option' do
gesture = @view.on_pan(edges: [:none], min_fingers: 2, max_fingers: 2) do |gesture|
end
gesture.should.be.kind_of(UIPanGestureRecognizer)
gesture.maximumNumberOfTouches.should == 2
gesture.minimumNumberOfTouches.should == 2
@view.gestureRecognizers.should.include?(gesture)
end


end

describe 'on_press' do
Expand Down