In my iPhone app, I have two views on a main view controller , one is normal view 'a' with some buttons and other is scrollView 'b'. When I tap on button in view 'a', I want to show scroll view with animations like the scroll view has to come up from the back of view 'a', but it comes up from the front of view 'a'.
I used the following code for animating the scroll view.
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
How to implement the animation to scroll view to show it from top of view 'a'.
Try to move "b" to the back first:
[self.view sendSubviewToBack:scrollview_b];
I found solution for my problem.
Solution :
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}
Related
I am trying to create an animation. When I click on an image, the view expands the from this frame to the view of the UIViewController to the bounds of whole screen on pushing it on the navigationController and shrinking to the same size while popping it back. I was able to create the pushing animation but unsuccessful in my attempt to create the popping animation.
// Code in the controller which pushes the controller on the stack
self.navigationController.view.center = center;
self.navigationController.view.frame = frame;
self.navigationController.view.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
// Code in the viewDidAppear of controller being pushed to the stack
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.frame = [[UIScreen mainScreen] bounds];
self.navigationController.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}
completion:nil];
This works properly but I tried the following code for pop animation in the viewDidDisappear, where initialFrame is the frame on which I am tapping to open the UIViewController.
// Code for viewDidDisappear
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.view.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
self.view.frame = self.initialFrame;
self.view.center = CGPointMake(self.initialFrame.size.width/2 + self.initialFrame.origin.x, self.initialFrame.size.height/2 + self.initialFrame.origin.y);}
completion:nil];
I am not able to make the pop animation work. Please help me out.
I've been following this and a bunch of other tutorials/blogs,etc trying to get my view to scroll when the user hits "return" and ends on a UITextField that is being covered by the keyboard but nothing is actually working.
I'm wondering what must I be doing or missing that is causing this?
Basically:
the user wants to do something in the app: add a credit card
show a UIView that contains all the CC fields
1/2 of the UITextFields are covered by the keyboard
scroll the view when the user gets there.
Nothing is happening. The code for detecting the 'covered by keyboard' case is being hit and executed but: [self.scrollView setContentOffset:scrollPoint animated:YES] has no effect at all.
Thoughts?
Reveal screenshot:
code: same as the tutorial in the link. Nothing new there.
Did you check the contentSize property?
To make a scroll view scrollable, the content must be larger than the display area(usually the bounds of the scrollview).
You can achieve this by explicitly setting the contentSize property which is CGSizeZero by default
In addition, setting the contentInset property to adjust the size of display area(in this case you can set bottom value equals keyboard height) when the keyboard is popped up
Try this,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y-keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y+keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
What I do to avoid the textField being covered by the keyboard is to animate the textfield to the top of the screen when the user touches it to start entering something with the keyboard and animate it back when done. It is much more elegant in my opinion. Maybe that could be an alternative solution for you... I do it for the different screensizes with different numbers... you might need to adjust it of course...
So listen to the textField delegate and in
- (void)textFieldDidBeginEditing:(UITextField *)textField {
you do
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 568){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 667){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -238; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 736){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -251; [self.view setFrame:frame]; [UIView commitAnimations];});
}
and in
- (void)textFieldDidEndEditing:(UITextField *)textField {
you just animate back
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations];});
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 have tried this code. http://www.mediafire.com/download/bvoqrkn82sd6az9/tablesample.zip ..Here, I need like this.. whenever I click the show button, it should display the list of Tableview like dropdown as it is in this screenshot. When on load, table view should be hidden. When on click the button, tableview should display. Your help is highly appreciated. Thanks in advance.
You can change height of tableView with animation. Set time according your suitability.
For Expansion:
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationYourChoice
animations:^{
CGRect frame = self.tableView.frame;
frame.size.height = 300;
self.tableView.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
For shrinking:
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationYourChoice
animations:^{
CGRect frame = self.tableView.frame;
frame.size.height = 0;
self.tableView.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
I want to animate the position of a UISearchBar, but there's no animate effect when I change the frame or bounds property of UISearchBar(center, alpha dose animate).
It's iOS SDK 4.2, is there a bug? I'm confused...
The problem is that some inner search bar views forcing the resize to ignore the animation.
This worked for me -
[UIView animateWithDuration:0.2 animations:^ {
[searchBar setFrame:searchBarFrame];
[searchBar setNeedsLayout];
}];
Where searchBarFrame - is a frame you need to set (I save it before search bar resized first time)
Use the UIViewAnimationOptionLayoutSubviews option:
[UIView animateWithDuration:.25
delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
CGRect frame = searchBar.frame;
frame.size.width = newWidth;
searchBar.frame = frame;
} completion:nil
];
Try this:
[UIView animateWithDuration:0.3 animations:^{
self.searchDisplayController.searchBar.frame = CGRectMake(0, 200, 320, 44);
}];