Skip to content

Commit

Permalink
Working gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed May 20, 2014
1 parent cb19e7e commit 5f3b83f
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/gesture_tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def viewDidLoad
@detail_view = UITextField.alloc.initWithFrame [[0, 0], [160, 26]]
@detail_view.delegate = self
@detail_view.borderStyle = UITextBorderStyleNone;
@detail_view.accessibilityLabel = "gesture_name"
@detail_view.accessibilityLabel = "gesture_detail"
@detail_view.setFont(UIFont.fontWithName("Helvetica", size:12))
@detail_view.textAlignment = UITextAlignmentCenter
@detail_view.text = ""
Expand Down
169 changes: 169 additions & 0 deletions app/controllers/gestures_visualizer_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
class GesturesVisualizerController < UIViewController
include ReadonlyFieldDelegate

def viewDidLoad
super

# simple init
self.title = "Gestures -- Visual"
self.view.backgroundColor = UIColor.whiteColor

@mainImage = UIImageView.alloc.initWithFrame self.view.frame
@mainImage.backgroundColor = UIColor.whiteColor
@mainImage.opaque = false
@mainImage.userInteractionEnabled = true
@mainImage.multipleTouchEnabled = true
image = UIImage.imageNamed('white.png')
image.accessibilityLabel = "whiteBackground"
@mainImage.setImage(image)
@mainImage.accessibilityLabel = "paintCanvas"
self.view.addSubview @mainImage

# @mainImage = UIScrollView.alloc.initWithFrame self.view.frame
# @mainImage.accessibilityLabel = "myscrollview"
# @mainImage.backgroundColor = UIColor.whiteColor
# @mainImage.opaque = false
# self.view.addSubview @mainImage

label = UILabel.alloc.initWithFrame [[0, 0], [160, 26]]
label.setFont(UIFont.fontWithName("Helvetica-Bold", size:14))
label.textAlignment = UITextAlignmentCenter
label.text = "Gestures"
label.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height - 200)
@mainImage.addSubview label

@name_view = UITextField.alloc.initWithFrame [[0, 0], [160, 26]]
@name_view.delegate = self
@name_view.borderStyle = UITextBorderStyleNone;
@name_view.accessibilityLabel = "gesture_name"
@name_view.setFont(UIFont.fontWithName("Helvetica", size:12))
@name_view.textAlignment = UITextAlignmentCenter
@name_view.text = ""
@name_view.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height - 175)
@mainImage.addSubview @name_view

# @detail_view = UITextField.alloc.initWithFrame [[0, 0], [160, 26]]
# @detail_view.delegate = self
# @detail_view.borderStyle = UITextBorderStyleNone;
# @detail_view.accessibilityLabel = "gesture_detail"
# @detail_view.setFont(UIFont.fontWithName("Helvetica", size:12))
# @detail_view.textAlignment = UITextAlignmentCenter
# @detail_view.text = ""
# @detail_view.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height - 150)
# @mainImage.addSubview @detail_view

# add gesture control stuffs
# init_swipe_recognizers(@mainImage)
# scroll.addGestureRecognizer(UIPinchGestureRecognizer.alloc.initWithTarget(self, action:"handle_pinch:"))
# scroll.addGestureRecognizer(UILongPressGestureRecognizer.alloc.initWithTarget(self, action:"handle_longpress:"))
# scroll.addGestureRecognizer(UITapGestureRecognizer.alloc.initWithTarget(self, action:"handle_tap:"))


def @mainImage.touchesBegan(touches, withEvent:event)
self.subviews.first.text = "began"

@mouseSwiped = false
touch = touches.anyObject
@lastPoint = touch.locationInView self
end

def @mainImage.touchesMoved(touches, withEvent:event)
@mouseSwiped = true
touch = touches.anyObject
currentPoint = touch.locationInView self

self.subviews.first.text = "moved"

UIGraphicsBeginImageContext(self.frame.size)
self.image.drawInRect(CGRectMake(0, 0, self.frame.size.width, self.frame.size.height))
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), @lastPoint.x, @lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y)
CGContextSetLineCap(UIGraphicsGetCurrentContext(), KCGLineCapRound)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0)
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0)
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), KCGBlendModeNormal)
CGContextStrokePath(UIGraphicsGetCurrentContext())
self.image = UIGraphicsGetImageFromCurrentImageContext()
self.setAlpha(1.0)
UIGraphicsEndImageContext()

@lastPoint = currentPoint
end

def @mainImage.touchesEnded(touches, withEvent:event)
self.subviews.first.text = "ended"

