xcode - make searchbar appear/disappear - iphone

I my mapview app, I have a search bar used to search a concrete address.
It works bur for now, it appear in "static" way above the map.
My idea is make that it appear and disapperar using a button "search" as in the picture:
Any idea about hoy do thath?
Thanks in advance

You can do this in code. All you have to do is change its frame.
Add it to your navigationBar.frame and below it and then, when the icon is tapped, put it down. I usually use this code to make it look nice too:
[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Move it down here by changing its .frame
}completion:^(BOOL finished){
}];

You just Need To change the Frame of UISearchBar as You click search Icon to display it.
See
Initially,When You don't to display UISearchBar You should set the Frame In such manner.
initially set The Frame of UISearchBar Out Side The ViewFrame.
thisSearchBar.frame = CGRectMake(0.0, -50.0, searchBarWidth, searchBarHeight)]; in viewDidLoad method
On clicking the Search Icon to display the UISearchBar
- (void)showSearchBar{
[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
thisSearchBar.frame = CGRectMake(0.0, 50, searchBarWidth, searchBarHeight)];
}completion:^(BOOL finished)
{
}];
}
And You finished Work With SearchBar Again Call Set The Frame of Searchbar with ANimation
- (void)hideSearchBar{
[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
thisSearchBar.frame = CGRectMake(0.0, -50, searchBarWidth, searchBarHeight)];
}completion:^(BOOL finished)
{
}];
}

Per my comment, simply use:
searchBar.hidden = true;

Related

UIButton always moves back to its first location?

I am having this hair pulling problem, i hope anybody will be able to help me out. it seems like a little thing but i have not been able to find any solution.
I have a UIButton that i want to move around with a animation block. Everytime i click the button i have it move to a new location, the button always moves to the correct location the problem is that it always moves from its first location to its new location.
lets say that i have a button at (0,0) i then move it to (0,50) works like a beaut!
Now i want to move the button to (0,100) and i expect it to animate from (0,50) to (0,100) but what it does it animate from (0,0) to (0,100) so it ends up in the right location, but moves FROM the wrong location.
i move the UIButton by setting a a new frame in a animation block
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
}
completion:nil];
in my case i move the button in a completion block when other controls a finished moving.
i really hope somebody out there knows exatly what im talking about and have the answer for me.
the code i use for animating the uibutton is this, the buttons name is "btnAddPhoneNumber" and its also the "sender"
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationCurveLinear
animations:^{
txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height);
}
completion:^(BOOL finished) {
txtNewPhoneNumber.placeholder = #"here you go!";
//animate add textfield button.
[UIView animateWithDuration:0.5f
delay:0
options:UIViewAnimationCurveLinear
animations:^{
[sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)];
txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height);
[btnDelete setFrame:btnDeleteRect];
}
completion:^(BOOL finished) {
}];
}];
thank you for reading my question, and for helping me find a solution.
EDIT (added code example)
Try adding UIViewAnimationOptionBeginFromCurrentState to the options of the second animation, see if it solves your problem.
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState
animations:^{
}
completion:nil];
Another way to go would be to set the frame of the button again (to the same value) in the completion handler of the second animation.
Check this work around. As I mentioned in comments, you can use this as your last option. Not a recommended approach.
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationCurveLinear
animations:^{
txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height);
}
completion:^(BOOL finished) {
txtNewPhoneNumber.placeholder = #"here you go!";
[self performSelector:#selector(callSecondAnimation) withObject:nil afterDelay:1.5f)]; //or 0.5f
}];
- (void)callSecondAnimation {
//animate add textfield button.
[UIView animateWithDuration:0.5f
delay:0
options:UIViewAnimationCurveLinear
animations:^{
[sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)];
txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height);
[btnDelete setFrame:btnDeleteRect];
}
completion:^(BOOL finished) {
}];
}
Update:
Try this first,
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationCurveLinear
animations:^{
txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height);
}
completion:^(BOOL finished) {
txtNewPhoneNumber.placeholder = #"here you go!";
txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height);//try setting it here once and then call next one
[self callSecondAnimation];
}];
- (void)callSecondAnimation {
//animate add textfield button.
[UIView animateWithDuration:0.5f
delay:0
options:UIViewAnimationCurveLinear
animations:^{
[sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)];
txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height);
[btnDelete setFrame:btnDeleteRect];
}
completion:^(BOOL finished) {
}];
}
I found the problem, the view used AutoLayout, when i removed the tick in AutoLayout the new position of my button is saved, and the next animation continues from the previously location.
thanks to ACB and Tobi for their input their helped me try different solutions that ended up fixing the problem!

