7

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;
                     }];
}

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: Default view

The expanded view:

Expanded view

The textview just a moment after tapping the button:

Shrinking view

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.

4

2 回答 2

2

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

执行以下操作:

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

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

[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];
于 2013-05-31T19:52:38.773 回答
1

另一种解决方案是对边界变化进行动画处理。

有几种方法,但这里有一个简单的子类,UITextView它正是这样做的。

import Foundation
import UIKit

import QuartzCore

class SmoothTextView : UITextView {

    override func actionForLayer(layer: CALayer!, forKey key: String!) -> CAAction! {

        if key == "bounds" {

            let x = super.actionForLayer(layer, forKey: "backgroundColor")

            if let action:CAAnimation = x as? CAAnimation {

                let transition = CATransition()

                transition.type             = kCATransitionFade
                transition.beginTime        = action.beginTime
                transition.duration         = action.duration
                transition.speed            = action.speed
                transition.timeOffset       = action.timeOffset
                transition.repeatCount      = action.repeatCount
                transition.repeatDuration   = action.repeatDuration
                transition.autoreverses     = action.autoreverses
                transition.fillMode         = action.fillMode

                transition.timingFunction = action.timingFunction
                transition.delegate = action.delegate

                return transition
            }
        }

        return super.actionForLayer(layer, forKey: key)
    }
}

从 iOS 8 开始。

作为额外的调整,您可能希望通过将所有填充或插入归零来调整文本容器来配置文本视图实例的文本:

textView.textContainer.lineFragmentPadding = 0.0
textView.textContainerInset = UIEdgeInsetsZero
于 2015-04-16T20:11:53.383 回答