Animation not Shown When Calling it First Time - iphone

I have one simple animation:
- (void)showMenu
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.scrollView.frame = CGRectMake(296, -150, 432, 356);
[self.buttonMenu setImage:[UIImage imageNamed:#"menu_arrow_up.png"] forState:UIControlStateNormal];
[self.view bringSubviewToFront:self.scrollView];
[UIView commitAnimations];
}
When I call this animation the first time, just the image of my button changes,
the position of my scrollview does not change.
Everything works correctly, after I called my showMenu method twice.
I call showMenu. the image of the button changes. position of scrollview not
I call another method to reset everything. image of button changes. position of scrollview not
I call showMenu. the image of the button changes. position of scrollview changes
from now on it works every time I call showmenu
the second method is closeMenu:
- (void)closeMenu
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.scrollView.frame = CGRectMake(296, -317, 432, 356);
[self.buttonMenu setImage:[UIImage imageNamed:#"menu_arrow_down.png"] forState:UIControlStateNormal];
[UIView commitAnimations];
}
So why does it work just when I click it twice?

I had the same problem when i perform a UIAnimation and simultaneously adding a UITableView programmatically. The frame of UIView changes but it will not update in display.
To solve this problem call your method using performSelector:withObject:afterDelay: with a delay of 0.

If you have autolayout turned on, you may want to wary about changing the frame of a view. Unfortunately, when the constraints are reapplied (which triggered by a myriad of seemingly innocuous actions, such as setting a text label, etc.), your manually set frame will be replaced with values governed by the constraints.
You're probably not seeing this problem manifest itself the second time time you call this routine because you're probably not doing anything immediately thereafter that causes constraints to be reapplied. Even with autolayout on, you can sometime change a frame manually, and it will work, but as soon as constraints are reapplied, the frame may get reset.
There are two solutions:
If using auto layout, you can animate the changing of constraint constants. See the latter part of this answer for an example of how to create an IBOutlet for constraint and then changing the constant property within an animation block.
You can also simply disable autolayout.

Related

Setting UIControl's frame changes view bounds but not where it can receive touches

I have a headerView which can expand and collapse when pressed. It's default state is collapsed.
I use the following code to expand the headerView.
if (!self.headerViewIsExpanded) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self.collapsedHeaderView removeFromSuperview];
self.headerView.frame = self.expandedHeaderView.frame;
[self.headerView addSubview:self.expandedHeaderView];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y + offset, self.tableView.frame.size.width, self.tableView.frame.size.height - offset);
[UIView commitAnimations];
self.headerViewIsExpanded = YES;
}
It correctly expands the view frame and diminishes the tableView frame. However, it fails to expand the touches received area of the original UIControl, so I can only collapse it by pressing where the original frame was.
How can I resolve this issue?
EDIT: Alternatively, I would accept an answer with a good explanation of why I cannot resolve the issue, or at least not resolve it safely.
EDIT 1: To be more clear, the headerView is NOT a tableView header view. It is just a view that sits on top of the table that happens to be called headerView. Both headerView and tableView are subviews of the main UIView that spans the available window.
You can try to overload layoutSubviews method. Inside you should use setFrame method for your headerView to define all view elements position.

Touch issue during UIView Animation

I am animating UIView when user touches on custom dropdown to show picker View from bottom side.UIView contains Pickerview...so when I change it's frame to move upwards.I get what I want, but pickerView doesn't recognize touch !(see screenshot below)
code is like this
CGRect pickerFrame=self.pickerSheet.frame;
CGRect viewFrame=self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:SHEET_ANIMATION_DURATION];
//change Frame of View containing UIPickerView
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;//animated distance is value by which view needs to move upward.
[self.pickerSheet setFrame:pickerFrame];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
screenshot
first of all, ++please**, stop using this animation declaration, and bedin use new, block animation:
[UIView animateWithDuration:SHEET_ANIMATION_DURATION
delay:0
options:nil
animations:^{
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;
}
completion:^(BOO f){
//any actions after animation ended
}];
Second, I think your problem is somewhere else. May by, you implementing your touches or picker wrong.
Please start using the new , better animation block , but I think there is something missing in your code , you need to put the code below :
UIViewAnimationOptionAllowUserInteraction
Put this code in options in the new animation block , or find a way to put it in the old animation block

