BBCyclingLabel 1.0.1

BBCyclingLabel 1.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 自定义
发布最后发布2015年5月

StevenBruno de Carvalho 维护。



  • Bruno de Carvalho

BBCyclingLabel就像一个UILabel,但允许您在更改文本时执行自定义动画。

您可以直接使用这个组件并配置它进行交叉淡入淡出;然后它会为您执行交叉淡入淡出动画。

下面是一个演示视频,演示了此组件在BBCyclingLabelDemo演示项目上的运行。

快速开始

BBCyclingLabel可在CocoaPods上找到。将以下内容添加到您的Podfile中:

pod 'BBCyclingLabel', '~> 1.0'

工作原理

在底层,BBCyclingLabel只是简单地动画化两个UILabel之间的过渡,在更改文本时始终循环。

如何使用

CGRect labelFrame = ...;

BBCyclingLabel* label = [[BBCyclingLabel alloc] initWithFrame:labelFrame];
label.transitionEffect = BBCyclingLabelTransitionEffectCrossFade;
label.transitionDuration = 0.3;
// Initial text doesn't need to be animated so we explicitly call a method to bypass it
[label setText:@"Initial text" animated:NO];

// ...

// Using the property 'text', the component will always animate
label.text = @"This will cross fade with initial text"

组件属性

除了包含在UILabel中的所有属性外,BBCyclingLabel还有四个额外属性

BBCyclingLabelTransitionEffect   transitionEffect;
NSTimeInterval                   transitionDuration;
BBCyclingLabelPreTransitionBlock preTransitionBlock;
BBCyclingLabelTransitionBlock    transitionBlock;
  • transitionEffect:枚举值BBCyclingLabelTransitionEffect的一个值,它决定了文本循环时将使用哪种动画效果。
  • transitionDuration:过渡动画的持续时间(以毫秒为单位);
  • preTransitionBlocktransitionBlock:在过渡前后执行的块 - 应只在使用自定义效果时手动设置。

效果

此组件支持以下简单效果

  • 淡入(《BBCyclingLabelTransitionEffectFadeIn》)
  • 淡出(《BBCyclingLabelTransitionEffectFadeOut》)
  • 缩放(《BBCyclingLabelTransitionEffectZoomIn》):增加缩放;
  • 缩放(《BBCyclingLabelTransitionEffectZoomOut》):缩小缩放;
  • 向上滚动(《BBCyclingLabelTransitionEffectScrollUp》)
  • 向下滚动(《BBCyclingLabelTransitionEffectScrollDown》)

它还提供以下复合效果(以上效果的组合)

  • 交叉淡入淡出(淡出 + 淡入,《BBCyclingLabelTransitionEffectCrossFade》)
  • 缩放淡出(淡出 + 淡入 + 缩放,《BBCyclingLabelTransitionEffectScaleFadeOut》)
  • 缩放淡入(淡出 + 淡入 + 缩放,《BBCyclingLabelTransitionEffectScaleFadeIn》)

可以使用逻辑“或”运算符组合预定义效果。如果希望在当前文本淡出同时向上滚动当前和新文本,可以通过组合淡出和向上滚动效果来实现此操作。

BBCyclingLabelTransitionEffect effect = BBCyclingLabelTransitionEffectFadeOut |
                                        BBCyclingLabelTransitionEffectScrollUp;

如果您想进行上下滚动时进行交叉淡入淡出,您可以选择以下两种方法之一

BBCyclingLabelTransitionEffect effect = BBCyclingLabelTransitionEffectFadeOut |
                                        BBCyclingLabelTransitionEffectFadeIn |
                                        BBCyclingLabelTransitionEffectScrollUp;

BBCyclingLabelTransitionEffect effect = BBCyclingLabelTransitionEffectCrossFade |
                                        BBCyclingLabelTransitionEffectScrollUp;

可定制效果

如果预定义的效果不足以满足需求,您可以动手编写自己的自定义过渡效果。要做到这一点,需要将 transitionEffect 属性设置为 BBCyclingLabelTransitionEffectCustom 并分配 preTransitionBlocktransitionBlock 块。

preTransitionBlock 块拥有以下签名

^(UILabel* labelToEnter)

此块在过渡开始之前被调用,因此可以准备好新的标签(即将替换当前文本的标签)进入。这就是您应重置进入标签状态的地点。

transitionBlock 块拥有以下签名

^(UILabel* labelToExit, UILabel* labelToEnter)

此块将用两个标签被调用,一个是即将被替换的标签,另一个是包含新文本的标签。这是您进行过渡的地方。隐藏、旋转、淡入淡出、缩放等,这是您尽情发挥效果的地方;只需确保留下的东西处于可用状态。

以下是一个愚蠢的示例,它对退出标签执行 180° 旋转并缩放到 0.2,然后简单地使进入的标签淡入

// Never forget to set this to custom
_customLabel.transitionEffect = BBCyclingLabelTransitionEffectCustom;
_customLabel.preTransitionBlock = ^(UILabel* labelToEnter) {
    // Reset the label to enter; make sure it's at alpha 0 and has no transforms applied to it
    labelToEnter.transform = CGAffineTransformIdentity;
    labelToEnter.alpha = 0;
};
_customLabel.transitionBlock = ^(UILabel* labelToExit, UILabel* labelToEnter) {
    // Fade the exiting label out...
    labelToExit.alpha = 0;
    // ... and apply a rotation + scale transform
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
    labelToExit.transform = CGAffineTransformScale(transform, 0.2, 0.2);
    // Fade the entering label in
    labelToEnter.alpha = 1;
};