UITextView动画更改框架不会动画文本重新分配 [英] UITextView animating change of frame does not animate text reallocation

查看:86
本文介绍了UITextView动画更改框架不会动画文本重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextView,我试图在用户点击一个按钮时动画帧的变化。基本上文本视图变得更大以适应屏幕,因此它可以显示更多文本,然后当用户再次点击按钮时,它缩小到原始帧。

I have a UITextView, and I am trying to animate a change of frame when the user taps on a button. Basically the text view grows larger to fit the screen so it can display more text, and then when the user taps the button again it shrinks to its original frame.

我执行使用块的动画,如下所示:

I perform the animation using blocks, like this:

if(!isDisplayingDetailView)
{
    //Expand view
    [UIView animateWithDuration:0.5
                     animations:^{
                         self.detailView.frame = self.view.frame;
                     }
                     completion:^(BOOL finished){
                         isDisplayingDetailView = YES;
                     }];
}
else{
    //Shrink view.
    [UIView animateWithDuration:0.5
                     animations:^{
                         self.detailView.frame = self.detailFrame;
                         }
                     completion:^(BOOL finished){
                         isDisplayingDetailView = NO;
                     }];
}

其中self.detailView是UITextView,self.detailFrame只是一个CGRect拿着原始的框架。 isDisplayingDetailView只是一个BOOL,用于检查视图是否展开。
我的问题是文本调整大小不是动画。我从测试应用程序中制作了一些屏幕截图来说明我的问题:
应用程序默认视图:

Where self.detailView is the UITextView, and self.detailFrame is just a CGRect holding the original frame. isDisplayingDetailView is just a BOOL to check if the view is expanded or not. My problem is that the text resize is not animating. I made some screenshots from a test app to illustrate my problem: App default view:

展开的视图:

点按按钮后的文本视图:

The textview just a moment after tapping the button:

当你可以看到文本自动缩小到最终的帧大小,没有任何类型的动画,而边界仍然是动画。我想这是正确的行为,但我想知道是否可以为文本设置动画及其视图。

As you can see the text automatically shrinks to the final frame size, without any kind of animation, while the bounds still are animating. I guess that's the right behavior, but I'd like to know if it's possible to animate the text along with its view.

推荐答案

textview中的文本并不总是按预期运行。为此,您必须设置NSTimer并在每个刻度上设置帧大小。

The text in textviews doesn't always act as expected. For this you'll have to set up a NSTimer and set the frame size on every tick.

执行以下操作:

textview.frame = CGRectMake (textview.frame.origin.x, textview.frame.origin.y, textview.frame.size.width-1, textview.frame.size.height-1);

然后当它完成后我会从superview中删除textview并将其设置为nil,然后init一个新的。这样做的原因是,在文本框的周围添加了由于某种原因填充文本视图的框架。你不会注意到它,除非你改变框架至少100次。

Then when it's done I would completely remove the textview from superview and set it to nil, and then init a new one. The reason for this is that when changes the frames of textviews for some reason padding is added around the text's box. You won't notice it unless you change the frame a probably at least 100 times.

[textview removeFromSuperview];
textview = nil;
textview = [[UITextView alloc] initWithFrame:CGRectMake(x, y, width, height)];
textview.text = yourTextString;
//You will also have to set whatever non default properties you want, such as text or background color
[view addSubview:textview];

这篇关于UITextView动画更改框架不会动画文本重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