UIView block animation not working - ios

I have a custom view (which draws callouts/bubbles) that I currently add as a subview to a UIImageView. It works as intended but I would like to animate the operation with a spring-like effect. I am using the below code but it does not work (the block gets executed but without animation):
/* ViewController */
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
bubbleView.hidden = NO;
[self.view layoutIfNeeded]; // tried this
}
completion:nil];
I have tried to animate both the hidden property as well as the alpha properties but same result. I have also tried animating the addSubview method, no difference. I started out with a more simple animation as proof of concept but that didn't work either.
The above code is executed within a method which is called during the (void)viewDidAppear:(BOOL)animated cycle. Does this have anything to do with this in that the animation is being executed during the main thread? I have read something to that effect but unsure. Also, I am using auto layout for the UIImageView but didn't think that would matter in this case since the animation is being applied to a custom subview of the UIImageView.
Any help appreciated.

Better use alpha.
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.alpha = 0;
// bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
// bubbleView.hidden = NO;
bubbleView.alpha = 1;
//[self.view layoutIfNeeded]; // tried this
}
completion:nil];

Try with this.
UIButton *catchButton = (UIButton *)sender;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut//UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |
animations:^{
catchButton.alpha = 0.4;
catchButton.enabled = NO;
}
completion:NULL];

Related

Animating the expansion of a UICollectionViewCell's content upon selection

How to animate the expansion of a UICollectionViewCell's content upon selection?
Currently I'm using transition animation on my didSelectItemAtIndexPath for animating a view Which isn’t working smoothly like AppStore card animation.
Here is my current code...
AnimateStaticImageViewController *animateImageVC = [[AnimateStaticImageViewController alloc] init];
animateImageVC.modalPresentationStyle = UIModalPresentationFullScreen;
animateImageVC.modelImage = [UIImage imageNamed:text];
[UIView animateWithDuration:0.7 animations:^{
animateImageVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.3);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.7 animations:^{
animateImageVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.7 animations:^{
animateImageVC.view.transform = CGAffineTransformIdentity;
}];
}];
}];
[self presentViewController:animateImageVC animated:NO completion:nil];
So, I was trying a modal transition animation in my app's home page staticImageCollectionView immediately after clicking the image.
The core animation & transform mechanism didn't work for me as I need something like AppStore's card animation.
Using this library worked after some hassle! Following this...
Setting the library with following steps.
Calling the transitioningDelegate of the animationViewController on didSelectItemAtIndexPath (or prepareForSegue if working with segue-navigation controller).
Calling it within a completion block to ignore flickering & delay issue in view presentation.
AnimatedStaticImageViewController *animateImageVC = [[AnimatedStaticImageViewController alloc] init];
animateImageVC.modalPresentationStyle = UIModalPresentationFullScreen;
animateImageVC.modelImage = [UIImage imageNamed:text];
[UIView animateWithDuration:0.2
animations:^{
animateImageVC.transitioningDelegate = self;
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
[self presentViewController:animateImageVC animated:YES completion:nil];
}];
Be careful when setting imageView which is the main presentation of the animation.
Make sure you declaring the delegate 'RMPZoomTransitionAnimating' & 'RMPZoomTransitionDelegate' methods on both fromViewController and toViewController.

Making Menu coming From Left Corner in iOS

