UIView Animation iPhone (obtaining information on the frame dynamically) - iphone

I have a UIView called goalBar which is being animated by increasing the size of the frame according to a float value called destination:
CGRect goalBarRect = CGRectMake(0, 0, destination, 29);
[UIView beginAnimations:#"goal" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
goalBar.frame = goalBarRect;
[UIView commitAnimations];
The animation works perfectly and the rectangular view increases its width over the course of the animation (from 0 to the value for destination).
However, I wish to be able to extract the values for the frame of the UIView being animated (i.e. goalBar) as the animation takes place. By that I mean that I wish to place the value of the width for the animated frame in a separate UILabel so that the user sees a counter that provides the width of the UIView as it's being animated.
Any help on how to do the above would be gratefully received.

How about observing the frame property of the view. So that you get callbacks when it changes.
See Key-Value Observing.

I've looked into this and it seems that it is not possible to get updates on the state of the components being animated during the UIView animation process.

Related

Animation not Shown When Calling it First Time

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.

Animating UIView frame size change with autoresizing subviews

I have a UIView which contains two subviews. I would like to change the superview's frame size with an animation and have the subviews' sizes change in accordance with their autoresize masks.
[UIView beginAnimations:#"Resize" context:nil];
[UIView setAnimationDuration:1.0];
CGRect frame = self.myView.frame;
frame.size.height += 30.0;
self.myView.frame = frame;
[UIView commitAnimations];
The view self.myView is resized over 1 second as expected, but the subviews are resizing immediately. Does anyone know why this is happening and how I might make the subviews animate their resizing as well?
From my googling around, I think it might have something to with the contentMode property. Unfortunately, I'm not really clear as to what that property does. Any help would be much appreciated.
It would have been hard for someone else to give you the correct answer w/o seeing your code though I would have asked you what you are doing after [UIView commitAnimations]. Nevertheless, glad you figured it out. I suggest you use the block animations. They make this type of mistake much easier to avoid by using a completion block. Example:
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.myView.frame;
frame.size.height += 30.0;
self.myView.frame = frame;
}
completion:^(BOOL finished){
// whatever you need to do when animations are complete
}];
OK, so I figured out the issue after reading the answer to the following question: Animating a UIView frame, subview UIScrollView doesn't always animate
I basically was doing some work after I scheduled the animation which was causing -layoutSubviews to be called on each of my subviews which automatically caused those views to be arranged at the end point of the animation. By scheduling my animation after this work was done I was able to solve my problem.

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

Simulate push view animation inside UIView

I have a single UIView with some labels that display attributes from objects inside an array. I'm using gesture recognizers to change the current element in the array and hence the text in the labels. My question is how do I mimic a push animation without actually pushing a new view controller. Thanks for your help.
Thats simple way to use CGAffineTransformMakeTranslation to move any view from X axis to Y axis using UIViewAnimation. Before using this transition set your object frame to some x,y axis(0,0) and pass your x and y to move from.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x,y);
YOUR ViewOBJECT.transform=transform;
[UIView commitAnimations];
Have you tried to use the UIView class methods for animation? For example this could help:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
In the animations block you could move one view out and another in.

Animated stretch of an image frame (iPhone)

So I'm trying to animate an image stretching from a zero width to full size while fading in. The fade in comes across fine, but for whatever reason the thing appears at the full frame at the start of the animation, instead of growing as the alpha effect happens. My code is as follows:
[UIView beginAnimations:#"FadeButtons" context:nil];
[UIView setAnimationDuration:.9];
[UIView setAnimationDelay:0.300];
batteryLife.alpha = 1;
batteryLife.frame = CGRectMake(40, 71, 240, 128);
[UIView commitAnimations];
Any advice on changing a frame in an animated fashion? I don't want to scale the entire image, I want to "unveil" it. Thanks.
Perhaps the frame is already that size before beginAnimations? Try setting the frame immediately before beginAnimations to the smaller size, like batteryLife.frame = CGRectMake(40, 71, 0, 128);