Module: MotionWiretap::Gestures

Defined in:
lib/motion-wiretap/ios/wiretap_gestures.rb

Overview

Some gesture recognizer translators. Based on SugarCube’s gesture recognizer methods.

Class Method Summary collapse

Class Method Details

.tap(taps) ⇒ Object .tap(options) ⇒ Object

Overloads:

  • .tap(taps) ⇒ Object

    Parameters:

    • taps (Fixnum)

      Number of taps

  • .tap(options) ⇒ Object

    Options Hash (options):

    • :min_fingers (Fixnum)

      Minimum umber of fingers for gesture to be recognized

    • :max_fingers (Fixnum)

      Maximum number of fingers for gesture to be recognized

    • :fingers (Fixnum)

      If min_fingers or max_fingers is not assigned, this will be the default.

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 121

def pan(target, fingers_or_options=nil)
  fingers = nil
  min_fingers = nil
  max_fingers = nil
  delegate = nil

  if fingers_or_options
    if fingers_or_options.is_a? Hash
      fingers = fingers_or_options[:fingers]
      min_fingers = fingers_or_options[:min_fingers]
      max_fingers = fingers_or_options[:max_fingers]
      delegate = fingers_or_options[:delegate]
    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(target, action: 'handle_gesture:')
  recognizer.maximumNumberOfTouches = min_fingers if min_fingers
  recognizer.minimumNumberOfTouches = max_fingers if max_fingers
  recognizer.delegate = delegate if delegate
  return recognizer
end

.pinch(target, scale_or_options = nil) {|recognizer| ... } ⇒ Object

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 36

def pinch(target, scale_or_options=nil)
  scale = nil
  delegate = nil

  if scale_or_options
    if scale_or_options.is_a? Hash
      scale = scale_or_options[:scale]
      delegate = scale_or_options[:delegate]
    else
      scale = scale_or_options
    end
  end

  recognizer = UIPinchGestureRecognizer.alloc.initWithTarget(target, action: 'handle_gesture:')
  recognizer.scale = scale if scale
  recognizer.delegate = delegate if delegate
  return recognizer
end

.press(duration) ⇒ Object .press(options) ⇒ Object

Overloads:

  • .press(duration) ⇒ Object

    Parameters:

    • duration (Fixnum)

      How long in seconds before gesture is recognized

  • .press(options) ⇒ Object

    Options Hash (options):

    • :duration (Fixnum)

      How long in seconds before gesture is recognized

    • :taps (Fixnum)

      Number of taps before gesture is recognized

    • :fingers (Fixnum)

      Number of fingers before gesture is recognized

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 156

def press(target, duration_or_options=nil)
  duration = nil
  taps = nil
  fingers = nil
  delegate = nil

  if duration_or_options
    if duration_or_options.is_a? Hash
      duration = duration_or_options[:duration]
      taps = duration_or_options[:taps]
      fingers = duration_or_options[:fingers]
      delegate = duration_or_options[:delegate]
    else
      duration = duration_or_options
    end
  end

  recognizer = UILongPressGestureRecognizer.alloc.initWithTarget(target, action: 'handle_gesture:')
  recognizer.minimumPressDuration = duration if duration
  recognizer.numberOfTapsRequired = taps if taps
  recognizer.numberOfTouchesRequired = fingers if fingers
  recognizer.delegate = delegate if delegate
  return recognizer
end

.rotate(target, rotation_or_options = nil) {|recognizer| ... } ⇒ Object

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 56

def rotate(target, rotation_or_options=nil)
  rotation = nil
  delegate = nil

  if rotation_or_options
    if rotation_or_options.is_a? Hash
      rotation = rotation_or_options[:rotation]
      delegate = rotation_or_options[:delegate]
    else
      rotation = rotation_or_options
    end
  end

  recognizer = UIRotationGestureRecognizer.alloc.initWithTarget(target, action: 'handle_gesture:')
  recognizer.rotation = rotation if rotation
  recognizer.delegate = delegate if delegate
  return recognizer
end

.swipe(taps) ⇒ Object .swipe(options) ⇒ Object

Overloads:

  • .swipe(taps) ⇒ Object

    Parameters:

    • direction (Fixnum)

      Direction of swipe

  • .swipe(options) ⇒ Object

    Options Hash (options):

    • :fingers (Fixnum)

      Number of fingers before gesture is recognized

    • :direction (Fixnum, Symbol)

      Direction of swipe, as a UISwipeGestureRecognizerDirection constant or a symbol (‘:left, :right, :up, :down`)

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 81

def swipe(target, direction_or_options)
  direction = nil
  fingers = nil
  delegate = nil

  if direction_or_options
    if direction_or_options.is_a? Hash
      direction = direction_or_options[:direction]
      fingers = direction_or_options[:fingers]
      delegate = direction_or_options[:delegate]
    else
      direction = direction_or_options
    end
  end

  case direction
  when :left
    direction = UISwipeGestureRecognizerDirectionLeft
  when :right
    direction = UISwipeGestureRecognizerDirectionRight
  when :up
    direction = UISwipeGestureRecognizerDirectionUp
  when :down
    direction = UISwipeGestureRecognizerDirectionDown
  end

  recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(target, action: 'handle_gesture:')
  recognizer.direction = direction if direction
  recognizer.numberOfTouchesRequired = fingers if fingers
  recognizer.delegate = delegate if delegate
  return recognizer
end

.tap(taps) ⇒ Object .tap(options) ⇒ Object

Overloads:

  • .tap(taps) ⇒ Object

    Parameters:

    • taps (Fixnum)

      Number of taps

  • .tap(options) ⇒ Object

    Options Hash (options):

    • :taps (Fixnum)

      Number of taps before gesture is recognized

    • :fingers (Fixnum)

      Number of fingers before gesture is recognized

Yields:

  • (recognizer)

    Handles the gesture event, and passes the recognizer instance to the block.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/motion-wiretap/ios/wiretap_gestures.rb', line 13

def tap(target, taps_or_options=nil)
  taps = nil
  fingers = nil
  delegate = nil

  if taps_or_options
    if taps_or_options.is_a? Hash
      taps = taps_or_options[:taps]
      fingers = taps_or_options[:fingers]
      delegate = taps_or_options[:delegate]
    else
      taps = taps_or_options
    end
  end

  recognizer = UITapGestureRecognizer.alloc.initWithTarget(target, action: 'handle_gesture:')
  recognizer.numberOfTapsRequired = taps if taps
  recognizer.numberOfTouchesRequired = fingers if fingers
  recognizer.delegate = delegate if delegate
  return recognizer
end