How to check if UIView is at coordinates - objective-c

I have a tableView with textFields inside of them, and I have animation that slides the tableView up when the textFieldDidBeginEditing method is called. Here is my code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.headerView.alpha = 0.2;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.18];
[UIView setAnimationDelegate:self];
self.theTableView.frame = CGRectMake(0, 130, 320, 209);
[UIView commitAnimations];
}
The problem is that the animation happens whenever one of the cells is tapped, but I only want it to happen the first time a cell is tapped. So I was thinking that I should make an if statement that checks whether the tableView is at the (0, 130) coordinates. Does anyone know how I would do this?

For an exact comparison, CGPointEqualToPoint() will suffice. For a broader comparison (checking if the rect of the view contains the given point), CGRectContainsPoint() is perfect.
Of course, all of this is a little much. Subclass your view and keep a flag on it to track whether or not they've been slid up. Not only does it keep your code more self-contained, but it keeps the logic for the view out of your controller.

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.

Get current frame of UIView while animated

I have a UIView and I want to get it's current frame while it's animated.
I am using a basic animation:
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:3];
_testView.frame = CGRectMake(0, 100, 40, 40);
[UIView commitAnimations];
and I have a button which should add another UIView at the current position of the first one while animated, but when I press the button, it adds it to the end position of the animation...
Any suggestions?
Thank you in advance!
Most probably you're setting the newly added UIView properties based on the animated view's modelLayer. Let's try to setup the UIView properties based on the animated view's presentationLayer:
The layer object returned by this method provides a close approximation of the layer that is currently being displayed onscreen. While an animation is in progress, you can retrieve this object and use it to get the current values for those animations.
CALayer Class Reference

UIView animation not going back to original state

I am trying to make UIView glow back and forth twice. My animation below works but just after it has finished it goes to the state that I animated it to, and not the original. I set autoreverses to YES so why is it not going back to original state ?
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
textDetailView.layer.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.3].CGColor;
[UIView commitAnimations];
The animation reverses but the value doesn't. After the animation completes it removes itself from the view (actually from the views layer) and the view appears to have the properties that have been set on it.
You could set the value back in a completion block or in the selector for the older UIView animation that you are using.
Alternatively, for animations where the value doesn't change you could benefit from using Core Animation (which doesn't hangs the value (after the animation the view goes bak to its original state)).
You would need to import QuartzCore.framework and include QuartzCore/QuartzCore.h to get it to work.
The could would also need to be updated to something like this:
CABasicAnimation *myColorAnimation = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
[myColorAnimation setAutoreverses:YES];
[myColorAnimation setRepeatCount:2];
[myColorAnimation setDuration:0.3];
[myColorAnimation setToValue:(id)[myColor CGColor]];
// the default "from value" is the current value
[[textDetailView layer] addAnimation:myColorAnimation forKey:#"myColorKey"];

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).

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?