How to animate overlay drawing in MKMapView - ios

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];

Related

Changing UIViews height goes to the bottom but should go up

I want to do a bar-graph animation. Where the bars go up when the view loads. Here is what I do.
CGRect bar1Frame = self.rootView6.bar1.frame;
bar1Frame.size.height = 181;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.rootView6.bar1.frame = bar1Frame;
[UIView commitAnimations];
The problem is that the bars height goes to the bottom of the view instead of going to the top.
Anyone can help me?
You need to update the y origin of the bar1Frame as well with
bar1Frame.origin.y = bar1Frame.origin.y - 181;
bar1Frame.size.height = 181;
and apply the animations as you did. Doing so will show your bar above the horizontal line of the graph.
You need to change the y-position of the UIView.
Eg: if you need to change height to 100 then
bar1Frame.origin.y = bar1Frame.origin.y-100;
CGRect bar1Frame = self.rootView6.bar1.frame;
bar1Frame.size.height = 181;
bar1.Frame.origin.y -= bar1Frame.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.rootView6.bar1.frame = bar1Frame;
[UIView commitAnimations];

Resize an image but never came back to original size

I'm trying to resize an image (tiny and return to original size). This code is working for resize the image but never go back to the original size. I thin I miss something in the endItem1Animation code. Any suggestions? Thanks
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self endItem1Animation];
This is the endItem1Animation code:
// Item 1 End Animation (Return to Original Size)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:5.0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(50,50);
image.frame = newRect;
[UIView commitAnimations];
The problem is that when you call beginAnimations / endAnimation, this does not block the main thread from running, so calling [self endItem1Animation] sets the second animation which will prevent the first animation from running.
You'll want to use the UIView animation block method instead, which lets you supply a completion handler when the animation completes.
[UIView animateWithDuration:0.5 animations:^{
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
} completion:^(BOOL finished) {
[self endItem1Animation];
}];
Another, more straightforward issue, is that the duration for the first animation is zero.
I fix this issue running a selector at the end of the code. Now my code is working. Here is my final code, I Hope to help someone. Thank you for your suggestions.
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self performSelector:#selector(endItem1Animation) withObject:nil afterDelay:8.0];

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;
}];

ImageView Animation Repeatition

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;
}];
}];

removing UIView Animation acceleration

How does one remove the acceleration and deceleration animation that UIView Animations do?
If you don't understand what I mean, when an animation starts up it accelerates and then when it get to the end, it decelerated. I want the animation to be a constant speed.
Here is an example of what my code looks like (I MUST use the old animation ways).
-(void)animate:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:#selector(animateStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:3];
Motion.center = (*PathPoints)[Location];
[UIView commitAnimations];
}
[UIView setAnimationCurve: UIViewAnimationCurveLinear];

Resources