if(!@mouseSwiped)
UIGraphicsBeginImageContext(self.frame.size)
self.image.drawInRect(CGRectMake(0, 0, self.frame.size.width, self.frame.size.height))
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), @lastPoint.x, @lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), @lastPoint.x, @lastPoint.y)
CGContextSetLineCap(UIGraphicsGetCurrentContext(), KCGLineCapRound)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0)
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0)
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), KCGBlendModeNormal)
CGContextStrokePath(UIGraphicsGetCurrentContext())
self.image = UIGraphicsGetImageFromCurrentImageContext()
self.setAlpha(1.0)
UIGraphicsEndImageContext()
end
end
end

def handle_swipe(sender)
display_gesture("#{direction_name(sender.direction)} swipe", sender.locationInView(self.view))
end

def direction_name(direction)
if direction == UISwipeGestureRecognizerDirectionDown
"Down"
elsif direction == UISwipeGestureRecognizerDirectionUp
"Up"
elsif direction == UISwipeGestureRecognizerDirectionRight
"Right"
elsif direction == UISwipeGestureRecognizerDirectionLeft
"Left"
else
"Unknown"
end
end

def handle_tap(sender)
display_gesture("Tap", sender.locationInView(self.view))
end

def handle_longpress(sender)
display_gesture("LongPress", sender.locationInView(self.view))
end

def handle_pinch(sender)
@name_view.text = sender.scale >= 1 ? "Zoom" : "Pinch"
# @detail_view.text = "Scale: #{sender.scale}; Velocity: #{sender.velocity}"
end

def display_gesture(name, location)
@name_view.text = "#{name}"
# @detail_view.text = "Location: x:#{location.x}, y:#{location.y}"
end

def init_swipe_recognizers(view)
rightSR = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:"handle_swipe:")
view.addGestureRecognizer(rightSR)

leftSR = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:"handle_swipe:")
leftSR.direction = UISwipeGestureRecognizerDirectionLeft
view.addGestureRecognizer(leftSR)

upSR = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:"handle_swipe:")
upSR.direction = UISwipeGestureRecognizerDirectionUp
view.addGestureRecognizer(upSR)

downSR = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:"handle_swipe:")
downSR.direction = UISwipeGestureRecognizerDirectionDown
view.addGestureRecognizer(downSR)
end

def gestureRecognizer(gestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer)
return true
end
end
Binary file added resources/dmg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 41 additions & 8 deletions spec/sanity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ def server_url
# end
# end

describe "Tap action" do
it "should tap on the right place" do
el = find_element(:name, "Gestures -- Schematic").click

name = find_element(:name, "gesture_name")
name.text.should eq ""

el = find_element(:name, "myscrollview")
action = Appium::TouchAction.new
action.tap(x: 100, y: 100, duration: 100).perform
detail = find_element(:name, "gesture_detail")
puts detail.text
name.text.should eq "LongPress"
end
end

describe "Swipe actions" do
it "should swipe right" do
el = find_element(:name, "Gestures -- Schematic")
Expand All @@ -112,11 +128,6 @@ def server_url
name = find_element(:name, "gesture_name")
name.text.should eq ""

el = find_element(:name, "myscrollview")
action = Appium::TouchAction.new
action.press(element: el).release.perform
name.text.should eq "Tap"

swipe(start_x: 100, start_y: 400, end_x: 200, end_y: 0)
name.text.should eq "Right swipe"
end
Expand All @@ -129,8 +140,6 @@ def server_url
name = find_element(:name, "gesture_name")
name.text.should eq ""

el = find_element(:name, "myscrollview")

swipe(start_x: 300, start_y: 400, end_x: -200, end_y: 0)
name.text.should eq "Left swipe"
end
Expand Down Expand Up @@ -208,9 +217,33 @@ def server_url
zoom.add top
zoom.add bottom
zoom.perform

sleep(1)

name.text.should eq "Pinch"
end
end

describe "Visualizing complex gestures" do
it "should tap on the right place" do
el = find_element(:name, "Gestures -- Visual").click

name = find_element(:name, "gesture_name")
name.text.should eq ""

el = find_element(:class_name, "UIAImage")

action = Appium::TouchAction.new
action.
press(x: 100, y: 200).
move_to(x: 25, y: -100).
move_to(x: 25, y: 100).
release.
perform

# need to do this for some reason... TODO: look into
name.text

sleep(10)
end
end
end

0 comments on commit 5f3b83f

Please sign in to comment.