iOS8动画代码不再起作用

时间:2014-09-08 22:18:57

标签: objective-c uitextfield ios8

我用来为UITextField制作动画的代码对我有用,直到XCode版本6.0(6A215l)。现在XCode6 Beta7没有重新定位UITextField。我该怎么做才能让它发挥作用? 代码如下:

@interface TableConfigViewController ()
@property(nonatomic, strong) IBOutlet UITextField                   *customTableName;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.customTableName setDelegate:self];
}
- (void)animateTextField:(UITextField*)textField
                      up:(BOOL)up
{
    const float movementDuration = 0.5f;
    const int movementDistance = 380;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: @"animation" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:movementDuration];
    self.customTableName.frame = CGRectOffset(self.customTableName.frame, 0, movement);
    [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField*)textField
{
    [self animateTextField:textField up:YES];
}

- (void)textFieldDidEndEditing:(UITextField*)textField
{
    [self animateTextField:textField up:NO];
}

非常感谢您提前

1 个答案:

答案 0 :(得分:0)

不确定这是否是您的问题的根源,但您应该考虑使用基于块的API(自iOS 4.0开始提供)用于UIView动画。

来自Apple关于beginAnimations:context:

的文档

"在iOS 4.0及更高版本中不鼓励使用此方法。您应该使用基于块的动画方法来指定动画。"

例如(未经测试):

- (void)animateTextField:(UITextField*)textField
                      up:(BOOL)up
{
    const float movementDuration = 0.5f;
    const int movementDistance = 380;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView animateWithDuration:movementDuration
                     animations:
                     ^{
                         self.customTableName.frame = CGRectOffset(self.customTableName.frame, 0, movement);
                     }
    ];
}