Skip to content

Commit

Permalink
Merge pull request #134 from rubymotion/refactored_gestures
Browse files Browse the repository at this point in the history
Made the camelCase methods snake_case
  • Loading branch information
dmarkow committed Sep 6, 2012
2 parents 5aa044c + 45b8b42 commit 308a714
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
49 changes: 41 additions & 8 deletions motion/ui/gestures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,71 @@

class UIView

def when_tapped(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UITapGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end

def when_pinched(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UIPinchGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end

def when_rotated(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UIRotationGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end

def when_swiped(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end

def when_panned(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UIPanGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end

def when_pressed(enableInteraction=true, &proc)
add_gesture_recognizer_helper(UILongPressGestureRecognizer.alloc.initWithTarget(self, action:'handle_gesture:'), enableInteraction, proc)
end


def whenTapped(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UITapGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenTapped] please use when_tapped instead."
when_tapped(enableInteraction, &proc)
end

def whenPinched(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UIPinchGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenPinched] please use when_pinched instead."
when_pinched(enableInteraction, &proc)
end

def whenRotated(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UIRotationGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenRotated] please use when_rotated instead."
when_rotated(enableInteraction, &proc)
end

def whenSwiped(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenSwiped] please use when_swiped instead."
when_swiped(enableInteraction, &proc)
end

def whenPanned(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UIPanGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenPanned] please use when_panned instead."
when_panned(enableInteraction, &proc)
end

def whenPressed(enableInteraction=true, &proc)
addGestureRecognizerHelper(proc, enableInteraction, UILongPressGestureRecognizer.alloc.initWithTarget(self, action:'motionHandleGesture:'))
NSLog "[DEPRECATED - whenPressed] please use when_pressed instead."
when_pressed(enableInteraction, &proc)
end



private

def motionHandleGesture(recognizer)
def handle_gesture(recognizer)
@recognizers[recognizer].call(recognizer)
end

# Adds the recognizer and keeps a strong reference to the Proc object.
def addGestureRecognizerHelper(proc, enableInteraction, recognizer)
def add_gesture_recognizer_helper(recognizer, enableInteraction, proc)
setUserInteractionEnabled true if enableInteraction && !isUserInteractionEnabled
self.addGestureRecognizer(recognizer)

Expand Down
18 changes: 12 additions & 6 deletions spec/motion/core/gestures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,33 @@
# it 'responds to interaction'
end

describe '#whenTapped' do
describe '#when_tapped' do
testMethod.call :when_tapped
testMethod.call :whenTapped
end

describe '#whenPinched' do
describe '#when_pinched' do
testMethod.call :when_pinched
testMethod.call :whenPinched
end

describe '#whenRotated' do
describe '#when_rotated' do
testMethod.call :when_rotated
testMethod.call :whenRotated
end

describe '#whenSwiped' do
describe '#when_swiped' do
testMethod.call :when_swiped
testMethod.call :whenSwiped
end

describe '#whenPanned' do
describe '#when_panned' do
testMethod.call :when_panned
testMethod.call :whenPanned
end

describe '#whenPressed' do
describe '#when_pressed' do
testMethod.call :when_pressed
testMethod.call :whenPressed
end

Expand Down

0 comments on commit 308a714

Please sign in to comment.