ios - 使用动画在 UITextField 中移动文本

标签 ios iphone objective-c uitextfield

我需要在 UITextField 中移动 text(从右到左,然后向后移动),就像在 iOS 7 中的 Safari 应用程序 (见下面的截图)。 有什么办法吗? 我知道我可以设置 textAlignment,但不会设置动画。

也许我应该更改 UITextField 中的 leftView 大小?但我不确定如何计算大小,当文本应该在中心对齐时。请帮忙~

1
(来源:cs424818.vk.me)

最佳答案

您可以通过这样做来实现此行为:

- (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];
}

然后当你想将你的 UITextField 从中心移动到左边时,你可以简单地做:

// 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);
}];

同样从左侧移动到中心你可以这样做:

[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);
}];

希望这对您有所帮助!

关于ios - 使用动画在 UITextField 中移动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20891627/

相关文章:

ios - 有没有办法以编程方式创建展开转场?

iphone - 可拉伸(stretch)图像拉伸(stretch)部分宽于1px

iphone - 当我调用 [Timer isValid] 或 [Timer invalidate] 时,NSTimer 崩溃

objective-c - 按其成员字典的键对数组进行排序

ios - 通过另一个类以编程方式将对象添加到 xib 文件

ios - 如何正确继承 UITableViewController

ios - 我的Tableview按字母分成几部分

ios - 如何向用户呈现警报?

ios - Xcode:存档在验证期间有不正确的包 ID 和配置文件

iPhone。 UITableView 和推送 View Controller