IOS 编辑框焦点失去与获取时提示文本的的动画

本文介绍了一个扩展的 UIView,用于实现 UITextField 的动画效果。当 UITextField 获取焦点时,UILabel 动画缩放并上移;失去焦点且文本为空时,UILabel 恢复原状。通过 CABasicAnimation 和 CAAnimationGroup 实现移动和缩放动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、本文实现的效果如图
在这里插入图片描述
二、实现思路
扩展一个UIView ,内部使用一个UILabel与UITextFiled,其中UILabel 使用动画控制缩放与位移,在UITextFiled 获取焦点时启动动画,进行UILabel 缩放并往上移,失去焦点且文本内容为空时再使用动画控制UILabel恢复到原来的状态。源码如下:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface AnimTextFiled : UIView

@property(nonatomic,strong) UILabel* hintLabel;
@property(nonatomic,strong) UITextField* editText;
@property(nonatomic,copy) NSString* hintStr;

- (instancetype)initWithFrame:(CGRect)frame hintStr:(NSString*) placeHolderTxt;

-(void) releaseFirstResponder;

@end

NS_ASSUME_NONNULL_END

。源文件

//
//  AnimTextFiled.m
//  ZacharyTextField
//
//  Created by liuxiaobing on 2019/4/25.
//  Copyright © 2019 gaogang. All rights reserved.
//

#import "AnimTextFiled.h"

@interface AnimTextFiled()<UITextFieldDelegate>
{
    
    BOOL isEditing;
    BOOL isOrigin;
}
@end


@implementation AnimTextFiled

- (instancetype)initWithFrame:(CGRect)frame hintStr:(NSString*) placeHolderTxt{
    self = [super initWithFrame:frame];
    if(self){
        self.hintStr = placeHolderTxt;
        [self initView:frame];

    }
    return  self;
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initView:frame];
    }
    return  self;
}

-(void) initView:(CGRect)frame{
    isEditing = NO;
    isOrigin = YES;
    self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 1.0 / 3.0 * frame.size.height, frame.size.width, 2.0 / 3.0 * frame.size.height)];
    self.hintLabel.text = self.hintStr;
    self.hintLabel.textColor = [UIColor grayColor];
    //self.hintLabel.backgroundColor = [UIColor blueColor];
    [self addSubview:self.hintLabel];
    
    self.editText = [[UITextField alloc] initWithFrame:CGRectMake(0, 1.0 / 3.0 * frame.size.height, frame.size.width, 2.0 / 3.0 * frame.size.height)];
    //self.editText.backgroundColor = [UIColor greenColor];
    self.editText.delegate = self;
    [self addSubview:self.editText];
    
}


-(void) startHintAnim{
    
    if(isEditing){
        return;
    }
    isEditing = YES;
    isOrigin = NO;
    self.hintLabel.center = CGPointMake(0, 1.0 / 3.0 * self.frame.size.height);
    self.hintLabel.layer.anchorPoint = CGPointMake(0, 0);//缩小的中心点
    
    //移动动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];///.y的话就向下移动。
    animation.toValue = [NSNumber numberWithFloat:-(1.0 / 3.0 * self.frame.size.height)];
    
    //缩小动画
    CABasicAnimation *ZoomAnima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    ZoomAnima.fromValue = [NSNumber numberWithFloat:1];
    ZoomAnima.toValue = [NSNumber numberWithFloat:0.8];
    
    CAAnimationGroup *AnimationGroup = [CAAnimationGroup animation];
    AnimationGroup.animations = @[animation,ZoomAnima];
    AnimationGroup.duration = 0.3;
    AnimationGroup.removedOnCompletion = NO;
    AnimationGroup.fillMode = kCAFillModeForwards;
    [self.hintLabel.layer addAnimation:AnimationGroup forKey:@"groupAnimation"];

}

-(void) stopEditingAnimaiton{
    
    if(isOrigin) return;
    isOrigin = YES;
    isEditing = NO;
    self.hintLabel.center = CGPointMake(0, 0);
    self.hintLabel.layer.anchorPoint = CGPointMake(0, 0);//缩小的中心点
    //移动动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];///.y的话就向下移动。
    animation.toValue = [NSNumber numberWithFloat:1.0 / 3.0 * self.frame.size.height];
    //缩小动画
    CABasicAnimation *ZoomAnima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    ZoomAnima.fromValue = [NSNumber numberWithFloat:0.8];
    ZoomAnima.toValue = [NSNumber numberWithFloat:1];
    
    CAAnimationGroup *AnimationGroup = [CAAnimationGroup animation];
    AnimationGroup.animations = @[animation,ZoomAnima];
    AnimationGroup.duration = 0.3;
    AnimationGroup.removedOnCompletion = NO;
    AnimationGroup.fillMode = kCAFillModeForwards;
    [self.hintLabel.layer addAnimation:AnimationGroup forKey:@"groupAnimation"];
}

-(void) releaseFirstResponder{
    [self.editText resignFirstResponder];
    if([self.editText.text isEqualToString:@""]){
        [self stopEditingAnimaiton];
    }
}

#pragma  mark -- UITextFeildDelagete
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self startHintAnim];
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self releaseFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self releaseFirstResponder];
    return  YES;
}

@end

使用:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    CusTextField = [[ZacharyTextField alloc]initWithFrame:CGRectMake(40, 200, 200, 60)];
//    [self.view addSubview:CusTextField];
    
    tt = [[AnimTextFiled alloc]initWithFrame:CGRectMake(40, 200, 200, 60) hintStr:@"请输入手机号"];
    tt.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:tt];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [super touchesBegan:touches withEvent:event];
    [tt releaseFirstResponder];
//    [CusTextField ZacharyResignFirstResponder];
//    [_CusTextField1 ZacharyResignFirstResponder];
    
}

源码:https://github.com/06110902002/AnimTextFiled_CircleChart.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值