Rotating Image multiple times and stopping at desired degree? - ios

i had tried various method and couldn't figure out how to achieve this.i searched on internet a lot but can't figure out.
i have an ImageView (a wheel which spins) , i want to rotate it by 360 degree for 10 times(this is to just give user feel of fast turning wheel) , and then i want to rotate it by particular value say 90 degree( but it might be varying).
after this animation finishes i want to bring ImageView back to the initial position.
how do i achieve this ?
Thanks in advance.

well i found out how to do this.
i used below method to rotate by 360 degree for 10 times , and then rotate by particular degree.
-(void)rotate:(int)degreeToRotate
{
CGFloat radians = (M_PI/180) * degreeToRotate;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations: ^{
//configuring to rotate 360 degree 10 times
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;
[_scoreImageView.layer addAnimation:rotationAnimation forKey:#"rotationAnimation"];
} completion:^(BOOL finished) {
//rotate by particular degree
[ UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// -radians, to ensure clockwise rotation
self.scoreImageView.transform = CGAffineTransformMakeRotation(-radians);
} completion:^(BOOL finished) {
//method call to reset image original position
[self performSelector:#selector(resetPosition) withObject:nil afterDelay:3];
}];
}];
}
below method is used to reset image to original position.
-(void)resetPosition
{
[UIView animateWithDuration:0.2 animations:^() {
self.scoreImageView.transform = CGAffineTransformIdentity;
}];
}

Related

UIImageview rotation animation

I want to apply an animation to two UIImageViews. The idea is for image one to flip horizontally by 90 degrees and then image two to complete the rotation. Think of it as a coin spinning: Head side (image one) faces forward -> turn 90 degrees -> Tail side (image two) rotates to face forward. I can do the first half of the animation but I am stuck on the second.
[UIView animateWithDuration:1.0f animations:^{
image1.transform = CGAffineTransformMakeScale(-0.01, 1);
} completion: ^(BOOL finished) {
// How can I make image 2 to rotate out as if it initially was already rotated by 90 degrees?
}];
for flipping animation, there a animation option called UIViewAnimationOptionTransitionFlipFromRight, use it with the UIView's animation method for example, like below for example
[UIView transitionWithView:myImageView //with first image
duration:animationDuration
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
myImageView.image = toImage; //to next image
} completion:^(BOOL finished) {
//completion actions after flipped
}];
there are other animation options also there, like FromLeft,
FromTop,FromBottom use any one to your requirement
Take a look at this question about flipping a UIView in 3D using CATransform3D.
Simple Core Animations 3D transform of UIView
The answer:
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -1000.0;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI * 0.6, 1.0f, 0.0f, 0.0f);
[UIView animateWithDuration:1.0 animations:^{
self.someView.layer.anchorPoint = CGPointMake(0.5, 0);
self.someView.layer.transform = rotationAndPerspectiveTransform;
} completion:^(BOOL finished){
// code to be executed when flip is completed
}];

UIView will only animate once

I'm trying to animate an arrow spinning while my app downloads information. After each half spin, I want the app to check if the data has been downloaded yet. If not, the arrow should spin halfway again, with a short pause in between each spin.
-(void)animation {
[UIView animateWithDuration:0.7 delay:0 options:0 animations: ^{
imageView.transform = CGAffineTransformMakeRotation(180 * M_PI / 180);
} completion: ^(BOOL completed) {
if (completed && stillReloading) {
[self performSelector:#selector(animation) withObject:nil afterDelay:0.2];
}
}];
}
Even though the animation function is continually called, the image only rotates once. All calls made to the animation function after the first animation are ignored. Why is this? I don't want to set the repeat option on the animation as I do not know how many times the arrow will have the spin, and as I would like there to be a short pause between each spin.
This line
imageView.transform = CGAffineTransformMakeRotation(180 * M_PI / 180);
sets the angle to 180 degrees, and just that. The next time it's called same angle is used, so you see no animation.
If you want to rotate it again from its current position then you need to use a different method. CGAffineTransformMakeRotation makes a rotation from the identity transform. So what you end up with is this: Rotate to 180 degrees, rotate to 180 degrees, rotate to 180 degrees and the object never moves. Instead you need to use a method that applies the offset to the current transform. That is what the following method is for:
CGAffineTransformRotate(CGAffineTransform, CGFloat)
However, instead of applying it to CGAffineTransformIdentity as Saohooou's answer does, you should apply it to the current transform of your view.
Because you've made you view animate to angle (180 * M_PI / 180), it will never spring again if you set the same angle to it.
try this
CGFloat t = 180*M_PI / 180.0;
CGAffineTransform translateSpring = CGAffineTransformRotate(CGAffineTransformIdentity, t);
[UIView animateWithDuration:0.7 delay:0.0 options:nil animations:^{
imageView.transform = translateSpring;
} completion:^(BOOL completed) {
if (completed && stillReloading) {
//Use this method to let it spring back to its original angle
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
imageView.transform = CGAffineTransformIdentity;
} completion:NULL];
[self performSelector:#selector(animation) withObject:nil afterDelay:0.2];
}
}];
OR you can also set a dynamically update angle for your imageView to tranform into. See if it could help:)
Base on your problem, you can see my code, Here I use an animation to change the view back to its identity Transformation:
if (completed && stillReloading) {
//Use this method to let it spring back to its original angle
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
imageView.transform = CGAffineTransformIdentity;
} completion:NULL];
[self performSelector:#selector(animation) withObject:nil afterDelay:0.2];
}
// So if you want to transform it without getting seen, you can simply detele the animation code
if (completed && stillReloading) {
imageView.transform = CGAffineTransformIdentity;
[self performSelector:#selector(animation) withObject:nil afterDelay:0.2];
}
You will find that it is very difficult to get the arrow to rotate continuously clockwise in 180 degree increments. It will be simpler to rotate it in 90 degree increments.
You need to apply new rotation to the arrow's existing transform, to add more rotation each time.
-(void)animateArrowRotation {
[UIView animateWithDuration:0.35 delay:0 options:0 animations: ^{
imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI_2);
} completion: ^(BOOL completed) {
if (completed && stillReloading) {
[self animateArrowRotation];
}
}];
}

