Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAffineTransformMakeRotation counter clockwise always

I an trying to animate a UIImageView to rotate counter clockwise when a user touches it and moves its finger on it in clockwise direction.
Basically I want the UIImageView to go back to its original position once the touch ends and it should move in counter clockwise direction.

The problem I have is that every time angle (in degrees) is greater than 180 then the image rotates clockwise back to its original position. Its apparently taking the shortest path back. As for angles 179 degrees or less the image rotate counter clockwise (what i need).

I tried both these pieces of code and they behave the same way. Any suggestions?

NOTE: here angle variable is a double and its initialized to be zero and it remains 0 throughout my code

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self handleObject:touches withEvent:event isLast:YES];

    /* this doesn't make rotation counter clockwise for rotations greater than 180 */
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationRepeatCount:1]; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    circle1ImgVw.transform = CGAffineTransformRotate(transform, angle);
    // ends animation
    [UIView commitAnimations];


    /* this doesn't make rotation counter clockwise for rotations greater than 180 */
    [UIView animateWithDuration:1.0 animations:^{
            circle1ImgVw.transform = CGAffineTransformMakeRotation(angle);
        }];
} 

I tried looking into this post and there are couple of problems with it

  • I cannot touch and rotate the image anymore after the animation is done
  • It always animates in clockwise direction and I haven't been able to figure out how to make it go counter clockwise smoothly from the point where the user ended rotating the image.
like image 659
Sam B Avatar asked Dec 09 '12 20:12

Sam B


People also ask

Is rotation always counter clockwise?

By convention, positive rotations go counter clockwise, and negative rotations go clockwise." Generally, clockwise is assumed if the direction is not specified.

What is rotate counter clockwise?

If something is moving counterclockwise, it is moving in the opposite direction to the direction in which the hands of a clock move. [US] Rotate the head clockwise and counterclockwise. Counterclockwise is also an adjective.

Which angle rotates in a counter clockwise direction?

If the rotation is counterclockwise, the angle has a positive measure. If the rotation is clockwise, the angle has a negative measure. An angle in standard position is said to lie in the quadrant where the terminal side resides. One way to measure an angle is in degrees.

Which way is anti-clockwise left or right?

Clockwise and anti-clockwise are ways of indicating the direction of a turn. So, what way is clockwise? Clockwise, involves a turn to the right as it follows the hands of a clock and anti-clockwise involves a turn to the left, against the direction of a clock's hands.


1 Answers

I had a similar issue, check the accepted answer out, it might help you:

Rotate a UIView clockwise for an angle greater than 180 degrees


In response to comments, maybe this will help:

In my code I actually use

rotationAnimation.fromValue
rotationAnimation.toValue

instead of

rotationAnimation.byValue.

The rotation is always going to be counter clockwise if the toValue is less than the fromValue. It doesn't matter if your values are positive or negative, only the relation between them.

Figure out what your starting angle is, figure out which direction you want to rotate and figure out what your ending angle is. Make sure that it's less than your starting angle if you want to go counter clockwise. Here is the code I use for animating a clock hand clockwise from 12:00 to the current time.

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self updateClockArmAngle];
}

- (void)updateClockArmAngle
{
    // Get the time
    NSDate *date = [NSDate date];
    NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];
    NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];

    CGFloat hour = [components hour];
    CGFloat angle = (hour/24.0)*(2*M_PI);

    CGFloat minute = [components minute];
    angle += (minute/(24*60))*(2*M_PI);

    [self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle];
}


- (void) rotateViewAnimated:(UIView*)view
               withDuration:(CFTimeInterval)duration
                    byAngle:(CGFloat)angle
{    
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = 0;
    rotationAnimation.toValue = [NSNumber numberWithFloat:angle];
    rotationAnimation.duration = duration;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [rotationAnimation setRemovedOnCompletion:NO];
    [rotationAnimation setFillMode:kCAFillModeForwards];
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
like image 55
Darren Avatar answered Sep 21 '22 15:09

Darren