Imagine an iOS screen where you can move your finger up/down to do something (imagine say, "scale"),
Or,
as a separate function you can move your finger left/right to do something (imagine say, "change color" or "rotate").
They are
separate
functions, you can only do one at at time.
So, if it "begins as" a horizontal version, it is remains only a horizontal version. Conversely if it "begins as" a vertical version, it remains only a vertical version.
It is a little bit tricky to do this, I present exactly how to do it below...the fundamental pattern is:
if (r.state == .Began || panway == .WasZeros )
{
prev = tr
if (tr.x==0 && tr.y==0)
{
panway = .WasZeros
return
}
if (abs(tr.x)>abs(tr.y)) ... set panway
}
This works very well and here's exactly how to do it in Swift.
In storyboard take a UIPanGestureRecognizer
, drag it to the view in question. Connect the delegate to the view controller and set the outlet to this call ultraPan:
enum Panway
{
case Vertical
case Horizontal
case WasZeros
}
var panway:Panway = .Vertical
var prev:CGPoint!
@IBAction func ultraPan(r:UIPanGestureRecognizer!)
{
let tr = r.translationInView(r.view)
if (r.state == .Began || panway == .WasZeros )
{
prev = tr
if (tr.x==0 && tr.y==0)
{
panway = .WasZeros
return
}
if (abs(tr.x)>abs(tr.y))
{
panway = .Horizontal
}
else
{
panway = .Vertical
}
}
if (panway == .Horizontal) // your left-right function
{
var h = tr.x - prev.x
let sensitivity:CGFloat = 50.0
h = h / sensitivity
// adjust your left-right function, example
someProperty = someProperty + h
}
if (panway == .Vertical) // bigger/smaller
{
var v = tr.y - prev.y
let sensitivity:CGFloat = 2200.0
v = v / sensitivity
// adjust your up-down function, example
someOtherProperty = someOtherProperty + v
}
prev = tr
}
That's fine.
But it would surely be better to make a new subclass (or something) of UIPanGestureRecognizer, so that there are two new concepts......
UIHorizontalPanGestureRecognizer
UIVerticalPanGestureRecognizer
Those would be basically one-dimensional panners.
I have absolutely no clue whether you would ... subclass the delegates? or the class? (what class?), or perhaps some sort of extension ... indeed, I basically am completely clueless on this :)
The goal is in one's code, you can have something like this ...
@IBAction func horizontalPanDelta( ..? )
{
someProperty = someProperty + delta
}
@IBAction func verticalPanDelta( ..? )
{
otherProperty = otherProperty + delta
}
How to inherit/extend UIPanGestureRecognizer
in this way??