加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 动画UITextField时的奇怪行为

发布时间:2020-12-14 17:56:11 所属栏目:百科 来源:网络整理
导读:我正在尝试为UITextField制作动画,但我遇到了一个烦人且奇怪的问题.我的动画有以下顺序: 在第一个状态下,应用程序具有UITextField,相机UIButton和取消UIButton之后未显示的相机按钮,因为它已被定位在应用程序的限制范围之外.实际上我的应用程序有320的宽度
我正在尝试为UITextField制作动画,但我遇到了一个烦人且奇怪的问题.我的动画有以下顺序:

在第一个状态下,应用程序具有UITextField,相机UIButton和取消UIButton之后未显示的相机按钮,因为它已被定位在应用程序的限制范围之外.实际上我的应用程序有320的宽度和取消按钮来源是(350,7)

在第二状态,当用户触摸UITextField时,相机按钮alpha通道设置为0,取消按钮的原点设置为(247,7).

用户触摸键盘的返回按钮或取消按钮后,最终状态与第一状态相同.

在处理期间,UITextField宽度和x轴上的取消按钮原点是动画的.

我的代码是:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
  barcodeSearchButton.userInteractionEnabled = NO;
  cancelButton.userInteractionEnabled = NO;

   CGFloat cancelButtonXOffset = CGRectGetMaxX(barcodeSearchButton.frame) - cancelButton.frame.size.width;
   CGFloat searchFieldWidth = searchField.frame.size.width - cancelButton.frame.size.width + barcodeSearchButton.frame.size.width;
   [UIView animateWithDuration:0.3
         animations:^{
             searchField.frame = CGRectMake(searchField.frame.origin.x,searchField.frame.origin.y,searchFieldWidth,searchField.frame.size.height);
             cancelButton.alpha = 1.0;                
             cancelButton.frame = CGRectMake(cancelButtonXOffset,cancelButton.frame.origin.y,cancelButton.frame.size.width,cancelButton.frame.size.height);

             barcodeSearchButton.alpha = 0.0;

         }
         completion:^(BOOL finished) {
             barcodeSearchButton.userInteractionEnabled = NO;
             cancelButton.userInteractionEnabled = YES;
         }];    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  barcodeSearchButton.userInteractionEnabled = NO;
  cancelButton.userInteractionEnabled = NO;

  [UIView animateWithDuration:0.3
                 animations:^{
                     searchField.frame = CGRectMake(searchField.frame.origin.x,searchFieldStartWidth,searchField.frame.size.height);
                     cancelButton.alpha = 0.0;
                     cancelButton.frame = CGRectMake(cancelButtonStartX,cancelButton.frame.size.height);
                     barcodeSearchButton.alpha = 1.0;                
                 }
                 completion:^(BOOL finished) {
                     barcodeSearchButton.userInteractionEnabled = YES;
                     cancelButton.userInteractionEnabled = NO;
                 }];        
}

当动画第一次执行到最终状态时,这意味着,用户触摸了UITextField,WROTE DOWN(这部分很重要)场上的一些文本,然后用户触摸键盘返回按钮就会出现问题.问题是,当UITextField的宽度也被动画化时,UITextField中的键入文本也会被动画化.

动画表现得像波纹管:

> UITextField延伸回其初始值.
>取消按钮返回其初始原点
>相机按钮设置为1.0
>键入的文本从UITextField的左上角飞过,它进入UITextfield到它不应该离开的位置.

问题是第4步不应该发生,问题是它只发生一次.如果再次启动该过程(触摸uitextfield,键入一些文本,触摸返回键),则步骤4不会发生.

我花了一天时间试图解决这个问题,但我没有成功.

解决方法

感谢大家的答案,但我前段时间找到了解决方案.根据键盘状态,在UITextField中启动动画的正确方法是覆盖textFieldShouldBeginEditing和textFieldShouldEndEditing方法.

我的第一次尝试是覆盖textFieldDidBeginEditing和textFieldDidEndEditing来创建动画.我不知道动画在textFieldShouldBeginEditing和textFieldShouldEndEditing中工作的原因,但在textFieldDidBeginEditing和textFieldDidEndEditing中没有.但它只是有效.

我修改后的代码版本:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    CGFloat cancelButtonXOffset = CGRectGetMaxX(barcodeSearchButton.frame) -    cancelButton.frame.size.width;
    CGFloat searchFieldWidth = searchField.frame.size.width - cancelButton.frame.size.width + barcodeSearchButton.frame.size.width;
    [UIView animateWithDuration:0.3
         animations:^{
             searchField.frame = CGRectMake(searchField.frame.origin.x,cancelButton.frame.size.height);

             barcodeSearchButton.alpha = 0.0;

         } completion:^(BOOL finished) {
             searchField.clearButtonMode = UITextFieldViewModeAlways;
         }]; 
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [UIView animateWithDuration:0.3
         animations:^{
             searchField.frame = CGRectMake(searchField.frame.origin.x,searchField.frame.size.height);
             cancelButton.alpha = 0.0;
             cancelButton.frame = CGRectMake(cancelButtonStartX,cancelButton.frame.size.height);
             barcodeSearchButton.alpha = 1.0;                
         } completion:^(BOOL finished) {
             searchField.clearButtonMode = UITextFieldViewModeNever;
         }];
   return YES;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读