UIView's animations stopped when adding a subview - ios

Here is the view that I created using Storyboard:
Clouds, avatar image button, and other text buttons, etc. are its subviews. In its viewDidAppear method, I added several animation methods which cause the clouds to "float" horizontally, for example:
[UIView animateWithDuration:4.0f
delay:0.0f
options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction)
animations:^{
_cloud1.frame = its new frame so that it looks to float horizontally;
}
completion:nil];
Now I have another subview which I created using a XIB file. It's basically a custom-sized subview which will appear when the avatar image button is tapped (we can think of it as "editing profile screen" in social apps). I added it with this animation block (please note that it justs appear on top of the purple circle, which is actually not removed):
_editProfileViewController = [[EditProfileViewController alloc] initWithNibName:nil bundle:nil];
[self addChildViewController:_editProfileViewController];
[_editProfileViewController didMoveToParentViewController:self];
[UIView transitionWithView:self.view duration:0.3 options:(UIViewAnimationOptionTransitionCrossDissolve) animations:^
{
[self.view addSubview:_editProfileViewController.view];
} completion:NULL];
Problem: When I do that, all clouds stop animating (while the transition still works).
Any ideas? Thank you.

After hours banging my head against the wall, it seems that this is an iOS simulator bug. Everything works ok on device.
Note for anyone who faces the same problem:
OS: Mavericks 10.9.4
Xcode: 5.1.1
iOS Simulator target: iOS 7.1

Related

UIView animateWithDuration not animating in ios8

I have implemented a custom segue class for emulating the push transition without navigation bar. The trick is to take to snapshots, add them to the view, replace the viewController, move the snapshots, and finally remove them. This emules a horizontal movement of the viewController, but actually only two UIImagesView are moved.
The following code implements this.
self.destinationImageView.frame = offsetFrameD;
self.sourceImageView.frame = offsetFrameS;
//ViewController replacement
[self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];
//Overpose the snapShot who will simulate the transition between vies.
[destinationView addSubview: self.sourceImageView];
[destinationView addSubview: self.destinationImageView];
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.destinationImageView.frame = self.finalFrameD;
self.sourceImageView.frame = self.finalFrameS;
}
completion:^(BOOL finished) {
[self.sourceImageView removeFromSuperview];
[self.destinationImageView removeFromSuperview];
}];
This code worked with iOS 7. However, when using iOS 8, it seems like the animation is not performed. The destinationImageView and sourceImageView are directly moved to the finalPosition (WITHOUT ANIMATING) and the completion block is not called, so both UIImageView are not finally removed.
Does anyone knows how the animation should be performed with iOS 8?
You should adjust the auto layout constraints and not the frames position. Have a look at this solution. I hope it helps.
UIView transitionWithView & setting the frame no longer works iOS8
Use the following line of code before the start of the animation:
[UIView setAnimationsEnabled:YES];

CGAffineTransformMakeScale animation not working

I have a viewcontroller with a view that I am dismissing using a UIView animation to scale it down to 0 before removing it. My code for dismissing it is:
[UIView animateWithDuration:_dismissAnimationDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^(void) {
_menuContainerView.transform = CGAffineTransformMakeScale(0.0, 0.0);
}
completion:^(BOOL finished){
if ([_delegate respondsToSelector:#selector(popUpMenuDidClose)])
{
[_delegate popUpMenuDidClose];
}
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
That works perfectly when building from XCode 5 onto devices running both iOS 7 and iOS 8. But, as soon as I build to iOS 8 from XCode 6 (beta 6 and beta 7) the view just cuts away instead of animating. If that wasn't weird enough as soon as I change the target scale to (0.001, 0.001) it animates fine regardless of XCode version. Any ideas as to why I can't animate to an actual (0.0, 0.0) scale with XCode 6?
So after speaking with a developer at Apple the reasoning I got back was that some base frameworks need to work with the inverses of transform matrices quite often, and since there is no inverse for the zero matrix the animation just returns out to avoid crashing. Hopefully this post has helped others who ran into a similar situation.
If acceptable for you, set the scale values to 0.01 like so:
_menuContainerView.transform = CGAffineTransformMakeScale(0.01, 0.01);
Reference

Constraint animation not working on device but on simulator

Im using a UIView animations to move up/down a view (Using Autolayout) .
Animation works on simulator but it doesn't on actual device. What could be the problem? Code to do the animation is here
[self layoutIfNeeded];
[UIView animateWithDuration:.5 animations:^{
self.animationConstraint.constant = -190;
[self bringSubviewToFront:self.containerView];
[self.containerView layoutIfNeeded];
}];
I have checked the memory usage, but it doesn't cross more than 2% at the time of doing the animations.
Is the simulator device the same like the actual device in regard to iOS and display size?

UIView, very slowly animating background color, hangs scroll view (!)

Here's one, when a certain screen arrives,
[UIView animateWithDuration:20 animations:^
{ self.view.backgroundColor =
[UIColor colorWithHue:0 saturation:0 brightness:0.97 alpha:1];
}];
I want to very slowly, take it from white to soft white.
Bizarrely - if you add this to a view, it crashes any scroll views (table views, collection views) you happen to have there, I think on that view or indeed just anywhere on screen. (The slow fade works fine!)
Try it on any fullscreen view.
Has anyone noticed this? Thoughts? Cheers
PS just incidentally, very slowly going to off-white for the whole screen is a great design look, particularly on retina iPad. (Breaking any tables is not so good though :O )
Here's an example of rdelmar's tip!
[UIView animateWithDuration:1
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{ c.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.5];}
completion:nil
];

CGAffineTransformRotate shifts object before performing rotation

I am trying to animate a transform of a UIButton with CGAffineTransformRotate, and while it is performing the animation properly, it shifts the button about 15 pixels down and to the left before doing it. Here's the code performing the animated transformation:
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
self.addCloseButton.transform = CGAffineTransformRotate(self.addCloseButton.transform, degreesToRadians(45));
}
completion:nil];
When I reverse the transformation it does the same thing except it shifts it back to its original position before animating (15 pixels up and 15 pixels to the right), and I do that with this code:
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
self.addCloseButton.transform = CGAffineTransformIdentity;
}
completion:nil];
Why would this shift occur? The button was created using interface builder, and the shift happens immediately even if I set the animation duration higher or add a delay.
I figured it out: turns out having "Use Autolayout" selected on my xib (which adds a bunch of auto constraints) messes things up when trying to use transforms. Turning it off fixed my problem.
It is posible to fix this while still using auto layout and storyboards. See my answer on this question: https://stackoverflow.com/a/19582959

Resources