如何使用动态更改和循环文本制作UILabel?

时间:2016-06-15 06:25:55

标签: ios swift animation uiview uilabel

我想制作一个标签,其中的文字会从" Loading" - > "安装&#34。 - >"中.." - > "加载中"动态地,只要该标签存在。

所以我刚创建了一个标签,并添加了一个我称之为animateDots的函数,它只是一直在调用自己。我使用UIView.animateWithDuration()完成了它,每个完成块调用另一个animateWithDuration(),直到它只调用animateDots(),依此类推。但这并不奏效,因为UILabel中的文本不具有动画效果,所以它只是快速地激活所有四个标签。我希望它很慢。

我尝试了UIView.preformWithoutAnimation,我也尝试了UIView.beginAnimations,但我无法回忆animateDots()而不会导致应用崩溃。我不知道还有什么可以尝试

编辑:由于@ 7vikram7的建议,我在这里解决了这个问题。

我在初始化标签后立即创建了计时器,并重复计时器。每次计时器完成循环时,它都会运行一个选择器,在本例中为animateDots,它会更改文本。在代码中:

每当您想要开始动画过程时添加它:

loading = UILabel(frame: CGRectMake(0,0,80,50))
loading.text = "Loading"
let timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector:
            #selector(LoadingCell.animateDots), userInfo: nil, repeats: true)
        timer.fire()

然后添加此功能:

func animateDots() {
        switch (loading.text!) {
        case "Loading...":
            loading.text = "Loading"
        case "Loading":
            loading.text = "Loading."
        case "Loading.":
            loading.text = "Loading.."
        case "Loading..":
            loading.text = "Loading..."
        default:
            loading.text = "Loading"
        }
    }

5 个答案:

答案 0 :(得分:2)

您可以使用NSTimer。安排一个间隔为2或3秒的计时器重复。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/

答案 1 :(得分:2)

复制并过去进入项目并更改计时器和秒的值

当您想要动画文本时启动计时器,并在不使用时停止

NSTimer *myticker = [NSTimer scheduledTimerWithTimeInterval:(set your time) target:self selector:@selector(showActivity) userInfo:nil repeats:YES];

-(void)showActivity {

    second=second-0.5; // set your second

    if (second <= 0) {
        [myticker invalidate];

    }


    if (second >= 5) {
        if ([connectingLabel.text isEqualToString:@"Connecting."]) {
            connectingLabel.text = @"Connecting..";

        }else  if ([connectingLabel.text isEqualToString:@"Connecting.."]) {
            connectingLabel.text = @"Connecting...";

        }else if ([connectingLabel.text isEqualToString:@"Connecting..."]) {
            connectingLabel.text = @"Connecting.";
        }else{
            connectingLabel.text = @"Connecting.";

        }
    }else{
        connectingLabel.text = @"";
    }

}

答案 2 :(得分:1)

Swos的Swift 5版本答案

    var myticker : Timer?
func LabelAnimating() {
    myticker  = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(showLoading), userInfo: nil, repeats: true)
}

@objc func showLoading() {

    if findingTripsLbl.text == "Connecting." {
        findingTripsLbl.text = "Connecting.."
    } else if findingTripsLbl.text == "Connecting.." {
        findingTripsLbl.text = "Connecting..."
    } else if findingTripsLbl.text == "Connecting..." {
        findingTripsLbl.text = "Connecting."
    } else {
        findingTripsLbl.text = "Connecting."
    }}

func StopLoading()  {
         myticker?.invalidate()
}

答案 3 :(得分:0)

你也可以创建4个这样的图像,你可以设置<center> <label style="font-size:40px">Please Enter A Number Here:</label> <input type="text" id="num" style="font-size:40px"> <br /> <br /> <br /> <br /> <button type="button" onclick="check()" style="font-size:20px">Check</button> <br /> <br /> <br /> <br /> <button type="button" onclick="abs()" style="font-size:20px">Absolute Value</button> <br /> <br /> <br /> <br /> <button type="button" onclick="round()" style="font-size:20px">Rounded Value</button> <br /> <br /> <br /> <br /> <button type="button" onclick="log()" style="font-size:20px">Log Value</button> <br /> <br /> </center>的动画。它给出了你想要的相同结果。

答案 4 :(得分:0)

MarqueeLabel可以帮助您轻松修改。您可以将加载文本设为不同的标签,并使 DOTS 文本成为MarqueeLabel,或者您完全使所有三个点分别隐藏和取消隐藏它们。后者是一种非常繁琐的方法,并且非常不受推荐,因此首先使用。

pod MarqueeLabel

MarqueeLabel提供此功能。只需将标签的类名设置为MarqueeLabel:

@IBOutlet weak var lblLocation: MarqueeLabel!

根据您的要求设置属性:

  1. 持续时间
  2. FadeLength
  3. MarqueeType
相关问题