Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #51 from ethercrow/master

Ability to pick views by touching while being connected to WaxServer.
  • Loading branch information...
commit 3542925e95c53f3f21de409ca7ad0b749177db44 2 parents bba4229 + d400f13
@probablycorey authored
Showing with 55 additions and 0 deletions.
  1. +1 −0  lib/stdlib/helpers/init.lua
  2. +54 −0 lib/stdlib/helpers/pickView.lua
View
1  lib/stdlib/helpers/init.lua
@@ -6,6 +6,7 @@ require "wax.helpers.time"
require "wax.helpers.cache"
require "wax.helpers.autoload"
require "wax.helpers.WaxServer"
+require "wax.helpers.pickView"
-- Just a bunch of global helper functions
View
54 lib/stdlib/helpers/pickView.lua
@@ -0,0 +1,54 @@
+
+function startPick()
+ local w = UIApplication:sharedApplication():keyWindow()
+ _interceptor = InterceptorView:alloc():initWithFrame(w:bounds())
+ w:addSubview(_interceptor)
+ return "Go on, touch something. Get the view by calling endPick() when you're done."
+end
+
+function endPick()
+ local v = _interceptor:pickedView()
+ _interceptor:removeFromSuperview()
+ return v
+end
+
+-- wax doesn't automagically bridge C functions
+function CGRectContainsPoint(rect, point)
+ return rect.x <= point.x
+ and point.x <= rect.x + rect.width
+ and rect.y <= point.y
+ and point.y <= rect.y + rect.height
+end
+
+waxClass{"InterceptorView", UIView}
+
+function touchesEnded_withEvent(self, touches, event)
+ local point = touches:anyObject():locationInView(self)
+ local w = UIApplication:sharedApplication():keyWindow()
+ local pointInWindow = w:convertPoint_fromView(point, self)
+ self.pickedView_ = self:findOwnerOfPoint_startingWith(pointInWindow, w)
+ print(self.pickedView_:class())
+end
+
+function pickedView(self)
+ return self.pickedView_
+end
+
+function findOwnerOfPoint_startingWith(self, point, view)
+ if view == self then
+ return nil
+ end
+
+ if not CGRectContainsPoint(view:bounds(), point) then
+ return nil
+ end
+
+ local betterResult = nil
+
+ for i, subview in ipairs(view:subviews()) do
+ pointInSubview = subview:convertPoint_fromView(point, view)
+ betterResult = self:findOwnerOfPoint_startingWith(pointInSubview, subview) or betterResult
+ end
+
+ return betterResult or view
+end
Please sign in to comment.
Something went wrong with that request. Please try again.