nupac
nupac

Reputation: 2489

Restrict movement of UIButton along a UIBezierPath path

I have a circular UIBezierPath. I use the path to draw a circle on my view to create an outline of a 24 hr clock. I have a UIButton whose position depends on the current time. The button acts like an Hour hand. I want the users to be able to move the UIButton along the circular path. I call it "visit the future/past" feature. How do I restrict the buttons movement to the path I have?

Upvotes: 0

Views: 284

Answers (2)

Suhas
Suhas

Reputation: 1500

Override touchesBegan: and touchesMoved: methods in your view

- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event 
{
   if([[event touchesForView:button] count])
   {
       //User is trying to move the button set a variable to indicate this.
   }
}



- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint *point = [[event anyObject] locationInView:self];

    /*Compare x and y coordinates with the centre property of the button 
      If x or y are greater set center of button to next point in circle or previous
      point if any of them are lesser.*/
}

Note that you will have to save all points in your circle in an array before attempting this or you will have to calculate the points on the circumference of the circle by knowing the radius.

Upvotes: 1

Son Nguyen
Son Nguyen

Reputation: 3491

The easiest way is in touchesMoved, you can check to ignore touch which is not in your circle view by using:

CGPoint point = [touch locationInView:circleView];

if (![circleView pointInside:point withEvent:event]) {
     return;
}

Upvotes: 0

Related Questions