Animating a scaling view from a corner (zooming in)

I'm trying to scale my view like so:
CGRect endFrame = self.detailView.bounds;
[UIView animateWithDuration:0.25 animations:^{
self.descriptionButton.bounds = endFrame;
}completion:^(BOOL finished) {
self.containerView.alpha = 0.0;
self.detailView.alpha = 1.0;
}];
My detailView is the container view. My descriptionButton is in the upper left corner. I want to give a zoom in type effect by having the button take up the whole screen. However, by default Core Animation scales from the anchor point at its center.
I tried setting the anchor point to the lower right corner of the view's layer like this:
self.descriptionButton.layer.anchorPoint = CGPointMake(self.descriptionButton.bounds.size.width, self.descriptionButton.bounds.size.height);
CGRect endFrame = self.detailView.bounds;
[UIView animateWithDuration:0.25 animations:^{
self.descriptionButton.bounds = endFrame;
}completion:^(BOOL finished) {
self.containerView.alpha = 0.0;
self.detailView.alpha = 1.0;
}];
But when I do that, I don't see any animation at all. Any thoughts? Thanks.
You can just use a little math
CGRect endFrame = self.detailView.frame;
CGRect currentFrame = self.detailView.frame;
[UIView animateWithDuration:0.25 animations:^
{
self.descriptionButton.frame = CGRectMake(currentFrame.x - (endFrame.size.width - currentFrame.size.width), currentFrame.y - (endFrame.size.height - currentFrame.size.height), endFrame.size.width, endFrame.size.height);
}
completion:^(BOOL finished)
{
self.containerView.alpha = 0.0;
self.detailView.alpha = 1.0;
}];
The anchorPoint property is "normalized" to values from 0 to 1. 0.5, 0.5 is the center. 0,0 is top left. 1,1 is bottom right.
Your code is setting the anchorPoint property using pixel coordinates, which is wrong. I don't know what it would do, but it's wrong.

ios CATransform3DMakeScale scaleup animation

I have the following animation:
CATransform3D scaleTransform = CATransform3DMakeScale(0.1,0.1, 1.0);
scaleAnimation.toValue = [NSValue valueWithCATransform3D:scaleTransform];
keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
where I'm scaling down a subview. But I'm trying to do opposite. start from .01% of the size of the subview and go to 100% of the size of the subview. Does anyone knows how can I do this?
Do this:
yourSubView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//change time duration according to requirement
// animate it to the identity transform (100% scale)
yourSubView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];

iPad - CGAffineTransformMakeRotation - keeping rotation

I have a heads up display (HUD) that I'm trying to get to properly rotate with the view. I created a function orientationWillChange that is supposed to rotate the view and that works fine, just requires a little more programming on the side of the view controller implementing it.
My main problem is that it rotates when it closes. It does some other transformations when it closes (shrinking/fading it so it disappears), but it seems to be rotating it back to "normal".
I don't want to rotate the view another 90 degrees, I just want to make sure it stays in the rotation it is at. How do I do that?
My closing function is as follows:
- (void)close {
_windowIsShowing = NO;
[UIView animateWithDuration:0.125 animations:^{
hudView.transform = CGAffineTransformMakeRotation(rotationDegrees * M_PI / 180);
hudView.transform = CGAffineTransformMakeScale(1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.15 animations:^{
hudView.transform = CGAffineTransformMakeScale(0.4, 0.4);
hudView.alpha = 0.0;
backgroundColorView.alpha = 0.0;
} completion:^(BOOL finished) {
[hudWindow resignKeyWindow];
[self release];
}];
}];
}
(fyi this is based on CloudApp's HUD)

Resources