UITableView resizing when moving the container UIView - iphone

I have an application where I'm moving my entire UIView up when using the keyboard, in order to not hide some important UITextFields.
My problem is that the UITableView that I have there does not behave the same way as all the other views. Instead of moving the entire UITableView, it moves only its top edge, and that resizes the UITableView which is not the desired effect.
My problem is better described here: http://www.youtube.com/watch?v=AVJVMiBULEQ
This is the code that I'm using for the animation:
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (movedUp) {
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= 140;
rect.size.height += 140;
} else {
// revert back to the normal state.
rect.origin.y += 140;
rect.size.height -= 140;
}
self.view.frame = rect;
[UIView commitAnimations];
}

As solved in comments above, code above works when view is not autoresizing. The simpler approach would be to use CGAffineTransform transformations that calculate view frame instead of you.
-(void)setViewMovedUp:(BOOL)movedUp {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
if (movedUp) {
self.transform = CGAffineTransformMakeTranslation(0, -150);//move up for 150 px
} else {
self.transform = CGAffineTransformIdentity;//reset to initial frame
}
[UIView commitAnimations];
}

Related

View animation frame change

Are emerging in the subview scrolling animation. However actionsheet place when you scroll subview, not again appear in the first place.
if(checkboxState == 0)
{
NSTimeInterval animationDuration = 0.8;
CGRect newFrameSize = CGRectMake(0, 155,320,120);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
subview.frame = newFrameSize;
[UIView commitAnimations];
}
else if(checkboxState == 1)
{
NSTimeInterval animationDuration = 0.8;
CGRect newFrameSize = CGRectMake(0, 105,320,120);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
subview.frame = newFrameSize;
[UIView commitAnimations];
}
Pressing the View is a checkbox in the above code is provided to move up and down. However, even though actionsheet slipped up again when the first view is in place.
View is scrolled up, how can we keep it that way, even if opened actionsheet?
Use [UIView setAnimationBeginsFromCurrentState:YES]; after both "beginAnimation".
Then the previous changes will be saved.
I fixed the problem by using the
subview.bounds
Thank you for your helps.

iOS - Animation effects - Image pop-in

I'd like to have an image in my iphone app "pop-in" on screen rather than just appearing. By "pop-in" I mean that it would grow from a small dot to its actual size. For reference, this is exactly the same as the "pop" animation effect in Keynote.
I'm completely new to iOS animations, so if someone could point me in the direction on the animation effects I would need to use, it would be greatly appreciated.
Thanks
Brian
UPDATE
I've added this code from the suggestions below. This works but it scales my image down, rather than up. I know that is because I have 0.01 as the transform scale size, but I would like to know how I can start out with an image of size 0.0 and scale up to 1. Is it just a matter to setting the size of my image to 0?
Thanks
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
image.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
The effect you’re looking for is something like this:
// instantaneously make the image view small (scaled to 1% of its actual size)
view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
// animate it to the identity transform (100% scale)
view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];
if you want something like Facebook does on liking any post then use this
-(void)animateButton:(UIButton *)sender{
UIButton * btn = (UIButton *)sender;
[UIView animateWithDuration:0.3/1.5 animations:^{
btn.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.4, 1.4); // scales up the view of button
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
btn.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.7, 0.7);// scales down the view of button
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
btn.transform = CGAffineTransformIdentity; // at the end sets the original identity of the button
}];
}];
}];}
just call this method when you want to animate the view
if you have text and image on the button and you just want to animate the image of the button then just replace "btn" with "btn.imageView" , this will just produce animation on the image view property of the button.
Hope it helps
All the best.
You have to animate the frame of the view, to shrink it from zero to the final state.
This can be done for example with UIView block animation.
So for example start with your view as an IBOutlet property self.myView with the final size and position, but set the hidden flag.
Then when you want it to appear use the following:
// Save old frame as final stater and set it to zero size for the start of the animation
// But keep the same center position, so it just grows and don't move around
CGRect oldFrame = self.myView.frame;
CGRect oldCenter = self.myView.center;
self.myView.frame = CGRectZero;
self.myView.center = oldCenter;
self.myView.hidden = NO;
NSTimeInterval duration = 0.3;
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
// New position and size after the animation should be the same as in Interface Builder
self.myView.frame = oldFrame
}
completion:^(BOOL finished){
// You can do some stuff here after the animation finished
}];
Swift 2.0 version
view.transform = CGAffineTransformMakeScale(0.01, 0.01);
UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
// animate it to the identity transform (100% scale)
self.view.transform = CGAffineTransformIdentity;
}) { (finished) -> Void in
// if you want to do something once the animation finishes, put it here
}
For that you will have to use a simple UIView
Add the UIview to your current view.
- (void) initPopUpView
{
popup.alpha = 0;
popup.frame = CGRectMake (160, 240, 0, 0);
[self.view addSubview:popup];
}
- (void) animatePopUpShow
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:#selector(initPopUpView)];
popup.alpha = 1;
popup.frame = CGRectMake (20, 40, 300, 400);
[UIView commitAnimations];
}

