UIView Animation not working to resize view - ios

I am using following code to animate the appearance of selectionMoreOptionView
[selectionMoreOptionView setFrame:CGRectMake(974, 56, 50, 0)];
[self.view insertSubview:selectionMoreOptionView aboveSubview:lightBoxView];
[lightBoxView setAlpha:0.0];
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
[lightBoxView setAlpha:0.5];
[selectionMoreOptionView setFrame:CGRectMake(974, 56, 50, 100)];
} completion:^(BOOL finished) {
}];
But it is not animating instead it just appears at once without animating. What i am doing wrong?

Simply try this
[UIView animateWithDuration:.5 animations:^(void){
[lightBoxView setAlpha:0.5];
[selectionMoreOptionView setFrame:CGRectMake(974, 56, 50, 100)];
}];

The animation was working fine the problem was that i had two buttons inside the view big enough to cover all of the view. The views property Clip Subviews was unchecked. I had thought when a parent resizes it effects its child's automatically, but it don't in some cases when parent do not clip its subviews. Thanks everybody for your reply :)

check your
[selectionMoreOptionView setFrame:CGRectMake(974, 56, 50, 100)];
I think XY co-ordinates going out of viewController.
Try to bring it in the ViewController area,as of X should be possibly less than 320
and y<480 so that you can see your view.

Related

iOS 7 UIView animateWithDuration:animations:completion: resets its actions at any kind of transformation

I recently got a problem with my App.
I usually use
[UIView animateWithDuration:(NSTimeInterval) animations:^{...} completion:^(BOOL finished) {...}];
to make my animations and it worked perfectly until now. Here's my problem's code
UIImageView *someimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someimage"]];
[someimage setFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubView:someimage];
[UIView animateWithDuration:0.5 animations:^{
[someimage setFrame:CGRectMake(400, 400, 200, 200)];
} completion:^(BOOL finished) {
NSLog(#"Just finish moving some image lets rotate it!");
[UIView animateWithDuration:0.5 animations:^{
someimage.transform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
NSLog(#"What just happened?");
}];
}];
I got an image and i moved it from one corner to another position using animations. Then, when the animation finishes I rotate it, but when I make that rotation the UIImageView appear at the corner again.
Can someone explain me why this happen and how to handle it?
Thanks!
The UIView class reference says:
WARNING If the transform property is not the identity transform, the
value of this property is undefined and therefore should be ignored.
Changing a view's (or layer's) frame and its transform at the same time does not work as expected. Instead, change the view's center property.

UIView transform work strange

I use this code to move my view
[UIView animateWithDuration:0.3 animations:^{
[MyView setTransform:CGAffineTransformMakeTranslation(260, 0)];
} completion:^(BOOL finished) {
}];
MyView is a subview of self.view
I want to move it to right with 260
but it will first move to left with 130 and no animate
and then move to right with 260 and animate
I don't know why MyView move to left first?
The problem is that you are using auto layout. For a simple animation, trying turning off auto layout in storyboard.
Your animation should work then as expected.
I guess the problem is that the MakeTranslation function is making your view reset it's position before animating to the new transform.
Try this:
[UIView animateWithDuration:0.3 animations:^{
[MyView setTransform:CGAffineTransformTranslate(myView.transform, 260, 0)];
} completion:^(BOOL finished) {
}];

Make 2 animations in Custom View

Im creating a custom view, it contains methods like Refresh,*LayoutSubviews*, following this tutorial
I want a label to be animated growing, to it's full width and then back to 0 width. Im using this code in "refresh" method:
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[labelFav setFrame:frame];
} completion:^(BOOL finished) {
//[labelFav setHidden:YES];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 0, 20);
[labelFav setFrame:frame];
}];
}];
The code works on any other view, but since this is a Custom view by some reason the completion block is not executing.
What happens is that it only does the first block. I set a breakpoint in the "completion" block and it stops there but the animation doesnt happen. I tried to setHidden the label and still it doesnt happen.
Any ideas?
remove auto layout from you Interface builder main screen.
your view may be changing it's frame, but auto layout is (possibly) forcing it elsewhere...
Another way to solve problem. Tested.
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[labelViewForFav setFrame:frame];
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 0, 20);
[labelViewForFav setFrame:frame];
} completion:^(BOOL finished) {
}];
}];
Problem is When you make UIlabel width less than its current width. Animation is not done properly. Animation not visible just jump to final frame.
to solve it Put your label in a view change the size of this view and it will work properly.

