Custom UIToolbar resize with animation - xcode

i'm using this piece of code to resize my custom UIToolbar to change width property
[UIView animateWithDuration:0.3 delay:0.3 options:0 animations:^{
self.navToolbar.frame=CGRectMake(0, 0, 200, 30);
} completion:nil];
the code above, change width correctly but without animation,
could anyone tell me why?
Thanks

UIViewAnimationOptionLayoutSubviews (iOS > 4.0) should fix the problem when the size change is not being animated.
[UIView animateWithDuration:0.3
delay:0.3
options:UIViewAnimationOptionLayoutSubviews
animations:^{
self.navToolbar.frame=CGRectMake(0, 0, 200, 30);
}
completion:nil
];

Related

How to stop animation of [UIView animateKeyframesWithDuration] with UIViewKeyframeAnimationOptionRepeat option

I want to add a heartbeat effect to my view , one way to do is use UIView's animation method as follow :
- (void)heartBeating
{
UIView *panel;
NSTimeInterval during = 0.8;
[UIView animateKeyframesWithDuration:during delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeLinear|UIViewKeyframeAnimationOptionRepeat
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.4 animations:^{
panel.transform = CGAffineTransformMakeScale(1.2, 1.2);
}];
[UIView addKeyframeWithRelativeStartTime:0.4 relativeDuration:0.6 animations:^{
panel.transform =CGAffineTransformIdentity ;
}];
} completion:^(BOOL finished) {
}];
}
the problem is : if i want to stop the animation in an action , for example , a Stop butotn tapped , how I can do .
I know I can realize the same effect by create CAKeyFrameAnimation and add it to the view' CALayer .But I want to know how to do with the UIView's animation method. Thanks.
Try
[panel1.layer removeAllAnimations];
Hope this will help you.

Button fires on double not single click

I got view animations to work perfectly in Xcode (slide off the screen on a button click with CGRect.) Unfortunately I have to click the button twice to execute the animation. What have I done wrong? I appreciate any feedback
Here's my .H code
#interface AnimationBlocksViewController : UIViewController{
IBOutlet UIView *theview;
IBOutlet UIView *theview2;
BOOL isAnimated;
BOOL switchback;
}
-(IBAction)animate:(id)sender;
-(IBAction)change:(id)sender;
Here's my .M code
-(IBAction)animate:(id)sender;{
if (isAnimated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[theview setFrame:CGRectMake(0, 0, 320, 460)];
[theview2 setFrame:CGRectMake(320, 0, 320, 460)];
[UIView commitAnimations];
isAnimated=NO;
}
else{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[theview setFrame:CGRectMake(-320, 0, 320, 460)];
[theview2 setFrame:CGRectMake(0, 0, 320, 460)];
[UIView commitAnimations];
isAnimated=YES;
}
}
-(IBAction)change:(id)sender;{
if (switchback) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[theview2 setFrame:CGRectMake(0, 0, 320, 460)];
[UIView commitAnimations];
switchback=NO;
}
else{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[theview2 setFrame:CGRectMake(320, 0, 320, 460)];
[theview setFrame:CGRectMake(0, 0, 320, 460)];
[UIView commitAnimations];
switchback=YES;
}
}
It works but I have to double click to trigger the animation, what have I done wrong?
Thank you
After your initial double click, does a subsequent single click trigger the animation properly? If so, it may be that you haven't explicitly initialised your BOOL variables, though they should default to NO. Try doing that and see if it helps.
If you always need to do two taps to animate, maybe try inserting a call to the run loop to see if it's only processing the animation after running on your application thread for a while i.e. it could be that only when it comes to process a second click event does it actually go and fetch any pending animations and execute them. The code for that would be (after you set isAnimated and call commitAnimations):
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate date]];
Not sure whether you can just use nil for a date but try that too if you like.

Xcode: Why does my flip animation flip twice?