Swiping A View with Gestures

I am trying to develop an application to Iphone; but i am new-bee so i have got a lot of problems.
My application has got a default view and another small view in the main view that name is flashcard.
I want to add capability this view swiping (or sliding) like Photos in Iphone. For example if user swipes view until the inner view's center reaches bounds of main view there will be two possibilities,
1- if user finish swiping before reach inner view will return to original position.
2- if user don't stop swiping, inner view will go out at screen from swipe direction and return the screen from the opposite direction.
So i declare UIPanGestureRecognizer at viewDidLoad like below
UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanFlashCard:)];
[flashcard addGestureRecognizer:panFlashCard];
[panFlashCard release];
handPanFlashCard action:
UIView *piece = [recognizer view];
CGPoint center = piece.center;
CGFloat x = 160; // the original x axis of inner view
if ([recognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:piece ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)];
[UIView commitAnimations];
CGFloat totalX;
totalX= x + translation.x;
if(translation.x <0)
{
if (totalX <= 0)
{
[self showNextCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
else
{
if (totalX >= self.view.bounds.size.width)
{
[self showPreviousCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
}
showNextCard action:
[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
showPreviousCard action:
[flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
When i run this application, it builds and runs successfully but there is something wrong with animations, some frame mistakes i think.
Please help me to correct this code and run fluent animations while swiping.
If I understand what you are trying to do then you can just use built in controls for this. Take a look at the pagingEnabled property on the UIScrollView class. There is also a sample project called PhotoScroller that may help.
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html

Animated moving and rotating a UIView doesn't quite work

I want to create an animation that moves and rotates a UIView at the same time. I tried the following code:
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
myView.frame = newFrame;
myView.transform = CGAffineTransformMakeRotation(0.4);
[UIView commitAnimations];
The result is, that after the animation has finished the view is drawn incorrectly (some parts are not visible anymore). If I only change the frame OR the transformation for the animation, the view draws correctly. The problem only occurs if I set both the frame and transformation.
What would be the correct way to animate moving and rotating of a view at the same time?
You can't use frame, bounds, or center if you are also animating things like rotation. You need to use CGAffineTransforms for everything and concatenate them together. Like so:
CGAffineTransform transform = CGAffineTransformMakeTranslation(100,100);
transform = CGAffineTransformRotate(transform, 0.4);
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
myView.transform = transform;
[UIView commitAnimations];
Try doing it this way: (Moving left and rotating, just for example)
[UIView beginAnimations:#"MoveAndRotateAnimation" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
CGPoint center = CGPointMake(-200, self.view.center.y);
self.view.transform = CGAffineTransformMakeRotation(-0.5);
self.view.center = center;
[UIView commitAnimations];
The center point locations would be the point you wish to make the view move, of course.
The problem is probably with your new frame size as compared to original frame size. View frame is a bit tricky when you rotate a frame:
Frame is in superviews coordinate system. So, when you rotate a rectangle, the new frame will be the smallest rectangle in superview's coordinate system which contains the view.
e.g. if you have a square view with frame size of (100*100) and rotate it by 45 degrees your new frame would have a size of (141*141) and if you set the frame to have a size of (100*100) you'll cut part of your view.
Look at here for better understanding of how view frame works.
You need to do this:
#pragma mark (Woblling animation)
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));
self.view .transform = leftWobble; // starting point
[UIView beginAnimations:#"wobble" context:self.view ];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:11];
[UIView setAnimationDuration:1.00];
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(wobbleEnded:finished:context:)];//should be same as written
self.view .transform = rightWobble; // end here & auto-reverse
[UIView commitAnimations];
and then implement these methods. They are necessary to keep your view in the same position after rotation.
- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue]) {
UIView* item = (UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
Use this method if you are using navigation and when you navigate from one view to another, then your view would be at the same place.else leave this method:
- (void)viewWillAppear:(BOOL)animated
{
UIView* item = self.view;
item.transform = CGAffineTransformIdentity;
}
I had the same problem but did some experiments and came up with this solution.

show the keyboard of a textfield on top of the screen

In my iPhone apps, my problem is that I have a textfield at the bottom of the screen, so when the keyboard appear, he hides the textfied, there is a way to show the keyboard on the top of the screen?
You should move your view when the keyboard appears.
The code is:
In .m file
- (void) loginViewUp : (UIView*) view
{
if(!alreadyViewUp)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = view.frame;
rect.origin.y -= View_Move_Hight;
view.frame = rect;
[UIView commitAnimations];
alreadyViewUp = !alreadyViewUp;
}
}
- (void) loginViewDown :(UIView*) view
{
if(alreadyViewUp)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = view.frame;
rect.origin.y += View_Move_Hight;
view.frame = rect;
[UIView commitAnimations];
alreadyViewUp = !alreadyViewUp;
}
}
In .h file
- (void) loginViewUp : (UIView*) view;
here
#define View_Move_Hight 170
is defined before #implementation.
You should design your view so that it shifts up with the keyboard, iPhone users are used to the keyboard always being on the bottom of the screen so this would go against the HIG