UITextField的占位符抖动效果

  • 1首先新建一个继承于UITextField的文件,相当于自定义UITextField这个控件


  • 2 可以在init初始化方法里,统一对textField设置一些属性

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.font = [UIFont systemFontOfSize:14];
    }
    return self;
}
  • 3对textField的组成要熟悉,他是由几个不用的View组成的,而我们要用到的是placeHoldLabel和DisplayLabel,怎么来获取呢,可以通过 runtime来得知,但是由于对runtime不了解,可以百度,得到key,根据KVC来获取
-(UILabel *)gtPlaceholderLabel
{
    return [self valueForKey:@"_placeholderLabel"];
}
-(UILabel *)gtDisplayLabel
{
    return [self valueForKey:@"_displayLabel"];
}
  • 4 只要得到了View,对view进行相关的动画细节就不说了,可以根据自己的需求去实现不同的动画效果,那到底应该什么时候去执行这些动画效果,那就得了解执行顺序,然后我们需要复写父类的俩个方法来来调用动画
//复写父类的方法
-(BOOL)becomeFirstResponder
{
    if (self.normalColor == nil) {
        self.normalColor = self.gtPlaceholderLabel.textColor;
    }if (self.selectedColor == nil) {
        self.selectedColor = self.gtPlaceholderLabel.textColor;
    }
    self.gtPlaceholderLabel.textColor = self.selectedColor;
    [self placeholderLabelDoAnimationWithType:self.gtAnimationType];
    return [super becomeFirstResponder];
}
-(BOOL)resignFirstResponder
{
    switch (self.gtAnimationType) {
        case GTAnimationTypeUpDown:{
            self.gtPlaceholderLabel.transform = CGAffineTransformIdentity;
        }
            break;
        case GTAnimationTypeLeftRight:{
           self.gtPlaceholderLabel.transform = CGAffineTransformIdentity;
        }
            break;
        case GTAnimationTypeBlowUp:{
            
        }
            break;
        case GTAnimationTypeEasyInOut:{
            [UIView animateWithDuration:0.5 animations:^{
                self.gtPlaceholderLabel.alpha = 1;
            }];
        }
            break;
        case GTAnimationTypeNone:{
            break;
        }
            break;
        default:
            break;
    }
    
    self.gtPlaceholderLabel.textColor = self.normalColor;
    return [super resignFirstResponder];
}

代码地址: https://github.com/WWLJ/AnimationTextField.git
参考来源: //www.greatytc.com/p/8a73e4e2664e

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容