UIView animateWithDuration... not working in iOS 8 - ios

I have a problem with [UIView animationWithDuration] when trying to animate a button when the keyboard come up. I have a notification tell me when the keyboard comes up then I have the ViewController call this method in the view. The button animates but not correctly. It just animates from a random position on the screen to the position it was at previously. The reason I bring this up again is because I've seen answers from other posts saying that setting the view's translatesAutoresizingMaskIntoConstraints property to YES in the ViewController will fix the problem, but it doesn't fix the problem.
[self.view setTranslatesAutoresizingMaskIntoConstraints:YES];
The animation is below:
- (void)animateForKeyboardAppearanceWithKeyboardSize:(CGSize)keyboardSize; {
[UIView animateWithDuration:0.3f
animations:^{
[self.sendSMSButton setFrame:CGRectMake(0,
self.frame.size.height - keyboardSize.height - self.sendSMSButton.frame.size.height,
self.frame.size.width,
self.sendSMSButton.frame.size.height)];
}];
}
Anyone know what's happening? Thanks in advance.

Have you ensured that this method isn't being called repeatedly? Also try subclassing the button, override -setFrame and double check to see if something else is setting the frame of your button while you are trying to animate it.

Related

Appearing keyboard changes view's frame

in my app im tapping on a navigation bar button and as a response i move the view beneath the navigation bar 60 px down (the delta var in the code), and under the view that moves down, appears a UITextField and at the same time show the keyboard.
that's how i achieve it:
-(void)btnSearch_Click:(id)sender{
float delta = _viewBtnSort.frame.size.height;
CGRect rect = rectSearch;
rect.origin.y += delta;
[UIView animateWithDuration:0.3f animations:^{
[_viewSearchResults setFrame:rect];
}completion:^(BOOL finished) {
[buttonSearch setEnabled:NO];
[_txtFieldSearch becomeFirstResponder];
}];}
Problem
it works fine for a few times and then for some unexplained reason(couldnt find so far why), when the keyboard appears it pushes my view back up i and i dont get the effect i want.
anybody got any ideas? thanks!
so i kept looking for a fix, and did it using this iOS8: What's going on with moving views during keyboard transitions? answer.
i changed my constraint instead of the frame and now it seems to work fine.

Object returns to the original position (after an animation) when keyboard appears

I have this strange new issue: this code (that works perfectly)
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect frame = _logoClaim.frame;
frame.origin.y -= 180;
_logoClaim.frame = frame;
} completion:NULL];
moves a view that contains a UIImageView and an UILabel to the top of my self.view.
The view that moves unhides a UITextField.
When I try to write text into the UITextField, obviously appear the keyboard.
And at this moment, the view animated before, returns to the original start position!!!
What is the reason?
Put a completion block in your animation and check the finished value. The keyboard is cancelling the animation and the finished bool will be NO. I would disable input in the UITextField until the animation is completed. Do this in your completion block.
EDIT
Looking at your duration and re-reading the question,I may be mistaken with what I think you mean. If this answer is incorrect I will remove it.
Also, search your code for _logoClaim.frame in case you are adjusting it onKeyboardWillAppear
You would need to create outlet for its bottom/top space constraints and update the constant value of it inside the animation block.
[UIView animateWithDuration:0.4
...
animations:^{
/*Update the value of the constraint outlet supposing it to be a bottom space constraint*/
_layoutConstraintBottom += 180;
[self.logoClaim layoutIfNeeded];
} completion:NULL];
Do not forget to call -layoutIfNeeded for your animating view.

hide/unhide UIPicker with the same animation

im currently making a project where i need to hide my UIPicker, i've done all the hiding and animation stuff with this code,
on button press event this code is written:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, -200);
self.picker.transform = transfrom;
self.picker.alpha = self.picker.alpha * (-1) + 1;
[UIView commitAnimations];
and on view did load initialized;
self.picker.alpha = 0;
[self.view addSubview:self.picker]; //i dont really need this one
so here the picker will appear from button to top (0,-200) but when i click the button again it dissapears right away as self.picker.alpha goes to 0. i tried also putting animation delay and [UIView setAnimationDelay:3]; and tried also to set the animationDuration more but it does not effect when its going to hide.
i would like to know if how do i make the UIPicker hide in the same manner as it appears.
hope its not so confusing.
thanx
To reset the view to its original position you would want to reset the transform to CGAffineTransformIdentity.
As #Wain mentioned, you have changed the transform property while appearing the UIPickerView.
When you want to disappear the Picker View on click of a button you need to set its transform again.
Here is a link to learn animation in UIView in iOS:
http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html

fade in/fade out UISegmentedControl in XIB

I have a UISegmentedControl set up in my XIB. I want it to appear on viewDidLoad and if the user taps the area of the screen it's in, and then to disappear if the user taps it again or to fade out if the user leaves it alone.
In looking around for how to manage this I've found a lot of stuff about fading UIViews, but not as much on fading individual subviews, and little at all on fading elements in the XIB. I tried to adapt the UIView stuff but failed.
How can I make this work?
EDIT: Okay, I've got the appearance at viewDidLoad and the fade out working. But when the user taps the area where the UISegmentedControl is (now invisible because alpha=0), nothing happens. This is the code I'm using:
- (IBAction)tapInvisibleSegContr
//This is connected to the UISegmentedControl with the action Touch Up Inside. Until now, the segmented control has been at alpha=0 since fading after viewDidLoad.
{
self.segContrAsOutlet.alpha=1.0;
[self fadeMethodThatWorksInViewDidLoad];
NSLog(#"Yup, tapped.");
}
I'm not even getting the NSLog. I've got the action hooked up to the UISegmentedControl, with the action Touch Up Inside. What am I missing?
If it is resident in a xib, just put his alpha to 0, do the properly connections: an Outlet and an IBAction for value changed
Then in the viwDidLoad right after [super viewDidLoad] write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 1;}];
Inside the IBAction right after you code the answer before the last } write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 0;}];
This is the easiest method.
Bye
In the xib set your control's alpha to 0.0, then use UIView animation methods to animate its alpha to 1.0. For example:
[UIView animateWithDuration:1.0 animations:^{
self.segmentedControl.alpha = 1.0f;
}];
EDIT: To your problem with not getting the action called, try attaching it for the value changed control event - I don't think UISegmentedControl sends for touch up inside.

UIScrollView cancel/stop setContentOffset:<CGPoint>animate:YES animation

I have a UIScrollView that I'm scrolling using setContentOffset:animate:YES but if the device rotates I want to be able to stop it. Is this possible? Or how would I go about writing my own implementation of the setContentOffset with animation? thanks!
scrollview.contentOffset = scrollView.contentOffset should stop the scrollView at its last scroll position.
Do this in willAnimateRotationTo..... in your viewController
If you have a UIScrollView sub-class you could do it in setFrame: , this is assuming the frame of the scrollview changes upon orientation change.
try to use this instead:
[UIView animateWithDuration:.25 animations:^
{
// replacement for setContentOffset:animated:
self.scrollView.contentOffset = scrollPositionBeforeKeyboardAdjustments;
}];
if you are struggling with the keyboard, see my answer
How to make a UITextField move up when keyboard is present?

Resources