iphone - make a list of buttons appear from below - iphone

I would add a drop down list of buttons that appear/disappear from below side, as the "share button" in the native iphone. How can I do that?
Thanks in advance.

The share view is an UIActivityController object and you can only use it to share and so on.
The best thing is to create a custom UIView and place your buttons in it. Then you sinply animate it into the visible part of the screen and then again out. If you need a simple code example let me know.
Update:
// Create your UIView and connect in your storyboard/XIB
UIView *myView = [[UIView alloc] init];
// Add the UIButtons to your UIView
UIButton *button = [[UIButton alloc] init];
[myView addSubview:button];
// Animate the UIView that contains the buttons
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myView.frame = CGRectMake(0, 740, 768, 264);
[UIView commitAnimations];

Related

Unable to animate just a frame to slide up on click of a button.

I am try to create an animation in my app which would slide up a frame to my desired position. The frame would be having other buttons and text on it as well.
I was unable to use actionsheet for this, as it starts from the bottom most part of the window. What i want to create is shown in the picture below.
the frame I want to animate is the one with Capture mode: text.
Click on the button on the tabView I want this frame to slide up, starting from the top of the tabview. and again clicking the button should make the frame disappear.
So far my code looks like
-(IBAction)setting
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor clearColor]];
[tempView setAlpha:.87];
tempView.hidden = YES;
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem I am facing here is that when I animate my frame to move, the main view moves along with it. Which is something I don't want it to do.
I am new to iPhone programming, please guide how can I fix this.
Or what would be a better way to do it.
Thanks
You don't need to make the view hidden, just add it as a subview after you set its frame to have y origin of 480. Also, don't use the old UIView animation methods; use blocks.
Here's an example of a view that starts with a y origin of 0. It gets positioned at negative y equal to its height, added as a subview then animated down to meet the keyboard coming up from the bottom of the screen.
// add to view while completely off-screen
CGRect unlockViewFrame = unlockView.frame;
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
unlockView.frame = unlockViewFrame;
[self.view addSubview:unlockView];
unlockViewFrame.origin.y = 0.;
[UIView animateWithDuration:0.3 animations:^{
unlockView.frame = unlockViewFrame;
}];
Here's the same thing in reverse, using a completion block to remove the view.
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
[UIView animateWithDuration:0.3 animations:^{
view.frame = unlockViewFrame;
} completion:^(BOOL completion) {
[view removeFromSuperview];
}];
Another thing I notice is that you are doing everything in code; consider doing it in Interface Builder. A smart man once told me that code you don't write can't have any bugs and he was right. It's easy to create a view in IB and instantiate it inyour code when you need it:
NSArray* nibViews;
nibViews = [[NSBundle mainBundle] loadNibNamed:#"UnlockView" owner:self options:nil];
UnlockView *unlockView = [nibViews objectAtIndex:0];
Also, consider using ARC. No more autorelease!
Your resetting frame of main view and not tempView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
tempView.frame=tmpFrame; //it should be tempView not self.view ie main view
[UIView commitAnimations];

Loading a view from the bottom of the window using animations

i wanted to use animations to load a view from the bottom of the window, i know this can be done via presentmodalViewController but in as per the requirement of my app its not valid as i only want to load the view at half of the window. So i used Animations and heres a look at what i did
-(void) displayPicker
{
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setFrame:CGRectMake(197, 199, 103, 37)];
[done setBackgroundImage:[UIImage imageNamed:#"button_0.png"] forState:UIControlStateNormal];
[done setTitle:#"Done" forState:UIControlStateNormal];
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
[pickerDisplayView addSubview:datePicker];
[pickerDisplayView addSubview:done];
//animating here
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[self.view addSubview:pickerDisplayView];
[UIView commitAnimations];
}
The view called pickerDisplayView has two components called Picker and Button, but the problem is that Picker is not working correctly and neither the view (pickerDisplayView) is loading in a smooth way.
#Matt: I have followed ur instructions and did as u advised here's what i did
-(void) animationIn
{
CGPoint p = {x:0,y:185};
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[pickerDisplayView setCenter:p];
[UIView commitAnimations];
}
But the prob is that the view is going at the top of the window and is not coming at exact y axis of 185. I took these coordinates from IB. Please help me sir
You will have to add the view offscreen and then animate it in.
// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];
[self.view addSubview:pickerDisplayView];
[self performSelector:#selector(animateIn) withObject:nil afterDelay:0.25];
// Declare animateIn
- (void)animateIn
{
UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[pickerDisplayView setCenter:onscreenPosition];
[UIView commitAnimations];
}
The value offscreenRect is a CGRect for the size and position where you want the view to start. onscreenPosition is a CGPoint for the position where you want the animation to finish. Animating the view's center is adequate since you don't want to change the size of the view while animating.
Also, I placed the animation in a separate method that gets called with -performSelector:withObject:afterDelay to ensure that the animation with fire. Sometimes you won't see the animation run if you start it in the same run loop where you added the view you're animating.
You need to init and addSubview before beginAnimations:. When you init pickerDisplayView position it so that the top of it is inline with the bottom of the superview. The animation will then slide up pickerDisplayView. Ideally you should create the view in a nib or in the loadView method. (I strongly recommend using nibs over 'loadView').
Do this in loadView (or move to a nib):
CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];
This is the animation:
[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];
Also I'd advice against using 'magic numbers' in the frame. Instead you can derive the pickerDisplayView frame from the superviews frame. If you do use 'magic' numbers be sure explain them in some way (ie assigning them to a appropriately named variable or a comment).

Fade to black within a uinavigationcontroller?

I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons).
How would I achieve this?
EDIT
Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.
// this is called from a programmatically constructed button.
// change (void) to (IBAction) if linking from IB.
- (void)fadeAndShow:(id)sender
{
// blackView is a #property which has been #synthesize-d
// do I really need to alloc and init this?
blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.alpha = 0.0;
[blackView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar.superview addSubview:blackView];
[UIView beginAnimations:#"fadeAway" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDidStopSelector:#selector(showNewScreen:finished:context:)];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
// I guess you could fade in somewhere in the new view controller.
// don't know how to fade back in this view tho... viewDidAppear?
NewViewController *controller = [[NewViewController alloc] initWithNibName:#"NewView" bundle:nil];
[self.navigationController setNavigationBarHidden:YES];
controller.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:controller animated:NO];
[blackView removeFromSuperview];
[controller release];
}
Off the top of my head (I haven't actually tested the following at all):
-(IBAction) buttonClicked:(id) sender
{
[UIView beginAnimations:#"myAnimation" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
Then, add your new view on top.
EDIT: too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.
You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).

iPhone : disabling other elements on TextField focus

Let's start with an example: when you enter the Contact program on your iPhone, then type on the TextField "Search", the keyboard is displaying itself and the tableView under is going dark (and deactivated). Then if I touch this dark deactivated list, the keyboard hides itself and the list becomes normal.
I would like to reproduce this functionality, but I don't know how.
Is there a Cocoa method, or do I have to re-develop that by myself ?
Thanks for your advices.
Mart
I would even set the UIView to a uibutton so you can dismiss the keyboard when you want. Oh, That was there already!
OK, here's a bit of code that demonstrates how to add the button itself:
CGRect frame = CGRectMake(0,
0,
[self parentViewController].view.bounds.size.width,
[self parentViewController].view.bounds.size.height);
imageView = [[UIButton alloc] initWithFrame:frame];
[imageView setAlpha:0];
imageView.opaque = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[imageView setAlpha:0.8];
imageView.backgroundColor = [UIColor blackColor];
[UIView commitAnimations];
[imageView addTarget:self action:#selector(lockedScreenAction:) forControlEvents:UIControlEventTouchUpInside];
[[self parentViewController].view addSubview:imageView];
I'm pretty sure you are going to need to develop that for yourself. Looking at the effect on my phone, I think I would have a completely transparent (but black) UIView overlaying my UITableView. Register for the UIKeyboardWillShow notification, and when you receive it, animate the opacity of that UIView to 70%. Reverse the process on UIKeyboardWillHide.

iPhone - flipping views shows a white background

I am using the flip animation to animate between two views in my viewcontroller. The problem is that the background shows a white blank background while the animation is taking place. I would like to show a black background.
I tried setting the background color of the main view to black both in IB and code. But the background is still white.
Can someone please help me.
Thanks.
Adding the code
[self setContentView:[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]];
contentView.backgroundColor = [UIColor blackColor];
[contentView addSubview:toolbar];
[self setView:contentView];
[contentView release];
frontView = [[FrontView alloc] initWithFrame:viewFrame];
[frontView setViewController:self];
[self.view insertSubview:frontView belowSubview:toolbar];
//Initializing the back view here too
//on button click, executing normal flip code
Even after this I get a white background
I think your issue might be that the UIWindow is shown during the animation. To fix this issue, set the background color of your main window. You can do this in code or in IB.
You begin by creating a fake main view, and set its background to black:
// Create the main view
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
Then, you create your front and back views, and add them to your main view:
// create front and back views
UIView *frontView = ...
UIView *backView = ...
If you are using IB, skip the previous step and add directly your views
// add the views
[self.view addSubview:backView];
[self.view addSubview:frontView];
Now do the flip animation as usual.
EDIT: Probably it does not work because in your code you are adding the frontView below the toolbar. Add first the backView, then the frontView and finally the toolbar using the addSubview: method. Then, use the following code to animate the flip:
- (IBAction) flipView{
// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// Animations
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// Commit Animation Block
[UIView commitAnimations];
}
Since the code performs [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; the order in which you add the subviews is relevant.