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);
}];
Related
I have a button and in the button action I want to show my UITableView by sliding from the button and after the cell click it should goest up by sliding effect.I did it by UIViewAnimation by setting the fixed y position and changing the height from 0 to the height
tableview.hidden=FALSE;
tableview.frame = CGRectMake(0,myButton.frame.size.height,myButton.frame.size.width, 0);
[UIView animateWithDuration:AnimationTime animations:^{
tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.x, tableview.frame.size.width, 200 );
} completion:^(BOOL finished) {
}];
But this is not .I wanted a sliding effect.Any help will be appreciable
Use this method for animation. Provide a option for animation style. There a lot of option in objective-c for animation style
[UIView animateWithDuration:15.0f delay:0.0f options:UIViewAnimationOptions animations:^{
tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.x, tableview.frame.size.width, 200 );
} completion:^(BOOL finished)
{
}];
for your case i thought you should use UIViewAnimationOptionTransitionFlipFromTop as animation options for slide table from top to bottom.By using this your table view get its full height in 15 sec. you can mange that too.
EDIT:
Once you clicked on cell, you want that your tableview goes up with slide effect. Than use
[UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.x, tableview.frame.size.width, 0 );
} completion:^(BOOL finished)
{
}];
for slide up animation.
Problem is not animation, problem is the frame you are defining. Easiest way to do this, is to define open and close frame and use them to animate.. See below..
Define Open and Close frame
-(void)initComponents{
CGRect rect=tableView.frame;
rect.origin=myButton.center;
openFrame=rect;//Assign openFrame before changing the size.
rect.size=CGSizeZero;
tableView.frame=rect;
closeFrame=rect;//Assign closeFrame
}
Call the initComponents function in viewDidLoad to set the initial state of your tableview. Now simply use the functions below on actions where you want to call.
-(void)openMenu{
[UIView animateWithDuration:.3 animations:^{
tableView.frame=openFrame;
}];
}
-(void)closeMenu{
[UIView animateWithDuration:.3 animations:^{
tableView.frame=closeFrame;
}];
}
Thats it. It should work.
Let me know. Cheers.
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!");
}];
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.
I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}
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];
}];
}