19 мар. 2015 г.

Анимация UITextField, проверка на число / UITextField not number animation

Проверка UITextField на числовой тип (float) и простая анимация если введенные данные не являются таковыми.




// Проверка введенных данных на числовую форму

- (void)textIsValidValue:(NSString *)text textField:(UITextField *)textField {
    
    BOOL result = NO;
    NSScanner *scanner = [NSScanner scannerWithString:text];
    
    if (!(result = [scanner scanFloat:NULL] && [scanner isAtEnd])) {
        
        _isWrongText = YES;
        [self animateTextFieldOnError:textField];
    
    } else { _isWrongText = NO; }

}


// Анимация поля и смена цвета текста если обнаружена ошибка

- (void)animateTextFieldOnError:(UITextField *)textField {
    
    CAKeyframeAnimation * anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    anim.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-10.0f, 0.0f, 0.0f)],
                    [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f)]];
    anim.autoreverses = YES;
    anim.repeatCount = 2.0f;
    anim.duration = 0.1f;
    
    [textField.layer addAnimation:anim forKey:nil];
    textField.textColor = [UIColor redColor];
}

0 коментарі:

Отправить комментарий