I wrote a custom UITableViewCell - image view, button and three labels now I am trying to add some animations to it. So once I tap the button, it fades away and the spinner replaces the button. After two seconds the cell is overlaid with a red color, as a subview of cell fades in, then the indicator is removed and and red overlay starts fading back out. As well the button I previously removed fades back in.
(I could not phrase it a better way :P )
The method is :
-(void)rentButtonPressed:(id)sender
{
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
indicator.center = self.rentButton.center;
[UIView animateWithDuration:0.2
animations:^{self.rentButton.alpha = 0.0;}
completion:^(BOOL finished){
[self.rentButton removeFromSuperview];
[self addSubview:indicator];
}
];
UIView *overlay = [[UIView alloc] initWithFrame:self.backgroundImage.frame];
overlay.alpha = 0.0;
overlay.backgroundColor = [UIColor redColor];
[self.contentView addSubview:overlay];
[UIView animateWithDuration:0.4
delay:2.0
options:UIViewAnimationCurveEaseInOut
animations:^{
[indicator removeFromSuperview];
overlay.alpha = 0.4;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4
animations:^{ overlay.alpha = 0.0; }
completion:^(BOOL finished)
{
[overlay removeFromSuperview];
}
];
[self.contentView addSubview:self.rentButton];
[UIView animateWithDuration:0.4 animations:^{ self.rentButton.alpha = 1.0;}];
[self.delegate didTryToRentMovieAtCell:self];
}
];
}
So the code does fade out the button, replace it with spinner and fades in the red overlay. The problem is, the red overlay does not fade away, but disappears same with the button, instead of fading in, it just appears.
During your animation, you are changing the view hierarchy by adding and removing subviews. The UIView class method animateWithDuration:animations:completion is intended only animating property changes in a view, and not for changing the view hierarchy.
Try using the UIView class method transitionWithView:duration:options:animations:completion: instead, and use the cell's content view as the "container."
This documentation is helpful in distinguishing between animating view property changes and animating view transitions, specifically the section "Changing the Subviews of a View":
http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html
Related
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];
I have a location button. I want to open a pop up view when location button got pressed. like this
when this view opened, all buttons and background view should be blurred.
How can i achieve this??
thnx for any kind of help.
To show the view with alert like animation create a popView you want to show and follow the below code:
popView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[self.view addSubview:popView];
[UIView animateWithDuration:0.3/1.5 animations:^{
popView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
popView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
popView.transform = CGAffineTransformIdentity;
}];
}];
}];
popView.transform = CGAffineTransformIdentity;
Hide the other views visible.
Just follow the below code
-(IBAction)buttonPress:(id)sender
{
popView.hidden = NO;
//OR
[self.view addSubview:popView];
btn1.hidden = YES;
btn2.hidden = YES;
btn3.hidden = YES;
view1.hidden = YES;
}
Simple,
Create one view & size of that view will be size of parent view. Your second view will contain your UI along with black blur view at background.
When you tap on the location button display that view so all other button will be hidden automatically.
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];
}];
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()
I have a View (CupsViewController) with a Label and when it's clicked, TableView (AddressTableController) slides from the bottom of the screen and stays in the lower half.
In TableView, when Ok Buttonis pressed, I want to change value of Labeland remove TableViewwith animation (that is, slide to the bottom).
Here is my code:
CupsViewController
Method called when click on Label
- (IBAction)showPop:(id)sender
{
addressTableController = [[AddressTableController alloc]initWithStyle:UITableViewStyleGrouped];
[addressTableController setDelegate:self];
[[self view] addSubview:[addressTableController myTableView]];
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[[addressTableController myTableView] setFrame:CGRectMake(0, kScreenHeight * 0.5, kScreenWidth, kScreenHeight * 0.5)];
} completion:nil];
}
Method called from AddressTableControllerwhen Buttonis pressed
- (void)addressTableController:(AddressTableController *)viewController didChooseValue:(int)value {
direccionLabel.text = [[[[myAppDelegate usuarioActual] cups] objectAtIndex:value] direccion];
[addressTableController.view removeFromSuperview];
}
Image
As you can see, I've tried with removeFromSuperviewbut it does nothing. How can I slide TableViewto the bottom?
It seems that you myTableView property returns a different view, not the one that is referenced in the view property. So you basically add the former as a subview ([[self view] addSubview:[addressTableController myTableView]];), but try to remove the latter ([addressTableController.view removeFromSuperview];).
Just do
[[addressTableController myTableView] removeFromSuperview];
instead of
[addressTableController.view removeFromSuperview];
Here you go. Cheers, mate! :)
P.S.
if you want to animate the view out, you can do it like this
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
// animation code...
} completion:^(BOOL finished) {
[[addressTableController myTableView] removeFromSuperview];
}];