I need to move text in UITextField (from right to left, and then back) with animation like in Safari App in iOS 7 (see the screenshots below).
Is there any way to do it?
I know that I can set textAlignment, but it won't be animated.
Maybe I should change leftView size in UITextField? But I'm not sure how to calculate size, when text should be aligned on the center. Please, help.~
(source: cs424818.vk.me)
You can achieve this behavior by doing this:
- (void)viewDidLoad
{
// add this to viewDidLoad
// keep the alignment left justified
[self.yourTextField setTextAlignment:NSTextAlignmentLeft];
[self.yourTextField addTarget:self action:#selector(textFieldDidChange)forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange
{
// this will cause your textfield to grow and shrink as text is added or removed
// note: this will always grow to the right, the lefthand side will stay anchored
[self.yourTextField sizeToFit];
}
Then when you want to move your UITextField from the center to the left you can simply do:
// 1 second animation
[UIView animateWithDuration:1.0 animations:^{
// move to the left side of some containing view
self.yourTextField.center = CGPointMake(containingView.frame.origin.x + self.yourTextField.frame.size.width/2, self.yourTextField.center.y);
}];
Likewise to move from the lefthand side to the center you can do:
[UIView animateWithDuration:1.0 animations:^{
// move to the center of containing view
self.yourTextField.center = CGPointMake(containingView.frame.size.width/2, self.yourTextField.center.y);
}];
Hope this helps!
Related
I have a problem that my custom navigation bar appears for the first time with animation. I think it happens because I use auto-layout and it animates it self into the state of landscape or portrait. But I want to have a functionality, that after first time I enter the screen everything is still, and where it belongs. and after that if I turn the screen or if I do something all the animations appears like now.
Is there a good think to omit the first animations when the view creates itself?
The animations are : labels floating from left to right. and 1 label appears as from 0px width and height it scales into 100% width and height
code:
[self.navigationController.navigationBar addSubview:self.topBar];
self.topbar is UIView. I have added it to block [UIView performWithoutAnimation:^{ }]; but it does not helps, everytime the view appears for the first time I have my labels floating from left to right.
The answer is that I used some methods [self layoutIfNeeded]; After setting the constraint. The solution is to have a method set like this:
- (void) yourAnimation:(BOOL)animated{
if(animated){
[UIView animateWithDuration:0.3 animations:^{
//your animated code
//[self layoutIfNeeded];
}];
}else{
[UIView performWithoutAnimation:^{
//your stuff without animation
}];
}
}
I'm making this type of screen.
I've used 3 UIButton and programmatically gave them border. The little red line is a UIView. I've provided a constraint to UIView Equal Widths as Trending Button.
Now my question that when user taps the next button this UIView should move to the next one. View Controller is 600x600 and next button is at 200x0 so if I change the value of UIView to 200 that will be hard coded and it'll alter according to the screen size. I want it to be perfect and don't know any other way.UPDATE:I'm using AutoLayout so for this purpose I used [self.buttonBottomView setTranslatesAutoresizingMaskIntoConstraints:YES]; and when I run the app the buttons were messed up like in the screenshot. I've applied some constraints on buttons.
You can use NSLayoutConstraint ,change it's LeadingConstaint when you tap on the next button.
Code:
- (void)changeSelectedByTapView:(UIView *)tapView {
_currentSelectedView = tapView;
self.lineImageViewLeadingConstaint.constant = tapView.frameX;
self.lineImageViewWidthConstaint.constant = tapView.width;
[UIView animateWithDuration:0.5f animations:^{
[self.lineImageView.superview layoutIfNeeded];
}];
}
change frame of your UIView.
view.frame = CGRectMake(0+buttonWidth*i, y, width, height);
i is index which is 0 for Trending, 1 for Made by the fans and 2 for your rules.
Also when you change frame for your view, it is shown only on one button at a time.
When I animate the location of my UITextField subclass, the text in the field jumps out of step with the location of the UITextField holding it, which is very jarring and I can't figure out why it happens:
My subclass adds a button to the right hand side of the text field that when tapped calls a method on the view controller, which changes the textfield border colour and moves the text field up the screen by changing the top space constraint of the text field, and a couple of other views located above it.
[UIView animateWithDuration:0.3
animations:^{
self.tableViewBottomSpaceConstraint.constant = 0;
self.mainLabelHeightConstraint.constant = 0;
self.subLabelTopSpaceConstraint.constant = 0;
self.postCodeFieldTopSpaceConstraint.constant = 12;
[self.view layoutIfNeeded];
}];
i fixed this. change your code to this. It will work.
[UIView animateWithDuration:0.3
animations:^{
self.tableViewBottomSpaceConstraint.constant = 0;
self.mainLabelHeightConstraint.constant = 0;
self.subLabelTopSpaceConstraint.constant = 0;
self.postCodeFieldTopSpaceConstraint.constant = 12;
[self.view layoutSubviews];
}];
or if this doesn't work then instead of
[self.view layoutSubviews];
use the parent view of the text field and layoutSubviews. In my case this worked. I think this answer is eligible for the bounty. Thanks.
EDIT:
I found the reason of this. When we are trying to layoutIfNeeded it means its changing the outer layout first and after it changes its layout its changing the inner text of UITextField because we are not forcefully changing layout of all subviews of that text field. After all UITextField is made up of UIView and Label and other base elements. But when we trying to layoutSubviews its changing layout of all subviews simultaneously. Thats why the bouncing is gone.
It looks like for some reason your UI isn't in a settled state before the animation is triggered.
Try adding a call to [self.view layoutIfNeeded]; before your animation block, and use [self.view layoutIfNeeded]; at the end of your block rather than [self.view layoutSubviews]; which shouldn't really be used in this context.
For iOS 9 or above, this solve the issue
Objective-C:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded]; //Fixes iOS 9 text bounce glitch
//...other stuff
}
Swift:
func textFieldDidEndEditing(_ textField: UITextField) {
textField.layoutIfNeeded() //Fixes iOS 9 text bounce glitch
//...other stuff
}
Google takes me here. Under #Mahesh Agrawal 's answer, #Andy comments a link that links to Here which is the best solution I can find for this problem.
I am trying to make a UITableView appear when a user taps on a button and disappear when the button is re-tapped.
I implemented the following code but nothing seems to appear when I tap the button.
- (IBAction)dropDown:(UIButton *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
self.markersTableView.transform = transfrom;
self.markersTableView.alpha = self.markersTableView.alpha * (-1) + 1;
[UIView commitAnimations];
}
What may potentially be the issue?
EDIT:
I was able to make the UITableView appear and disappear by adding self.markersTableView.hidden = YES; in viewDidLoad() and self.markersTableView.hidden = NO; in the IBAction method.
However, the table view disappears when I initially tap on the button as shown in the screenshot:
The fading of the rows is an indication it is moving down the screen, and then it disappears.
It only reappears when I re-tap on the UIButton the 2nd time.
Any clues?
Don't use a transform on your table view. That will make it LOOK like it's in a different position, but it won't respond to taps correctly.
Instead, use the newer UIView block-based animation methods and change the view's center.
The code might look like this (if you're NOT using auto-layout)
[UIView AnimateWithDuration: .2
animations: ^
{
CGPoint center = self.markersTableView.center;
center.y += 200;
self.markersTableView.center = center;
}
];
If you're using auto-layout, though, that won't work and you will need to animate the view's position using constraints.
From memory, here's an outline of how to do auto-layout based animations: You would create a vertical position constraint on your table view and connect the constraint to an IBOutlet in your view controller. In your animation code you change the constant of the constraint (the vertical position) and then in the animation block, send the view a needsLayout message.
I have a view with
UIScrollView -> image view
UILabel
I add a swipe gesture on my ScrollView to go to the next image. It works.
My label is the title of my image.
When i swipe, i want my label disappear on the left and appear on the right and set the new title.
Do I need 2 different labels?
How can I animate my label?
Make your label the subview of your scrollView. Then it will scroll when your image does.
You could use
[UIView animateWithDuration:1 animations^{ label.frame = leftOutsideView;} completion^(BOOL finished){ label.frame = rightOutsideView; label.text = newCaption; [UIView animateWithDuration:1 animations^{ label.frame = originalPlace;}];}];
(This is pseudo code)
Try to use animations in that context, it looks good.