animateWithDuration setFrame not working

Hi I have piece of code which only one line is not working...
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
[self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working
[self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
[self.currentLabel removeFromSuperview];
self.currentLabel = label;
}];
I'm running out of ideas what is wrong...
What I find out is: if I turn off "Use Auto Layout", then it magically working, so this issue is connected with auto layout.
After a search, I find this: http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was
Add constraint outlet mapping for your view, then instead of using setFrame, updating constraints will do the trick!
Below is my final implementation (_topConstraint is the top vertical space constraint of table view):
- (IBAction)switchButtonTouched:(id)sender
{
if (_topConstraint.constant <= 0)
_topConstraint.constant = _buttonView.frame.size.height;
else
_topConstraint.constant = 0;
[_tableView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5f animations:^(void){
[_tableView layoutIfNeeded];
} completion:^(BOOL finished){
}];
}
Frame must be a CGRect with 4 parameters: (xPosition,yPosition,width,height). Use CGRectMake for set frame
self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height)
You set 0.3 sec the animation duration, and when it s end you remove it from the view. This is too short. set the duration for example 2, and you will see that your label is moving .
Sometimes you should set frames for animating views also after animation, for example in completion block, try something like that
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
[self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working
[self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
self.frame = currentLabelFrame;
label.frame = label.frame;
[self.currentLabel removeFromSuperview];
self.currentLabel = label;
}];
If your issue is neither Autolayout related, or the others listed here, there is also another possible problem that turned out to be my issue. I'm simultaneously running a UIView transition (transitionFromView:toView:) on views that take up the same screen space (Different superviews, not related, other than positioning in the superview of the overarching view controller).
My code looked like this:
[UIView transitionFromView:self.someView
toView:self.someOtherView
duration:0.6f
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
[UIView animateWithDuration:0.3
delay:0.0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.someThirdView setFrame:someFrame]; //not working
[self.someThirdView setAlpha:1.0f]; //working
} completion:nil];
The frame changed, but it popped to the new frame rather than animating. I'm guessing this is an internal iOS issue. As soon as I removed the "transitionFromView:..." call, the frame animation worked fine.
My solution for now has been to move the second animation into the completion block of the transitionFromView. It's not perfect, as the two animations should have lined up, but it is a passable solution for now.

how scroll view up when uidatepicker/pickerview appear similar with keyboard ios

I have a screen with several buttons from top to bottom and when they touched, they will show pickerview and uidatepicker. the problem is how to scroll the view if uidatepicker blocked the button.
so i want scroll view working here just like when uikeyboard appear. thanks! :)
ps: i really want to upload the images but my reput don't allow me to do that :)
update:
in the end i use this on my code and the animation looks similar with tpavoiding scroll, here is the code
[UIView animateWithDuration: 0.5f animations:^{
self.containerView.frame=CGRectMake(0, -120, 320, 308);
}
completion:^(BOOL finished) {} ];
so containerView will move up 120, and when i click done(toolbar above the uidatepicker) i just make it to normal position.
[UIView animateWithDuration: 0.5f animations:^{
self.containerView.frame=CGRectMake(0, 0, 320, 308);
}
completion:^(BOOL finished) {} ];
Use UIView animations:
[UIView animateWithDuration: duration animations:^{ myView.frame = CGRectMake(0, 0, 320, 320);} completion:^(BOOL finished) { [self doSomething]; } ];

Resources