Animating UIView frame size change with autoresizing subviews - iphone

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.

Related

Animate fade-out for drawings

I have a view where the user can draw. What I want is that, the strokes that were drawn by the user must slowly fade-out in the order that they were drawn. Any advice ?
JYou can try pluging in a AnimationFadeOut and also set a duration to it. Here is a code snippet for that. Let me know if this helps you out.
-(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait
{
[UIView beginAnimations: #"Fade Out" context:nil];
// wait for time before begin
[UIView setAnimationDelay:wait];
// druation of animation
[UIView setAnimationDuration:duration];
viewToDissolve.alpha = 0.0;
[UIView commitAnimations];
}
Rather than uiview you can use your action to fade out.
For what you're asking, you could simply add a stroke as a subview, then use a cross fade transition.
[drawingView addSubView:strokeView];
[UIView transitionWithView:strokeView duration:0.2f options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:NULL];
For something like this (showing a drawing being completed), I would personally capture the touch points and time offsets to animate the drawing of each point at the speed of the drawer. More code, but a stylistic choice.

CGAffineTransform and scaling to center of the image

I started studying objective-c using the iPhone and iPad apps for Absolute Beginners by Rory Lewis book but i got stuck on the 5th chapter.
I want to make a button that shrinks an image.
My problem is, that after I wrote all the code, the image shrinks to the point (0, 0) of the UIImageView (the top left), while in the video the image shrinks to its center. I've done some research and found out that CGAffineTransform uses the center of the object to make translations, rotations etc.
So why does it not work in my case?
I have the latest Xcode version and haven't done anything strange.
I hope I've been clear. Sorry for my English.
EDIT
Sorry for the code, but i wrote the question from my phone.
Anyway the incriminated part goes something like this
- (IBAction)shrink:(id)sender {
if(hasShrink == NO){
[shrinkButton setTitle:#"Grow" forState:UIControlStateNormal];
}
else if(hasShrink == YES){
[shrinkButton setTitle:#"Shrink" forState:UIControlStateNormal];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
myIcon.transform = CGAffineTransformMakeScale(.25, .25);
hasShrink = YES;
[UIView commitAnimations];
}
All the animations are correctly written and the app does work, it just shrinks to the top left. Why is the point not set to the center of the UIImageView by default like it should be?
EDIT:
I figured out it was a problem caused by AutoLayout.
Once disabled everything went smooth ;)
If you are transforming a view mangled managed by auto-layout, you may experience some strange side-effects. See this answer for more details.
The solution I ended up employing was to encapsulate the view I wanted to transform inside another view of exactly the same size. The outer view had the appropriate layout constraints applied to it while the inner view was simply centered in its container. Applying a CGAffineTransform scale transform to the inner view now centers properly.
Old question... but just in case others come looking:
The CGAffineTransform acts (rotates, scales) around an anchorPoint.
If you are sizing to 0, 0 then your anchor point is set to 0, 0. If you want it to size to a different point, such as the center of the image, you need to change the anchor point.
The anchor is part of a layer
So if you have a UIImageView called imageview, you would make a call like this to set the anchor to the center of imageview:
[imageview.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
Try something like this:
CGAffineTransform t = CGAffineTransformMakeScale(0.5, 0.5);
CGPoint center = imageView.center; // or any point you want
[UIView animateWithDuration:0.5
animations:^{
imageView.transform = t;
imageView.center = center;
}
completion:^(BOOL finished) {
/* do something next */
}];
Next time show your code. It will be easier to help you.
Check this project: https://github.com/djromero/StackOverflow/archive/Q-13706255.zip
You must study how autolayout and autoresize affect your views. In the project above both are disabled to let you see how it works.

Animate size of a UIView around center point

I have a UIView which I want to grow from it's center when a user touches it. The problem is that when animating it the view expands left and then moves to the right, whereas I want it to expand to the left and right, while keeping the center point the same.
This is the code I have at the moment:
[UIView animateWithDuration:1 animations:^(void) {
[view setFrame:CGRectMake(-10, 0, 320, view.frame.size.height)];
}];
I didn't think it was going to be difficult to do this, but it seems it is. Short of animating it manually with a timer I have no idea how to get it to expand from it's center.
I am not quite sure why it's working for you in a weird manner. Did you alter the anchorPoint property in any way? Otherwise it should grow from center.
Does doing
CGRect newFrame;
newFrame = CGRectInset(view.frame, -10, -30);
[UIView animateWithDuration:1 animations:^(void) {
view.frame = newFrame;
}];
also give you the same result? What about this?
[UIView animateWithDuration:1 animations:^(void) {
view.transform = CGAffineTransformScale(view.transform, 1.1, 1.1);
}];
Try the code given by #Deepak Danduprolu and also don't forgot to UnCheck the "Use AutoLayout" checkbox in the show file inspector window of the storyboard.
It works for me.
Just offset the X,Y position of the view. Lets say you're DECREASING both the width and height of the frame by 20 points: you should then ADD 10 points to both the X and Y position of the frame.

Why uiscrollview doesn't make visible the contentOffset change?

Hello everyone I have a problem with a uiscrollview.
When I press a button to change the contentOffset scollview through a UIView animation so I have something like this:
NSLog(#"%#\n %f %f", scroll, scroll.contentOffset.x, scroll.contentOffset.y);
[UIView beginAnimation:#"anima" context:nil];
[UIView setAnimationDuration:0.5];
scroll.contentOffset = CGPointMake(0,index * scroll.frame.size.height;
[UIView commitAnimations];
NSLog(#"%#\n %f %f", scroll, scroll.contentOffset.x, scroll.contentOffset.y);
Making a log cotentOffSet before and after these instructions is changed and obviously the scrollview is an existing instance so it is different from nil.
The problem is that the screen does not change the offset scrollview that is not moving.
I do the same thing in other parts of the code and other scrollview but it works perfectly.
I also tried:
[scroll setContentOffset:CGPointMake(0, index * currentVerticalScrollView.frame.size.height) animated:YES] ;
but does not change too!
Why isn't it work?
Have you ensured to invoke setContentSize: on scroll originally, large enough to allow for the scroll offset?
If so, it may need a [scroll setNeedsDisplay] to give it a kick!

UIView Animation iPhone (obtaining information on the frame dynamically)

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.