Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

ios - Displaying text one character at a time in swift 2.0

New to Swift 2.0; trying to learn it, interested in animation and tried to write some code to display a message on screen one character at a time.

I wrote this, it works, but I cannot help but could I not do something with CALayers perhaps and/or alpha values? Or some animate gizmo, something more swift worthy; this feels & looks kinda clunky, sort 1977 really.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


let supertext  = "A long time ago, in a galaxy far, far away ..."
let round = supertext.characters.count
for i in 0...round {
    let delay = Double(i)/10
    let subtext = supertext[supertext.startIndex..<supertext.startIndex.advancedBy(i)]
    NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: "ghostText:", userInfo: ["theText" :subtext], repeats: false)
    }
}

var view3:UITextView = UITextView()

func ghostText(timer:NSTimer) {
    view3.removeFromSuperview()
    let userInfo = timer.userInfo as! Dictionary<String, AnyObject>
    let tempText:NSString = (userInfo["theText"] as! NSString)
    print(tempText)

    let font = UIFont(name: "Georgia", size: 18.0) ?? UIFont.systemFontOfSize(18.0)
    let textFont = [NSFontAttributeName:font]

    let subChararacter = NSAttributedString(string: String(tempText), attributes: textFont)
    view3.frame = CGRect(x: 100, y: 120, width: CGRectGetWidth(self.view.frame), height: CGRectGetWidth(self.view.frame)-20)
    view3.attributedText = subChararacter
    self.view.addSubview(view3)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose Swiftworthy is subjective, but here's another implementation based on how your code currently works that wraps the NSTimer in a Swift class.

class Timer {
    typealias TimerFunction = (Int)->Bool
    private var handler: TimerFunction
    private var i = 0

    init(interval: NSTimeInterval, handler: TimerFunction) {
        self.handler = handler
        NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "timerFired:", userInfo: nil, repeats: true)
    }

    @objc
    private func timerFired(timer:NSTimer) {
        if !handler(i++) {
            timer.invalidate()
        }
    }
}

class ViewController: UIViewController {
    let text: NSAttributedString = {
        let font = UIFont(name: "Georgia", size: 18.0) ?? UIFont.systemFontOfSize(18.0)
        return NSAttributedString(string: "A long time ago, in a galaxy far, far away ...", attributes: [NSFontAttributeName: font])
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        let textView = UITextView(frame: CGRect(x: 100, y: 120, width: CGRectGetWidth(self.view.frame), height: CGRectGetWidth(self.view.frame)-20))
        self.view.addSubview(textView)

        let _ = Timer(interval: 0.1) {i -> Bool in
            textView.attributedText = self.text.attributedSubstringFromRange(NSRange(location: 0, length: i+1))
            return i + 1 < self.text.string.characters.count
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...