I want to make an Menu Animation just as shown below.
I need to have UIButtons inside the menu.
How can i achieve this.
for this animation you should set anchor point by default the anchor point position is (0.5,0.5), so can change the anchor point position:
All geometric manipulations to the view occur about the specified point. For example, applying a rotation transform to a layer with the default anchor point causes the layer to rotate around its center. Changing the anchor point to a different location would cause the layer to rotate around that new point.
self.view.layer.anchorPoint = CGPointMake(0,0);
self.view.transform = CGAffineTransformMakeScale(0.001,0.001);
[UIView animateWithDuration:1.0 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
Please add the follow code on button click:
[UIView animateWithDuration:1
animations:^{
myView.transform = CGAffineTransformMakeScale(1.5, 1.5); }
completion:^(BOOL finished) {
[UIView animateWithDuration:1
animations:^{
myView.transform = CGAffineTransformIdentity;
}]; }];
It will work for you.
Create Super class SuperViewController.m
Add this method
- (void)viewDidLoad {
[super viewDidLoad];
//Here u can set image also in navigation bar
UIBarButtonItem *buttonC = [[UIBarButtonItem alloc]initWithTitle:#"Word" style:UIBarButtonItemStylePlain target:self action:#selector(GeTVAlue)];
[self.navigationItem setLeftBarButtonItem:buttonC];
}
-(void)GeTVAlue{
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(-350, -350, 350, 350)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
[UIView animateWithDuration:1
animations:^{
paintView.transform = CGAffineTransformMakeScale(2.5, 2.5); }
completion:^(BOOL finished) {
[UIView animateWithDuration:1
animations:^{
paintView.transform = CGAffineTransformIdentity;
}]; }];
}
Then Inherit your Super class in your class
#interface TheerdViewController : SuperViewController
If u have any question fill free ask me.
Thnak you

UIView doesn't animate

I want to animate the center of a UIView, and I did this in viewDidLoad:
_test.center = CGPointMake(100.0,100.0);
[UIView animateWithDuration:10.0
delay:5
options:UIViewAnimationOptionCurveLinear
animations:^{_test.center = CGPointMake(100.0,-100.0);}
completion:^(BOOL finished) {
}];
But when I run the code, the center of test view is already at 100,-100. Why it doesn't animate?
Move the code from your viewDidLoad to your viewDidAppear. In viewDidLoad, the UIView has been loaded, but it is not visually present yet.
I guess you are using auto constraints which prevent animating views the way you are doing.
In order to fix this issue try these steps:
First:
Move your code to - (void)viewDidLayoutSubviews.
Second:
Do these steps before and after doing any animation to your view:
- (void)viewDidLayoutSubviews{
_test.translatesAutoresizingMaskIntoConstraints = YES;
_test.center = CGPointMake(100.0,100.0);
[UIView animateWithDuration:10.0
delay:5
options:UIViewAnimationOptionCurveLinear
animations:^{_test.center = CGPointMake(100.0,-100.0);}
completion:^(BOOL finished) {
}];
[_test updateConstraints];
}

How to animate a view on button click iOS?

With the following code I'm showing a view with an animation on a button click.
UIViewController *modalView = self.pageViewController;
[self.view addSubview:modalView.view];
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];
[modalView.view removeFromSuperview];
}];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
This works fine for first time button click. When I'm pressing the button again and again this is not animating anymore. It's animating for the first time only. Is there any way to do so?
Thanks in Advance!
The cause is that modalView.view's frame initially if different from
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
so when changes its frame animation is happens.
But next time when you perform button's action modalView.view's current frame and that frame you sets are same. So there is nothing to animate at all.
Also, you should move [modalView.view removeFromSuperview]; from animation block to completion block
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];}
completion:^(BOOL finished) {
[modalView.view removeFromSuperview];
}];

UIView Animation at viewDidLoad/viewDidAppear?

I am attempting a simple UIView animation in the viewDidLoad or viewDidAppear method but it doesn't animate.
UIImage *bluredScreenshot = [self.parentViewControllerScreenshot applyBlur];
[UIView transitionWithView:self.screenshotView duration:3.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.screenshotView.image = bluredScreenshot;
} completion:nil];
Simple cross dissolving two images. When ran from viewDidLoad or viewDidAppear the image changes but isn't animated.
But lets say I run the animation from a method after I press a button, it will animate. Why? Seem strange.
Is it possible to make it from the viewDidLoad method? How would you solve this?
Thanks.
You need to fade in the screenshot. If you're using two images, you'll need to place one image view atop the other.
pop this inside viewDidAppear: after calling [super viewDidAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.screenshotView.frame];
[self.view addSubview:imageView];
[imageView setAlpha:0.0f];
imageView = blurredScreenshot;
[UIView animateWithDuration:0.3f animations:^{
[imageView setAlpha:1.0f];
} completion:^(BOOL finished){
self.imageView.image = blurredScreenshot;
[imageView removeFromSuperview];
}];
when the view is loading you can't animate because there isn't anything to animate. Try in viewdidapear: and Don't use transition
[UIView animateWithDuration:3.
delay:0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.screenshotView.image = bluredScreenshot;
}
completion:nil];
Just a reminder, sometimes your problem can be caused by not calling:
self.layoutIfNeeded()

Resources