UIView Animation Problem

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewSettings cache:YES];
viewSettings.alpha = 0;
[viewSettings removeFromSuperview];
[UIView commitAnimations];
I ve written the code above that works well when I add the view via animation, but it doesn't work when i remove the view from superview. Animation works if I remove [viewSettings removeFromSuperview] line. I don't know where I'm doing wrong.
You need to remove it from the superview after the animation has completed. This is very easy to accomplish if you use the blocks based API, which Apple is encouraging you to do:
[UIView transitionWithView:viewSettings
duration:0.30f
options:UIViewAnimationOptionTransitionNone
animations:^{
[viewSettings setAlpha:0];
} completion:^(BOOL finished) {
[viewSettings removeFromSuperview];
}];
You can read about all the options in Apple's documentation.
removeFromSuperview is not an animatable action, so it is getting performed immediately. Once you commitAnimations, your view is no longer part of it's superview, so you can't see the animation, if it is still even happening.
If you want your animation to happen, then the view to get removed, call removeFromSuperview when the animation ends, such as in a selector specified with setAnimationDidStopSelector:.
Try removing view after the animation is completed. Initially alpha value of the view is 1 then, you apply the animation and make it 0. Now the view is still there but it is not visible. Once the animation is over then remove the view. I think it should work.
I think viewSettings is removed before you commit the animation.
Try inverting the two last lines.

How to slide(push) controls down/up in a View?

I have a button which when pressed, should push the controls below it down and show textFields on the space freed. Basically, just changing the position of these controls in an animated way. Any ideas/tips on how to do that?
I tried the code in: How to Make a UITextField Move Up When Keyboard Is Present
But couldn't get it to work just changing the methods. Appreciate any useful information.
Thanks!
I think you have write code for that. If your controls below is a view (BottomView), when you press the button, change the frame of the BottomView, to make room for the textfield view. Now add the textField there. Write the code in a Animation block.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// bottomView.frame = (new frame)
// design a textfield in IB. make outlet.
// textField.frame = (new frame)
[UIView commitAnimations];

Animate the enabling of a UIBarButtonItem?

Is there a way to animate enabling or disabling a button? I've tried the following with no success. I'm guessing at this point that the enabled property cannot be animated like opacity can – but I hope I'm wrong.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
theButton.enabled = YES;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
I can't believe there isn't a setEnabled:(BOOL)enabled animated:(BOOL)animated method.
This is actually really easy. UIView.transitionWithView can take a snapshot of any view and cross-dissolve from the snapshot to the new state of the view. You can use that capability to animate a lot of changes, including enabled.
Just use the View Controller's view as the target view, specify TransitionCrossDissolve, then animate as you would normally.
UIView.transitionWithView(self.view,
duration: 0.5,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { () -> Void in
self.someButton.enabled = !self.someButton.enabled
}, completion:nil)
Animatable properties (through UIView beginAnimations:context:) are: frame, bounds, center, transform, alpha (from Apple docs).
So you've to determine what exactly your animation is (in terms of color/alpha/transform animations) and create manual animation (maybe you'll have to use CAAnimation instead of UIView beginAnimations:context:)
You can break down the enable/disable of a button into a change in opacity between two overlapping images each in one state or the other (i.e. make it look like a fade-in/fade-out). In the animation completion handler you can to do the actual enable/disable toggle.
Here's an idea :)
Within your animation block, Remove the button from view and add it again with disabled state. You can specify what kinda animation you want here..
Set: self.buttonSetEvent.customView.alpha = 0.2;
and try this
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.buttonSetEvent.customView.alpha = 1;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];