美文网首页iOS开发 ios零碎记录
UITextField的占位符抖动效果

UITextField的占位符抖动效果

作者: 武小寺 | 来源:发表于2015-11-04 18:41 被阅读286次
    • 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
    参考来源: http://www.jianshu.com/p/8a73e4e2664e

    相关文章

      网友评论

        本文标题:UITextField的占位符抖动效果

        本文链接:https://www.haomeiwen.com/subject/vuaqhttx.html