Objective c, reposition a view above the keyboard in one single animation - objective-c

I have a custom view at the bottom of my screen, i want to reposition it above the keyboard when it opens up.
I know i can register for the 'KeyboardWasShown' notification and then reposition the view or as the apple documentation suggests to use a scrollview with scrollRectToVisible, but the problem i have with both options is that it is not done as part of the keyboard animation.
I can see the keyboard appear and only a second later the view is repositioned or scrolled in to view.
Another option i tried was to set this view as InputAccessory for the textView im editing, This works very well but my view will not be visible once the keyboard is closed, and i need it to be available all the time.
I suppose i can create two instances of this view and have one as inputAccessory and the other just sitting in the bottom, but i really dont like this solution and these views have a state that will have to be synced between the two instances
Can anyone suggest an alternative solution ?

You better use UIKeyboardWillChangeFrameNotification notification.
Add observer first.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
The selector will be...
- (void)keyboardFrameChange:(NSNotification *)notification {
CGRect begin = [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect end = [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardSize= begin.size.height;
BOOL isShowing = begin.origin.y > end.origin.y;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
// do view animation here.
[UIView commitAnimations];
}
The animation of your view and the keyboard will be sync.

Related

Change background colour BEHIND ViewController frame (when frame.origin is altered)?

I have an app which intakes client details, built for iPad. When the user taps a UITextField towards the bottom half of the ViewController, the frame programatically shifts upwards so that the fields aren't hidden behind the keyboard. (I tried to implement a UIScrollView but just cannot seem to get it working.) The slight issue I'm having now is when the frame shifts up, you can vaguely see the black behind it. This isn't a huge issue because I have changed the animation time and the black background is barely visible, but I have a feeling there is a more elegant solution.
Here is my code to shift the frame:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Animate frame up if lower textfields are tapped
if (([textField isEqual:_emailField]) || ([textField isEqual:_dobField]) || ([textField isEqual:_niNumField])) {
[UIView beginAnimations:nil
context:NULL];
[UIView setAnimationDuration:0.35f];
CGRect frame = self.view.frame;
frame.origin.y = -210;
[self.view setFrame:frame];
[UIView commitAnimations];
} else {
// Return frame to original position if other textfields are tapped
[self dismissKeyboard];
}
return YES;
}
- (void)dismissKeyboard {
[UIView beginAnimations:nil
context:NULL];
[UIView setAnimationDuration:0.15f];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
Here is a brief screenshot of what I'm trying to describe. Left picture is the ViewController normally, right picture is when the frame has been shifted upwards. You can see (vaguely) almost in line with the second row of characters is where the frame stops.
I realise this doesn't seem like an issue because it is barely visible at all, but I have had to speed up the animation hiding the keyboard or else the frame drags behind and the black background becomes visible. I am wondering: is there a way to change this colour? Or is that something we don't have access to? If anyone can suggest a more elegant method for what I am trying to do, I'd gladly take a better solution. Thanks in advance.
Your UIViewController's view is added to the main UIWindow. So you should be able to achieve what you want by changing the UIWindow's background color.
In the UIApplicationDelegate:
self.window.backgroundColor = [UIColor whiteColor];
However, what you're doing isn't the best way to solve the problem of the keyboard covering up the text fields. You should use a UIScrollView or a UITableView to manage this view and use content insets to shift the view up or down.

Animating an image view to slide upwards

I am attempting to make an image view (logo below) slide upwards by 100 pixels. I am using this code, but nothing happens at all:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
logo.center = CGPointMake(logo.center.x, logo.center.y - 100);
[UIView commitAnimations];
This code is in the viewDidLoad method. Specifically, the logo.center = ... is not functioning. Other things (like changing the alpha) do. Maybe I'm not using the right code to slide it upwards?
For non-autolayout storyboards/NIBs, your code is fine. By the way, it's now generally advised that you animate using blocks:
[UIView animateWithDuration:3.0
animations:^{
self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100.0);
}];
Or, if you want a little more control over options and the like, you can use:
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100);
}
completion:nil];
But your code should work if you're not using autolayout. It's just that the above syntax is preferred for iOS 4 and later.
If you're using autolayout, you (a) create an IBOutlet for your vertical space constraint (see below), and then (b) you can do something like:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static BOOL logoAlreadyMoved = NO; // or have an instance variable
if (!logoAlreadyMoved)
{
logoAlreadyMoved = YES; // set this first, in case this method is called again
self.imageVerticalSpaceConstraint.constant -= 100.0;
[UIView animateWithDuration:3.0 animations:^{
[self.view layoutIfNeeded];
}];
}
}
To add an IBOutlet for a constraint, just control-drag from the constraint to your .h in the assistant editor:
By the way, if you're animating a constraint, be sensitive to any other constraints that you might have linked to that imageview. Often if you put something right below the image, it will have its constraint linked to the image, so you may have to make sure you don't have any other controls with constraints to your image (unless you want them to move, too).
You can tell if you're using autolayout by opening your storyboard or NIB and then selecting the "file inspector" (the first tab on the right most panel, or you can pull it up by pressing option+command+1 (the number "1")):
Remember, if you planning on supporting pre-iOS 6, make sure to turn off "autolayout". Autolayout is an iOS 6 feature and won't work on earlier versions of iOS.
have u try
logo.frame = CGRectMake(logo.frame.origin.x, logo.frame.origin.y - 100,logo.frame.size.width,logo.frame.size.height)

