ImageView Animation Repeatition - ios

I am working on an application which needs animations with imageView.
At starting the position of image is at CGPoint(735,112),on clicking the animation button it has to move to the left of the screen ie to point CGPoint(20,112) and hast to repeat the animation from left to right i.e to point CGPoint(735,112),has to stop after this. i am using the following Code.
CGRect newRect=cleanStrip.frame;
[UIView beginAnimations:#"animation1" context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:YES];
newRect.origin.x=20;
cleanStrip.frame=newRect;
[UIView commitAnimations];
After repeating the animation the image view has to stop at it original position CGPoint(735,112).But the above code stops the image at CGPoint(20,112).
How can i stop the image at its original position after animation.
Thanks,

you can use block to start your animation
__block CGRect newRect=cleanStrip.frame;
__block int originx = newRect.origin.x;
[UIView animateWithDuration:2.0
animations:^{
newRect.origin.x=20;
cleanStrip.frame=newRect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0
animations:^{
newRect.origin.x=originx ;
cleanStrip.frame=newRect;
}];
}];

Related

Bouncing animation while changing view frame

Add bouncing animation while changing UIVIew frame. I only know how to change frame but I don't know how to set bouncing effect while changing view frame. Below is my code....
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
CGRect frame = viewContent.frame;
frame.size.height = CGRectGetHeight(viewContentFullDetail);
viewContent.frame = frame;
} completion:^(BOOL finished) { }];
You can check animated gif image on below link. I want to add animation not same like this but near to this. Main animation are unfolding and bouncing effect.
https://drive.google.com/open?id=0B9k_Shyb5v62eFdxWXhYeXV3a0E
Please help. I don't know how to do this.
You need to try this
int duration, damping, velocity;
[UIView animateWithDuration:duration delay:0
usingSpringWithDamping:damping initialSpringVelocity:velocity
options:0 animations:^{
// your animation code here
} completion:nil];
Play with the damping and velocity values to know more about this feature in the animation code.
here is the link for a tutorial

iOS how to animate view scaling from the center of the view?

I'm using an affine transform within a repeating/reversing animation to create a "pulsing" effect. However, I noticed that the button gets shifted right when the animation begins. The anchor point for the button is at (0.5,0.5):
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionRepeat|
UIViewAnimationOptionAutoreverse|
UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationCurveEaseInOut
animations:^{
self.recordButton.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
}];
It appears that when the transform is applied, the origin of the button's frame stays at the same spot.
How can I animate scale transform so the center of the button remains in the same spot once the transform is applied?
I usually play with the frame and CGRect utility functions
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionRepeat|
UIViewAnimationOptionAutoreverse|
UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationCurveEaseInOut
animations:^{
self.recordButton.frame = CGRectInset(self.recordButton.frame, -1.2, -1.2);
} completion:^(BOOL finished) {
}];

Showing UIView with bouncy animation

I want to create a bouncy animation like in the Skitch application - when i press a button, a subview (simple view with several buttons) will appear from the side and will bounce a bit until it stops.
I can't find any standard animation that does that, so how i can implement this animation?
You can try this :
// Hide your view by setting its position to outside of the bounds of the screen
// Whenever you want, call this :
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your final view position here
}
completion:^(BOOL finished) {
}];
This method is the same as the usual animateWithDuration: but it introduces a spring animation.
Use UIView method:
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
usingSpringWithDamping:(CGFloat)dampingRatio
initialSpringVelocity:(CGFloat)velocity
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
This will do what you want
Use the UIView method, If you want bouncing effect. Then you need to nested the animation process and give the frame as needed for that view. Here's the example:
CGRect frameOnStart = CGRectMake(200,200 ,60 ,60); //On start show small view
CGRect frameOnAnimate = CGRectMake(80,80 ,150 , 150); //while animate show large view
CGRect frameAfterAnimate = CGRectMake(100,100,120 ,120); //after animate large take it back to original frame. SO it loos like bouncing animation
// Whenever you want, call this :
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your on Animate position here
view.frame = frameOnAnimate;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your final view position here
view.frame = frameAfterAnimate;
}
completion:^(BOOL finished) {
}]
}];
Thanks.

Cannot stop Animation

I am using the following code to make text in a label blink :
- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UILabel *) target {
NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:#"EffectSpeed"];
float speedFloat = (0.50 - [selectedSpeed floatValue]);
[UIView beginAnimations:animationID context:(__bridge void *)(target)];
[UIView setAnimationDuration:speedFloat];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(blinkAnimation:finished:target:)];
if([target alpha] == 1.0f)
[target setAlpha:0.0f];
else
[target setAlpha:1.0f];
[UIView commitAnimations];
}
and I am using the following code to make the animation Stop :
- (void) stopAnimation{
[self.gameStatus.layer removeAllAnimations];
}
Although the animation works fine, I cannot stop it.
Could you please help!
Thanks in advance....
The problem is that your animationDidStopSelector is being called when you manually stop your animation, but that method is just starting another animation. So you're stopping it ok, but you're immediately firing off another animation.
Personally, I'd suggest getting rid of the animationDidStopSelector and use the autoreverse and repeat features of the animation:
[UIView beginAnimations:animationID context:nil];
[UIView setAnimationDuration:speedFloat];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:CGFLOAT_MAX];
[target setAlpha:0.0f];
[UIView commitAnimations];
That should fix it, but as holex said, you should use block animation. I quote from the documentation: "Use of this method [beginAnimations] is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead."
So, the equivalent block animation would be:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
self.gameStatus.alpha = 0.0;
}
completion:nil];
Whichever technique you use, your removeAllAnimations will now work as expected.
As an aside, when you stop animations, it will set alpha immediately to zero. It might be more graceful to stop the repeating animation and then animate the alpha from the current value to your desired final value. To do that, you'll want to grab the current opacity from the presentation layer and then animate alpha from that to whatever you want it to stop at (in my example 1.0, but you could use 0.0, too):
CALayer *layer = self.gameStatus.layer.presentationLayer;
CGFloat currentOpacity = layer.opacity;
[self.gameStatus.layer removeAllAnimations];
self.gameStatus.alpha = currentOpacity;
[UIView animateWithDuration:0.25
animations:^{
self.gameStatus.alpha = 1.0;
}];

How to animate overlay drawing in MKMapView

I'm drawing MKCircleView overlay on MKMapView. it works fine, except that I need to animate drawing of this overlay.
I know that I need to use:
[UIView beginAnimations:nil context:NULL];
[UIView commitAnimations];
,but I don't know where to put this animation code.
Thanks.
not sure what you're looking for.. but for example:
[UIView beginAnimations:#"fade" context:nil];
[UIView setAnimationDuration:0.6f];
theView.alpha = 0.0f;
theView.transform = CGAffineTransformMakeScale(0.01,0.01);
theView.alpha = 1.0f;
[UIView commitAnimations];

Resources