iOS - Animating text size change in UILabel or UITextView?

余生颓废 提交于 2019-12-03 06:56:02

If I knew when the rotation was about to start, and the duration of the animation...

Funny you should mention that. Just before the animation starts, your view controller will receive a willAnimateRotationToInterfaceOrientation:duration: message that gives you the exact information you need.

One option is to fade the old text out, change the font size, and fade it back in. The font property may not be animatable, but alpha is. Since alpha is a property of UIView, you can treat all your text-bearing views the same way: UILabel, UITextView, etc. It looks good, too.

MiKL

A way to proceed could be to:

  1. Create a CAKeyframeAnimation
  2. Define the scaling and rotation you want to achieve during that animation using a set of CATransform3D objects
  3. Add these transforms to your keyframed animation
  4. Send the addAnimation message to your label layer object: [[label layer] addAnimation];

Here would be a code sample assuming yourLabel is the UILabel you want to scale and rotate:

CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

CATransform3D scaleUp = CATransform3DMakeScale(1.5, 1.5, 1); // Scale in x and y
CATransform3D rotationScaled = CATransform3DRotate (scaleUp, 90, 0, 0, 1); // Rotate the scaled font

[scale setValues:[NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DIdentity],
[NSValue valueWithCATransform3D:rotationScaled],
nil]];

// set the duration
[scale setDuration: 1.0];

// animate your label layer
[[yourLabel layer] addAnimation:scale forKey:@"scaleText"];

This is typically how a text bouncing around would be animated for instance.

You could start this when the device starts rotating and retrieve the animation when it's done rotating so you can update your label with the proper scale/position.

You will need tuning to find the proper timing and rotation.

Change the font size when didAnimateFirstHalfOfRotationToInterfaceOrientation: is called. That way the user won't see the change once the rotation completed. It would be pretty hard to see the font size change then, as the rotation is happening!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!