I am looking for a little guidance to start figuring out an animation that tracks finger movement and moves a collection of UIButtons along the outer path of a circle
I am picturing it will kind of have a revolver feel to it. Like each one locks into place at the bottom
or like the swiping through one of the slide inserts of these
thanks in advance
(Sample code on GitHub)
It's not really that difficult, there's just a lot of trigonometry involved.
Now, what I'm going to describe now, is not an animation, since you requested for it to track the position of your finger in the title. An animation would involve its own timing function, but since you're using the touch gesture, we can use the inherent timing that this event has and just rotate the view accordingly. (TL;DR: The user keeps the time of the movement, not an implicit timer).
Keeping track of the finger
First of all, let's define a convenient class that keeps track of the angle, I'm gonna call it
DialView
. It's really just a subclass of UIView, that has the following property:DialView.h
DialView.m
The
UIButton
s can be contained within this view (I'm not sure if you want the buttons to be responsible for the rotation? I'm gonna use aUIPanGestureRecognizer
, since it's the most convenient way).Let's build the view controller that will handle a pan gesture inside our
DialView
, let's also keep a reference to DialView.MyViewController.h
It's up to you on how you hook up the pan gesture, personally, I made it on the nib file. Now, the main body of this function:
MyViewController.m
CGPointAngle
is a method invented by me, it's just a nice wrapper foratan2
(and I'm throwingCGPointDistance
in if you call NOW!):The key here, is that there's two angles to keep track:
In this case, we want to have the view's angle be
originalAngle + deltaFinger
, and that's what the above code does, I just encapsulate all the state in a struct.Checking the radius
If that you want to keep track on "the border of the view", you should use the
CGPointDistance
method and check if the distance between thebegin.center
and thenow.finger
is an specific value.That's homework for ya!
Snapping back
Ok, now, this part is an actual animation, since the user no longer has control of it.
The snapping can be achieved by having a set defined of angles, and when the finger releases the finger (Gesture ended), snap back to them, like this:
The
buttons
variable, is just a stand-in for the number of buttons that are in the view. The equation is super simple actually, it just abuses the rounding function to approximate to the closes angle.