I'm having a little problem. I have an UILabel which have an UILongPressGestureRecognicer. When the UILongPressGestureRecognizer is called my app is supposed to switch to a new view using a flip animation.
This is the code I have used for the GestureRecognizer:
UILongPressGestureRecognizer *labelLongPressRecognizer;
labelLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LoadLabelSettings:)];
labelLongPressRecognizer.numberOfTouchesRequired = 1;
labelLongPressRecognizer.minimumPressDuration = 2.0;
[NewLabel addGestureRecognizer:labelLongPressRecognizer];
and this is the code for the view switching animation:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[self.view addSubview:LabelSettingsViewController.view];
[UIView commitAnimations];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
LabelSettingsViewController.view.frame = CGRectMake(0, 0, 480, 300);
}
My problem is that when I hold down on my UILabel the switch animation begins, but when I release it repeats the animation again. So basically the animation occur twice and I only want it to take place once.
Any ideas?
Thanks in advance :)
Are you checking the sender state, e.g.,
- (void)LoadLabelSettings:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) // or whatever
// then do the flipping stuff
}
Check out the "Overview" of the "UILongPressGestureRecognizer Class Reference", which talks about the long press being continuous and I presume numerous events could be triggered:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILongPressGestureRecognizer_Class/Reference/Reference.html

UIWebView and Animation

I am wondering if there was a way to animate the size and position of a UIWebView.
If I do sth like this:
[UIView animateWithDuration:1.0 animations:^{
self.frame = CGRectMake(20.0, 20.0, 80.0, 80.0);
}];
or
[UIView beginAnimations:#"zoomIn" context:nil];
[UIView setAnimationDuration:2.75];
[UIView setAnimationDelegate: self];
self.frame = CGRectMake(20.0, 20.0, 80.0, 80.0);
[UIView commitAnimations];
while there is a grey box doing the animation,
the content of the WebView instantly switches to the new size without animating.
(the scalesPageToFit property is set to YES)
any suggestions on that?
Haven't cracked the instant changing of the text width yet but you can get rid of the grey box by programmatically changing the background of the UIWebView to clearColor. Doesn't work if you do it through Interface Builder, for some reason.

animated UIButtons are clickable at destination before they reach destination

I have a UIView that contains a couple UIButtons that I am animating from offscreen to onscreen. I find that the region where they are heading is clickable before they reach it. The animation is pretty simple, so I'm wondering if there is something obvious that I am missing in terms of telling the code to not treat the button as if it is at the final destination (I'm not sure if that is supposed to be the expected behavior and the animation is purely a visual while the clickable region is instantaneously at the destination; I wouldn't expect it to be).
Here is the code I use to animate it. It's basically switching out a sub panel and bringing back a main panel with buttons:
// switch back to main abilities panel
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: kFadeInTime];
CGRect rect = CGRectMake(
480.0f,
mAbilities.mSubPanel.frame.origin.y,
mAbilities.mSubPanel.frame.size.width,
mAbilities.mSubPanel.frame.size.height);
mAbilities.mSubPanel.frame = rect;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: kFadeInTime];
[UIView setAnimationDelay: kFadeInTime];
rect = CGRectMake(
kAbilitiesBorderX,
mAbilities.mPanel.frame.origin.y,
mAbilities.mPanel.frame.size.width,
mAbilities.mPanel.frame.size.height);
mAbilities.mPanel.frame = rect;
[UIView commitAnimations];
As a workaround you can disable user interaction with your panel before animations and reenable it when animation finishes:
// Animation compete handler
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
mAbilities.mSubPanel.userInteractionEnabled = YES;
}
// Animating panel
mAbilities.mSubPanel.userInteractionEnabled = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: kFadeInTime];
[UIView setAnimationDelegate: self];
CGRect rect = CGRectMake(
480.0f,
mAbilities.mSubPanel.frame.origin.y,
mAbilities.mSubPanel.frame.size.width,
mAbilities.mSubPanel.frame.size.height);
mAbilities.mSubPanel.frame = rect;
[UIView commitAnimations];
If you're targeting iOS4 you can (and as specs say should) use block-based animation api:
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews
animations:^(void){
CGRect rect = CGRectMake(
480.0f,
mAbilities.mSubPanel.frame.origin.y,
mAbilities.mSubPanel.frame.size.width,
mAbilities.mSubPanel.frame.size.height);
mAbilities.mSubPanel.frame = rect;
}
completion:NULL
];
While animating using blocks user interaction is disabled by default - you can enable it by setting UIViewAnimationOptionAllowUserInteraction flag in options parameter:
... options:UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionAllowUserInteraction ...

Resources