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.
Related
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];
I have a button on view1 and and I want to move it to view2 , but with animation from top. My view1 is on on the top half of the screen and view2 on the bottom half of the screen. So the superview changes as you can see. I have tried multiple approaches but the button just appears from the left or right but not from its origin. Any help is appreciated.
Make the superview not clip their contents by setting clipsToBounds to NO.
Then you animate the button's frame and when the animation is done you remove it from view1 and add it to view2.
[UIView animateWithDuration:0.2f
animations:^{
button.frame = newFrame;
}
completion:^(BOOL finished){
CGRect rect = [view1 convertRect:button.frame toView:view2];
[button removeFromSuperview];
[button setFrame:rect];
[view2 addSubView:button];
}];
I have accomplished this very result using the following code:
CGRect tabFrame = tab.frame;
CGRect viewFrame = [mainWindow convertRect:tabFrame toView:currentView];
tab.frame = viewFrame;
[tab removeFromSuperview];
[currentView addSubview:tab];
This code converts tab's frame from mainWindow into currentView. This works perfectly, aside from this bug. If you don't encounter that problem, or find out how to solve it I would really appreciate that.
All you need to do from that point is apply your normal animation.
I'm trying to animate a UIButton to move up the screen. At any point the user can touch it. However, it doesn't seem to respond to touches while moving, only at the start and end of its animation. I guess this is because the button itself isn't moving, just the image of it. Any ideas how I can solve this? Here's my code so far. Thanks!
- (void)viewDidLoad {
[self newBubble];
[super viewDidLoad];
}
- (void)newBubble {
UIButton *bubble = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
bubble.frame = CGRectMake(10, 380, 50, 50);
UIImage *bubbleImage = [UIImage imageNamed:#"bubble.png"];
[bubble setBackgroundImage:bubbleImage forState:UIControlStateNormal];
bubble.imageView.contentMode = UIViewContentModeScaleAspectFit;
[bubble addTarget:self action:#selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:bubble];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
[UIView commitAnimations];
}
- (IBAction)bubbleBurst:(id)sender {
NSLog(#"Bubble burst");
UIButton *bubbleBurst = sender;
[bubbleBurst removeFromSuperview];
}
The +[UIView animateWithDuration:delay:options:animations:completion:] method takes a UIViewAnimationOptions argument. If you include UIViewAnimationOptionAllowUserInteraction, you should be able to press the button while it animates:
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
} completion:nil];
Well after posting a similar question, I found the answer here
Basically, you have to move the button frame by frame using an NSTimer. In my case I was worried there would be too many NSTimers involved moving each button, but in the end I used a single timer which loops through an array of buttons and moves them all one by one.
I'm pretty sure you would have to use Core Animation if you want the button to respond to touches while it is moving, but I have never done this so this is really more a tip then an answer!
I agree. I think the problem is that you're not using Core Animation. I can't believe you couldn't figure that out for yourself.
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).
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.