简体   繁体   中英

Rotating UIImageView more than 180 degrees using animations on iOS

Im trying to animate a moving meter by rotating it more than 180 degrees, but it wont work. Im using CGAffineTransform to rotate the meter, which uses a net result to make the rotation. This means that i cannot choose CW or CCW rotation when using this function. How can I modified this code to make it rotate 4 radians. Currently the rotation is transformed to a net result, making the rotation minimal.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
CGAffineTransform transform = CGAffineTransformMakeRotation(4);
self.meter.transform = transform;
[UIView commitAnimations];

EDIT: From the answers i managed to get it working by doing this

 [UIView animateWithDuration:1.5 animations:^{
    CABasicAnimation *fullRotation;
    fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((330*M_PI)/180)];
    fullRotation.duration = 2.0f;
    fullRotation.repeatCount = 1;
    fullRotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    fullRotation.fillMode = kCAFillModeForwards;
    fullRotation.removedOnCompletion = NO;

    // add animation in your view
    [self.meter.layer addAnimation:fullRotation forKey:@"330"];
    [self performSelector:@selector(stopRotate:) withObject:self.meter afterDelay:2.0];
}];

You need to convert the radian to degree using the following macro:

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

and pass the value to the function:

-(UIImage*)rotateImage:(UIImage*)img forAngle:(CGFloat)degree   {

UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degree);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;

UIGraphicsBeginImageContext(rotatedSize);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(context, radian);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(-img.size.width, -img.size.height, img.size.width, img.size.height), img.CGImage);
UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return returnImg;
}

Try this, just change the basic properties to match your requirements:

    CATransform3D rotationTransform = CATransform3DMakeRotation(((4) * (180.0 / M_PI)), 0, 0, 1.0);

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    rotationAnimation.duration = 0.6f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1;

    //Add this two lines
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;

    [self.meter.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

Let me know is it works for you.

//Add This in header prefix

 #define MAXFLOAT    0x1.fffffep+127f

// add For rotation

 [UIView animateWithDuration:1.5 animations:^{
      CABasicAnimation *fullRotation;
    fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:(2*M_PI))];
    fullRotation.duration =1.0f;
    fullRotation.repeatCount = MAXFLOAT;
    // add animation in your view
    [yourView.layer addAnimation:fullRotation forKey:@"360"];
    [self performSelector:@selector(stopRotate:) withObject:yourView afterDelay:1.0];
}];

And Add this code to stop rotation From view

-(void)stopRotate :(UIView *)yourView
{
  [yourView.layer removeAnimationForKey:@"360"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM