Swift 2.0新手;试图学习它,对动画感兴趣并尝试编写一些代码,一次在屏幕上显示一个消息。
我写了这个,它有效,但是我忍不住可以不用CALayers和/或alpha值做些什么?或者一些有生命的小发明,更快捷的东西;这种感觉&看起来有点笨重,真的排序1977年。
SELECT DISTINCT A1.license
FROM Accident A1
WHERE A1.license IN (SELECT A2.license
FROM Accident A2
WHERE A1.accident-date <> A2.accident-date)
答案 0 :(得分:5)
我认为Swiftworthy是主观的,但这是另一个基于你的代码当前如何工作的实现,它将NSTimer包装在Swift类中。
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
}
}
}