0

I want to create a rotating Button Which can be rotate from my given points I tried this but it gives angles and i want to give points

self.theImageView.transform=CGAffineTransformMakeRotation (angle);
       angle=30;

I also tried this but it has same problem

CABasicAnimation *halfTurn;
    halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    halfTurn.fromValue = [NSNumber numberWithFloat:0];
    halfTurn.toValue = [NSNumber numberWithFloat:((90*M_PI)/360)];
    halfTurn.duration = 1;
    halfTurn.repeatCount = false;
    [[button layer] addAnimation:halfTurn forKey:@"180"];

can any one suggest be or give code snippiest thanks in advance

zohaibkhan
  • 227
  • 1
  • 2
  • 12

1 Answers1

0

If you want to want to give two points and rotate your view around those two points, there is a catch. You have to see to it that those two points are equidistant from the anchor of rotation. You can calculate the arc-angle of such a rotation and set it accordingly.

You can find the length of the arc here: http://math.about.com/od/formulas/ss/surfaceareavol_9.htm

You can find the formula for finding the arc angle, in this link: http://www.regentsprep.org/Regents/math/algtrig/ATM1/arclengthlesson.htm

Edit

I am now given a premise that there are 7 buttons arranged circularly around an anchor point. What you do now, is maintain an instance level variable called currentAngle and initialize it to 0.

CGFloat nextAngle = currentAngle + (360/7);
CABasicAnimation *halfTurn;
    halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    halfTurn.fromValue = [NSNumber numberWithFloat:((currentAngle*M_PI)/360)];
    halfTurn.toValue = [NSNumber numberWithFloat:((nextAngle*M_PI)/360)];
    halfTurn.duration = 1;
    halfTurn.repeatCount = false;
    [[button layer] addAnimation:halfTurn forKey:@"clock_wise_rotation"];
currentAngle = nextAngle;

The above code is for clockwise rotation only. If you want to perform anticlockwise animation, you have to subtract 360/7 and perform the animation.

avismara
  • 5,141
  • 2
  • 32
  • 56
  • I have 7 buttons in circles and in Center there is a pointer . I want to move pointer to button with Animation which is clicked. In this scenario which approach should i Use. – zohaibkhan Sep 01 '14 at 11:04
  • 1
    The angle difference between each of those points will be 360/7. Make that animation, to move from one angle to other. Maintain a variable that tells you the current angle position. And add or subtract 360/7 degrees depending on the direction of movement. – avismara Sep 01 '14 at 11:07
  • how can i add animation to this rotation. please give some code snippiest if possible – zohaibkhan Sep 01 '14 at 11:10
  • Your code itself has the right implementation of a rotation animation. – avismara Sep 01 '14 at 11:12
  • Everything work fine but when animation complete it move back to its initial position – zohaibkhan Sep 01 '14 at 11:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60388/discussion-between-codingvoldemort-and-zohaibkhan). – avismara Sep 01 '14 at 11:36