Im trying to figure out why this isnt working
in my tableViewcontroller viewDidLoad
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320,0)];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 0)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}];
if I remove the slide back up part in the completion block of the first animate call It works. The view slides down correctly. However I cannot get it
to shrink back up at all. When I include the slide up code in the completion block the view is not displayed at all on load and I dont know why and Im going insane
I'm not sure why the label disappears, but you can fix that by giving the view and label an appropriate height when you create them, and only animate the label's y position rather than its height.
- (void)viewDidLoad {
[super viewDidLoad];
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320,30)];
self.headerView.backgroundColor = [UIColor yellowColor];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
}
viewDidLoad is not really the best place to be starting animations. You should move the code to viewWillAppear: , and if you only want this to occur the first time the view appears, you should add a BOOL property to your controller (i.e. self.hasperformedInitialHeaderAnimation), so:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(!self.hasPerformedInitialHeaderAnimation){
self.hasPerformedInitalHeaderAnimation = YES;
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}
Related
I have a container view that has one subview. I want to replace this subview with another view which has a different size than original, so the container should be resized as well. Here's what I'm trying to do
UIView *content = NEW_CONTENT;
content.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentContainer layoutIfNeeded]; // the container view
// make the old content disappear first
[UIView animateWithDuration:0.3
animations:^{
UIView *oldContent = [self.contentContainer.subviews firstObject];
oldContent.alpha = 0.0f;
}
completion:^(BOOL finished) {
// then replace it with a new content
[UIView transitionWithView:self.contentContainer
duration:0.1
options:0
animations:^{
[[self.contentContainer.subviews firstObject] removeFromSuperview];
[self.contentContainer addSubview:content];
[content autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
[self.contentContainer layoutIfNeeded];
}
completion:^(BOOL finished) { // completion stuff}];
}];
Old content opacity animates properly, but the layout is changed without any animation. How could I fix it?
//3 views(container,first,second) allocate in viewdidload
UIView *container=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 250, 300)];
container.backgroundColor=[UIColor redColor];
[self.view addSubview: container];
UIView *first=[[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
first.backgroundColor=[UIColor blueColor];
[self.view addSubview: first];
UIView *second=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 150)];
second.backgroundColor=[UIColor purpleColor];
second.alpha=0;
[self.view addSubview: second];
//Animation Function with first view removed adn changing the frame of container and adding the second view
[UIView transitionWithView:first duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
first.alpha=0.0;
} completion:^(BOOL finished) {
[UIView transitionWithView:second duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
container.frame=CGRectMake(50, 10, 100, 200);
second.alpha=1.0;
} completion:^(BOOL finished) {
}];
}];
I think so u r trying this same scenario.it works perfectly in my case
I would like to create a button that will show for only 5 second and then disappear. User have to click the button before it disappear. So I use animationWithDuration to set alpha to 1 and then set it back to 0. Here is my code...
__block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
showButton.alpha = 1.0;
}
completion:nil];
[UIView animateWithDuration:0.5
delay:3.0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton removeFromSuperview];
showButton = nil;
}];
-(void)showButtonClick{
NSLog(#"click")
}
But the showButtonClick can't get called. What am I do wrong?
Try this code:
UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[showButton setHidden:NO];
showButton.alpha = 1.0;
}
completion: {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton setHidden:YES];
}];
}];
-(void)showButtonClick{
NSLog(#"click")
}
You should create a NSTimer or something else to call the animation. Because both of your animation are being called at the same time. That is why you were not able to click the button, thus showButtonClick wasn't being call.
[UIView animateWithDuration:5.0
animations:^{
initWithFrame:CGRectMake(100, 0, 150, 40)
//Animation code goes here
} completion:^(BOOL finished) {
//Code to run once the animation is completed goes here
[your button remove from removefromsuperview];
}];
I can't understand,why you are using animation two times.....as your button is frame is allocated as initWithFrame:CGRectMake(0, 0, 150, 40).....check the animation code above....it will animate the button from 0 to 100....after animation completed the button will be removed...
I'd like to make settingView on myView display and disappear with animation.
I call -showSettingView
if (!self.settingView) : the animation correctly work.
But, else if (self.myView) : settingView disappear WITHOUT animation.
How do I fix it to make settingView disappear with animation?
#property (nonatomic, strong) UIView *myView;
#property (nonatomic, strong) UIView *settingView;
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem* settingButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showSettingView)];
self.navigationItem.rightBarButtonItem = settingButton;
}
- (void)showSettingView
{
self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 568, 320, 480)];
self.myView.opaque = NO;
self.myView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
[self.view addSubview:self.myView];
if (!self.settingView) {
self.settingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
self.settingView.backgroundColor = [UIColor blackColor];
[self.myView addSubview:self.settingView];
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{self.myView.frame = CGRectMake(0, 60, 320, 480);}
completion:^(BOOL finished) {}
];
} else if (self.myView){
self.myView.frame = CGRectMake(0, 60, 320, 480);
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);}
completion:^(BOOL finished) {}
];
[self.settingView removeFromSuperview];
self.settingView = nil;
[self.myView removeFromSuperview];
}
}
Thank you.
You dealloc the settings view before the animation starts. Try this:
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);}
completion:^(BOOL finished) {
[self.settingView removeFromSuperview];
self.settingView = nil;
[self.myView removeFromSuperview];
}
];
*Also: If you are using auto layout you need to do one of these options:
A. Set the constrains of myView instead of the frame, before the block, and then call only layoutIfNeeded: inside the block.
B. Set the transform property in the animation block.
Try adding this line
[self layoutIfNeeded];
as the last line within your animation block. Something like that:
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{
self.myView.frame = CGRectMake(0, 60, 320, 480);
// any other stuff you have to move
[self layoutIfNeeded];
}
completion:^(BOOL finished) {}
];
I use the following lines of code:
[UIView animateWithDuration:0.50
animations:^{ itemView.transform = CGAffineTransformIdentity; }
completion:^(BOOL finished) { if (finished) [itemView removeFromSuperview]; }];
The animation does nothing. If I take the removeFromSuperview out of the completion block, it doesn't work either, I guess because the view is removed before it completes. So, either way, the appearance is the same - no animation.
I'll appreciate any suggestion or workaround on this.
Are you setting the transform correctly? The below is an example of scaling a UIView to zero area and then removing it from the superview.
UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[itemView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:itemView];
CGAffineTransform trans = CGAffineTransformScale(self.view.transform, 0, 0);
[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
itemView.transform = trans;
} completion:^(BOOL finished) {
[itemView removeFromSuperview];
}];
I tried to add SubView with animation following codes.It's okay.
[self.view addSubview:pickerView];
pickerView.frame = CGRectMake(0, 0, 320, 50);
[UIView animateWithDuration:1.0
animations:^{
pickerView.frame = CGRectMake(0, 152, 320, 260);
}];
And i also want to remove the subView with slide animation like above animation.
How can i do that?
Thanks in advance.
you could use this animateWithDuration completion block to remove the view
[UIView animateWithDuration:1.0
animations:^{
pickerView.frame = //move it out of screen
} completion:^(BOOL finished) {
[pickerView removeFromSuperView];
}];
[UIView animateWithDuration:.2 animations:^{
pickerView.frame = CGRectMake(0, 0, 320, 50);
} completion:^(BOOL finished){
[pickerView removeFromSuperView];
}];