switch view with animation UIViewAnimationTransitionCurlUp

I am working on an iPad app which has a small popup view in front of the background main view.
The popup view displays one photo and its related notes etc. The main view is a bunch of thumbnails
I want to implement actions, like swipe left/right, to replace the popup view with a new popup view, with animation.
// popView is the existing pop up view to be replaced
MyPopupViewController *newPopView = [[MyPopupViewController alloc] initWithData:...];
[UIView animateWithDuration:0.4
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations: ^{
[self.view addSubView: newPopView.view];
[popView removeFromSuperView];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
forView:popView cache:YES];
} completion:^(BOOL finished) { popView = newPopView }
];
[newPopView release];
Well, this doesn't work. I can't remove the old view in animation block otherwise the animation won't fire. I can't remove it in the completion block either, because during the animation, the old image will still be visible under itself.
I've spent quite some time playing with the sequences but just can't get it to work. Please help.
Thanks in advance.
Leo
I finally fixed this by setting the old view hidden in animation block. The animation will reveal the new view during animation.
Here is how I did: it works well in landscape an curls from bottom to top
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionCurlUp animations:^()
{
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
forView:self.welcomScreen cache:YES];
[self.welcomScreen setHidden:YES];
}completion:^(BOOL finished)
{
[self.welcomScreen removeFromSuperview];
}];

UIViews not animating when changing between each other

I have 2 views which are the same size, with colourPreview set as a subview of self.view. This is my code for showing the animation:
-(void)startEditingHexLabel {
[UIView animateWithDuration:0.3
animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:hexTextView cache:YES];
[colourPreview removeFromSuperview];
[self.view addSubview:hexTextView];
[self.view sendSubviewToBack:colourPreview];
}
completion:^(BOOL finished){
[hexText becomeFirstResponder];
}];
}
But it just changes to the new view without a transition. Any ideas?
Have you tried using transitionWithView:duration:options:animations:completion: (docs here). Seems this is preferred in iOS 4.0+. There's an example of how to use it in those docs.
If you want to use your current method, I think forView in [UIView setAnimationTransition:forView:cache:] needs to be the superview of the views you want to animate. In your case, this looks to be self.view. Full docs here.
HTH

User interaction with uiview and animation completion blocks

I have the following code:
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
imageView.bounds = endBounds;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
imageView.bounds = startBounds;
}
completion:^(BOOL finished) {
[imageView removeFromSuperview];
}];
}];
Additionally I have:
[imageView setUserInteractionEnabled:YES];
and a tap gesture recognizer set that will handle the user tapping on imageView. While the first animation is happening, the gesture recognizer fires as I would expect. But if I try and tap imageView during the chained animation from the completion block, nothing happens even though I have set the appropriate option.
Anyone have any thoughts? I've googled and can't find an answer.
When using the new animation blocks, if you want user interaction to be enabled during the animation, you have to set it in the options mask. For example:
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{ myView.alpha = 0.5; }
completion:NULL];
I came up with a solution:
I wrap the UIImageView in a UIView (I subclass UIView) with the same bounds/center point as the image. Then I attach the gesture recognizer to the wrapper, instead of the image. Because the wrapper's bounds rectangle/center point never change for the duration of the animation, it's always available as the target of a gesture.
This works quite well.
-j
Do you see the same behaviour if you use:
+ [UIView setAnimationDidStopSelector:]
instead of using blocks?

making an object move without cocos2d?

I am trying to make a button move in my application without having to implement the cocos2d framework in my application. I am making a data application and dont want to add any other game elements except for a button that moves. Does anyone know how to do this?
To move any object you simply have to give it a new frame by setting
myView.frame = CGRectMake( x, y, width, height );
You can even easily have the phone animate it for you without any added frameworks by wrapping it with [UIView beginAnimation...] blocks.
Here's an example:
[UIView beginAnimations:#"moveButton" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5];
someButton.frame = CGRectMake( newX, newY, newWidth, newHeight );
[UIView commitAnimations];
You can move a view directly using
[theview setFrame:CGRectMake(x,y,w,h)];
or you can use animations to do so a in cooler way (this should be the iOS 4.x Way)
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionLayoutSubviews
animations:^{ [theview setFrame:CGRectMake(x,y,w,h)]; }
completion:^(BOOL finished){
[self iAmDoneAnimatingNowICanDoSomethingElse]; }];