How can I touch and drag an animated image? here's my animation code:

- (void)viewDidLoad
{
[super viewDidLoad];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint abc =chem1.center;
abc.y= 480;
chem1.center=abc;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint def =chem2.center;
def.y= 480 ;
chem2.center=def;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint hij =nat1.center;
hij.y= 480;
nat1.center=hij;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint klm =nat2.center;
klm.y= 480;
nat2.center=klm;
}
I'd really appreciate the help
There are two parts to your question: how to tap an animated image and how to drag an image.
How to tap an animated image.
Background
When you are animating a view (or more accurately its layer) the property that you change (in your case the position (by setting center)) is changed to its final value right away. If you only animate (like with a CABasicAnimation, then the properly might not change at all.
This means that if you try to tap the view (or layer) it will be at the position that you moved it to (its final position (despite any duration or repeat count of the animation).
Hit test a moving layer
To be able to hit test something that is moving you need to hit test against the presentationLayer of the layer of the view that you are trying to tap.
CGPoint touchPoint = [gestureRecognizer locationInView:[self view]];
if ([[[[self movingView] layer] presentationLayer] hitTest:touchPoint]) {
// Yeah, you hit the moving (animating) layer!
}
Now that the user has tapped a moving view (image in your case) you can begin to handle the drag.
How to drag an image
When you are dragging an image you move the layer to wherever the users finger is dragging the image.
Remove to animation that moves the view
First you should cancel the ongoing animation for that view (having a name for the animation enables you to cancel only one animation.
[[[self movingView] layer] removeAllAnimations];
Or if you have set a name to the movement animation.
[[[self movingView] layer] removeAnimationForKey:#"nameOfMovingAnimation"];
Move the view to the location of the users finger
Next you should (continuously) update the position of the view as the user moves its finger. You should not animate this since it will increase the time between the movement of the finger and the view and will make it seem like your application is lagging. Animating while dragging does not make it look smooth.
Note: since your question doesn't say what you want to do once the user are dragging the images or what happens when they drop them. I can't go much further in the explanation.
Need more help?
Search the web (or StackOverflow) for something like: "iOS drag view" and you will probably find more information about how to drag your imageViews.
Otherwise ask another question on StackOverflow once you have gotten this far (i.e. you can tap on the moving images and they stop moving).

Flipping a view that is contained on a view setup

I currently have a xib that is a standard view that has another view contained on top. I want the user to be able to click on this subview, and then have this subview flip over when pressed. I currently have code that flips the view around, but since I'm not explicitly assigning a new view, it just flips over and displays the same view. Here is my code when the button is pressed:
- (IBAction)flipCard:(id)sender
{
NSLog(#"Subview button was clicked!");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.frontOfView
cache:YES];
[UIView commitAnimations];
}
The biggest question is how to set this up. Do I need to create an entire view programmatically and then just call [[self view] addSubview: backOfView]*? Or is there a way to do this with interface builder?
*Note I haven't actually create a "backOfView" object, I'm not sure how I should create one at this point.
Try this one?
[UIView transitionFromView:self.frontOfView
toView:self.backOfView
duration:0.4f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];

How to implement the effect of popping up tableview from bottom of screen?

I'm a newbie of Xcode and Objective-c. Recently I wanna do a task about popping up tableviews. When a button is pressed, the tableview should pop up from the bottom of the screen. The tableview will contain just a few items so I don't wanna it occupy full screen. I read the manual and found UIModalPresentationStyle, but it seems not fulfill my requirements. So what's the more accurate methods I can use?
While #Bharat J's answer is correct, those methods are deprecated in iOS 4.0.
The latest and greatest implementation is + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
Implemented as:
[UIView animateWithDuration:0.5 animations: ^{
tableView.frame = newFrame;
}
];
You can use below code to create an animation block . Set the frame for the table view as (0,480,320,0) When you hit a button change the frame of the table view in the animation block and make it to CGRectMake(0,200,320,280) or something .
[UIView beginAnimations:#"AnimateTableView" context:view];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tableView.frame = CGRectMake(0, 200 , 320, 280);
[UIView commitAnimations];
The same animation block for hiding it again but with the frame again begin set to CGRectMake(0,480,320,0). This should work in your case .
Another option would be to use a UIActionSheet and add the uitableview as a subview.
Here is a very similar question about how to add a UIPickerview with similar effect.
Add UIPickerView & a Button in Action sheet - How?