diff --git a/CountdownLabel/CountdownLabel.swift b/CountdownLabel/CountdownLabel.swift index 46cbe96..a21076e 100644 --- a/CountdownLabel/CountdownLabel.swift +++ b/CountdownLabel/CountdownLabel.swift @@ -9,41 +9,41 @@ import UIKit @objc public protocol CountdownLabelDelegate { - optional func countdownStarted() - optional func countdownPaused() - optional func countdownFinished() - optional func countdownCancelled() - optional func countingAt(timeCounted timeCounted: NSTimeInterval, timeRemaining: NSTimeInterval) -} + @objc optional func countdownStarted() + @objc optional func countdownPaused() + @objc optional func countdownFinished() + @objc optional func countdownCancelled() + @objc optional func countingAt(timeCounted timeCounted: TimeInterval, timeRemaining: TimeInterval) -public extension NSTimeInterval { +} +extension TimeInterval { var int: Int { return Int(self) } } -public class CountdownLabel: LTMorphingLabel { +class CountdownLabel: LTMorphingLabel { public typealias CountdownCompletion = () -> ()? public typealias CountdownExecution = () -> () - private let defaultFireInterval = 1.0 - private let date1970 = NSDate(timeIntervalSince1970: 0) + internal let defaultFireInterval = 1.0 + internal let date1970 = NSDate(timeIntervalSince1970: 0) // conputed property - public var dateFormatter: NSDateFormatter { - let df = NSDateFormatter() - df.locale = NSLocale.currentLocale() - df.timeZone = NSTimeZone(name: "GMT") + public var dateFormatter: DateFormatter { + let df = DateFormatter() + df.locale = NSLocale.current + df.timeZone = NSTimeZone(name: "GMT") as TimeZone! df.dateFormat = timeFormat return df } - public var timeCounted: NSTimeInterval { - let timeCounted = NSDate().timeIntervalSinceDate(fromDate) + public var timeCounted: TimeInterval { + let timeCounted = NSDate().timeIntervalSince(fromDate as Date) return round(timeCounted < 0 ? 0 : timeCounted) } - public var timeRemaining: NSTimeInterval { + public var timeRemaining: TimeInterval { return round(currentTime) - timeCounted } @@ -73,28 +73,28 @@ public class CountdownLabel: LTMorphingLabel { } } public var timeFormat = "HH:mm:ss" - public var thens = [NSTimeInterval: CountdownExecution]() + public var thens = [TimeInterval: CountdownExecution]() public var countdownAttributedText: CountdownAttributedText! { didSet { - range = (countdownAttributedText.text as NSString).rangeOfString(countdownAttributedText.replacement) + range = (countdownAttributedText.text as NSString).range(of: countdownAttributedText.replacement) } } - private var completion: CountdownCompletion? - private var fromDate: NSDate = NSDate() - private var currentDate: NSDate = NSDate() - private var currentTime: NSTimeInterval = 0 - private var diffDate: NSDate! - private var targetTime: NSTimeInterval = 0 - private var pausedDate: NSDate! - private var range: NSRange! - private var timer: NSTimer! + internal var completion: CountdownCompletion? + internal var fromDate: NSDate = NSDate() + internal var currentDate: NSDate = NSDate() + internal var currentTime: TimeInterval = 0 + internal var diffDate: NSDate! + internal var targetTime: TimeInterval = 0 + internal var pausedDate: NSDate! + internal var range: NSRange! + internal var timer: Timer! - private var counting: Bool = false - private var endOfTimer: Bool { + internal var counting: Bool = false + internal var endOfTimer: Bool { return timeCounted >= currentTime } - private var finished: Bool = false { + internal var finished: Bool = false { didSet { if finished { paused = false @@ -102,7 +102,7 @@ public class CountdownLabel: LTMorphingLabel { } } } - private var paused: Bool = false + internal var paused: Bool = false // MARK: - Initialize public required init?(coder aDecoder: NSCoder) { @@ -115,19 +115,19 @@ public class CountdownLabel: LTMorphingLabel { setup() } - public convenience init(frame: CGRect, minutes: NSTimeInterval) { + public convenience init(frame: CGRect, minutes: TimeInterval) { self.init(frame: frame) - setCountDownTime(minutes) + setCountDownTime(minutes: minutes) } public convenience init(frame: CGRect, date: NSDate) { self.init(frame: frame) - setCountDownDate(date) + setCountDownDate(targetDate: date) } public convenience init(frame: CGRect, fromDate: NSDate, targetDate: NSDate) { self.init(frame: frame) - setCountDownDate(fromDate, targetDate: targetDate) + setCountDownDate(fromDate: fromDate, targetDate: targetDate) } deinit { @@ -135,30 +135,30 @@ public class CountdownLabel: LTMorphingLabel { } // MARK: - Setter Methods - public func setCountDownTime(minutes: NSTimeInterval) { - setCountDownTime(NSDate(), minutes: minutes) + public func setCountDownTime(minutes: TimeInterval) { + setCountDownTime(fromDate: NSDate(), minutes: minutes) } - public func setCountDownTime(fromDate: NSDate, minutes: NSTimeInterval) { + public func setCountDownTime(fromDate: NSDate, minutes: TimeInterval) { self.fromDate = fromDate targetTime = minutes currentTime = minutes - diffDate = date1970.dateByAddingTimeInterval(minutes) + diffDate = date1970.addingTimeInterval(minutes) updateLabel() } public func setCountDownDate(targetDate: NSDate) { - setCountDownDate(NSDate(), targetDate: targetDate) + setCountDownDate(fromDate: NSDate(), targetDate: targetDate) } public func setCountDownDate(fromDate: NSDate, targetDate: NSDate) { self.fromDate = fromDate - targetTime = targetDate.timeIntervalSinceDate(fromDate) - currentTime = targetDate.timeIntervalSinceDate(fromDate) - diffDate = date1970.dateByAddingTimeInterval(targetTime) + targetTime = targetDate.timeIntervalSince(fromDate as Date) + currentTime = targetDate.timeIntervalSince(fromDate as Date) + diffDate = date1970.addingTimeInterval(targetTime) updateLabel() } @@ -181,7 +181,7 @@ public class CountdownLabel: LTMorphingLabel { // if end of timer if endOfTimer { - text = dateFormatter.stringFromDate(date1970.dateByAddingTimeInterval(0)) + text = dateFormatter.string(from: date1970.addingTimeInterval(0) as Date) countdownDelegate?.countdownFinished?() dispose() completion?() @@ -190,7 +190,7 @@ public class CountdownLabel: LTMorphingLabel { } // MARK: - Public -public extension CountdownLabel { +extension CountdownLabel { func start(completion: ( () -> () )? = nil) { if !isPaused { // current date should be setted at the time of the counter's starting, or the time will be wrong (just a few seconds) after the first time of pausing. @@ -236,7 +236,7 @@ public extension CountdownLabel { } func cancel(completion: (() -> ())? = nil) { - text = dateFormatter.stringFromDate(date1970.dateByAddingTimeInterval(0)) + text = dateFormatter.string(from: date1970.addingTimeInterval(0) as Date) dispose() // set completion if needed @@ -246,14 +246,14 @@ public extension CountdownLabel { countdownDelegate?.countdownCancelled?() } - func addTime(time: NSTimeInterval) { + func addTime(time: TimeInterval) { currentTime = time + currentTime - diffDate = date1970.dateByAddingTimeInterval(currentTime) + diffDate = date1970.addingTimeInterval(currentTime) updateLabel() } - func then(targetTime: NSTimeInterval, completion: () -> ()) -> Self { + func then(targetTime: TimeInterval, completion: @escaping () -> ()) -> Self { let t = targetTime - (targetTime - targetTime) guard t > 0 else { return self @@ -265,7 +265,7 @@ public extension CountdownLabel { } // MARK: - private -private extension CountdownLabel { +extension CountdownLabel { func setup() { morphingEnabled = false } @@ -275,13 +275,13 @@ private extension CountdownLabel { // if time is before start let formattedText = timeCounted < 0 - ? dateFormatter.stringFromDate(date1970.dateByAddingTimeInterval(0)) - : dateFormatter.stringFromDate(diffDate.dateByAddingTimeInterval(round(timeCounted * -1))) + ? dateFormatter.string(from: date1970.addingTimeInterval(0) as Date) + : dateFormatter.string(from: diffDate.addingTimeInterval(round(timeCounted * -1)) as Date) if let countdownAttributedText = countdownAttributedText { let attrTextInRange = NSAttributedString(string: formattedText, attributes: countdownAttributedText.attributes) let attributedString = NSMutableAttributedString(string: countdownAttributedText.text) - attributedString.replaceCharactersInRange(range, withAttributedString: attrTextInRange) + attributedString.replaceCharacters(in: range, with: attrTextInRange) attributedText = attributedString text = attributedString.string @@ -296,8 +296,8 @@ private extension CountdownLabel { return } // change date - let pastedTime = pausedDate.timeIntervalSinceDate(currentDate) - currentDate = NSDate().dateByAddingTimeInterval(-pastedTime) + let pastedTime = pausedDate.timeIntervalSince(currentDate as Date) + currentDate = NSDate().addingTimeInterval(-pastedTime) fromDate = currentDate // reset pause @@ -309,14 +309,14 @@ private extension CountdownLabel { disposeTimer() // create - timer = NSTimer.scheduledTimerWithTimeInterval(defaultFireInterval, + timer = Timer.scheduledTimer(timeInterval: defaultFireInterval, target: self, selector: #selector(updateLabel), userInfo: nil, repeats: true) // register to NSrunloop - NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) + RunLoop.current.add(timer, forMode: RunLoopMode.commonModes) counting = true } @@ -339,7 +339,7 @@ private extension CountdownLabel { } } -public enum CountdownEffect { +enum CountdownEffect { case Anvil case Burn case Evaporate @@ -363,10 +363,10 @@ public enum CountdownEffect { } } -public class CountdownAttributedText: NSObject { - private let text: String - private let replacement: String - private let attributes: [String: AnyObject]? +class CountdownAttributedText: NSObject { + internal let text: String + internal let replacement: String + internal let attributes: [String: AnyObject]? public init(text: String, replacement: String, attributes: [String: AnyObject]? = nil) { self.text = text diff --git a/CountdownLabel/LTMorphingLabel/LTEmitterView.swift b/CountdownLabel/LTMorphingLabel/LTEmitterView.swift index 595b545..796825b 100644 --- a/CountdownLabel/LTMorphingLabel/LTEmitterView.swift +++ b/CountdownLabel/LTMorphingLabel/LTEmitterView.swift @@ -63,7 +63,7 @@ public struct LTEmitter { self.duration = duration var image: UIImage? defer { - cell.contents = image?.CGImage + cell.contents = image?.cgImage } image = UIImage(named: particleName) @@ -74,23 +74,27 @@ public struct LTEmitter { // Load from Framework image = UIImage( named: particleName, - inBundle: NSBundle(forClass: LTMorphingLabel.self), - compatibleWithTraitCollection: nil) + in: Bundle(for: LTMorphingLabel.self), + compatibleWith: nil) } public func play() { - if layer.emitterCells?.count > 0 { + if (layer.emitterCells?.count)! > 0 { return } layer.emitterCells = [cell] - let d = dispatch_time( - DISPATCH_TIME_NOW, - Int64(duration * Float(NSEC_PER_SEC)) - ) - dispatch_after(d, dispatch_get_main_queue()) { + DispatchQueue.main.asyncAfter(deadline: .now() + Double(duration)) { self.layer.birthRate = 0.0 } +// let d = dispatch_time( +// dispatch_time_t(DispatchTime.now()), +// Int64(duration * Float(NSEC_PER_SEC)) +// ) +// +// dispatch_after(d, DispatchQueue.main) { +// self.layer.birthRate = 0.0 +// } } public func stop() { @@ -99,7 +103,7 @@ public struct LTEmitter { } } - func update(configureClosure: LTEmitterConfigureClosure? = .None) -> LTEmitter { + func update(configureClosure: LTEmitterConfigureClosure? = .none) -> LTEmitter { configureClosure?(layer, cell) return self } @@ -125,7 +129,7 @@ public class LTEmitterView: UIView { ) -> LTEmitter { var emitter: LTEmitter - if let e = emitterByName(name) { + if let e = emitterByName(name: name) { emitter = e } else { emitter = LTEmitter( @@ -146,14 +150,14 @@ public class LTEmitterView: UIView { if let e = emitters[name] { return e } - return Optional.None + return Optional.none } public func removeAllEmitters() { for (_, emitter) in emitters { emitter.layer.removeFromSuperlayer() } - emitters.removeAll(keepCapacity: false) + emitters.removeAll(keepingCapacity: false) } } diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Anvil.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Anvil.swift index 0214537..923774e 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Anvil.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Anvil.swift @@ -40,7 +40,7 @@ extension LTMorphingLabel { let centerRect = self.newRects[Int(self.newRects.count / 2)] self.emitterView.createEmitter( - "leftSmoke", + name: "leftSmoke", particleName: "Smoke", duration: 0.6 ) { (layer, cell) in @@ -65,7 +65,7 @@ extension LTMorphingLabel { } self.emitterView.createEmitter( - "rightSmoke", + name: "rightSmoke", particleName: "Smoke", duration: 0.6 ) { (layer, cell) in @@ -90,7 +90,7 @@ extension LTMorphingLabel { } self.emitterView.createEmitter( - "leftFragments", + name: "leftFragments", particleName: "Fragment", duration: 0.6 ) { (layer, cell) in @@ -104,7 +104,7 @@ extension LTMorphingLabel { ) cell.scale = self.font.pointSize / 90.0 cell.scaleSpeed = self.font.pointSize / 40.0 - cell.color = self.textColor.CGColor + cell.color = self.textColor.cgColor cell.birthRate = 60 cell.velocity = 350 cell.yAcceleration = 0 @@ -116,7 +116,7 @@ extension LTMorphingLabel { } self.emitterView.createEmitter( - "rightFragments", + name: "rightFragments", particleName: "Fragment", duration: 0.6 ) { (layer, cell) in @@ -129,7 +129,7 @@ extension LTMorphingLabel { y: centerRect.origin.y + centerRect.size.height / 1.3) cell.scale = self.font.pointSize / 90.0 cell.scaleSpeed = self.font.pointSize / 40.0 - cell.color = self.textColor.CGColor + cell.color = self.textColor.cgColor cell.birthRate = 60 cell.velocity = 350 cell.yAcceleration = 0 @@ -141,7 +141,7 @@ extension LTMorphingLabel { } self.emitterView.createEmitter( - "fragments", + name: "fragments", particleName: "Fragment", duration: 0.6 ) { (layer, cell) in @@ -154,7 +154,7 @@ extension LTMorphingLabel { y: centerRect.origin.y + centerRect.size.height / 1.3) cell.scale = self.font.pointSize / 90.0 cell.scaleSpeed = self.font.pointSize / 40.0 - cell.color = self.textColor.CGColor + cell.color = self.textColor.cgColor cell.birthRate = 60 cell.velocity = 250 cell.velocityRange = CGFloat(Int(arc4random_uniform(20)) + 30) @@ -195,14 +195,14 @@ extension LTMorphingLabel { var rect = self.newRects[index] if progress < 1.0 { - let easingValue = LTEasing.easeOutBounce(progress, 0.0, 1.0) + let easingValue = LTEasing.easeOutBounce(t: progress, 0.0, 1.0) rect.origin.y = CGFloat(Float(rect.origin.y) * easingValue) } if progress > self.morphingDuration * 0.5 { let end = self.morphingDuration * 0.55 self.emitterView.createEmitter( - "fragments", + name: "fragments", particleName: "Fragment", duration: 0.6 ) {_ in}.update { @@ -212,7 +212,7 @@ extension LTMorphingLabel { } }.play() self.emitterView.createEmitter( - "leftFragments", + name: "leftFragments", particleName: "Fragment", duration: 0.6 ) {_ in}.update { @@ -222,7 +222,7 @@ extension LTMorphingLabel { } }.play() self.emitterView.createEmitter( - "rightFragments", + name: "rightFragments", particleName: "Fragment", duration: 0.6 ) {_ in}.update { @@ -236,7 +236,7 @@ extension LTMorphingLabel { if progress > self.morphingDuration * 0.63 { let end = self.morphingDuration * 0.7 self.emitterView.createEmitter( - "leftSmoke", + name: "leftSmoke", particleName: "Smoke", duration: 0.6 ) {_ in}.update { @@ -246,7 +246,7 @@ extension LTMorphingLabel { } }.play() self.emitterView.createEmitter( - "rightSmoke", + name: "rightSmoke", particleName: "Smoke", duration: 0.6 ) {_ in}.update { diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Burn.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Burn.swift index f08adbd..7bce309 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Burn.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Burn.swift @@ -42,7 +42,7 @@ extension LTMorphingLabel { UIGraphicsBeginImageContextWithOptions( maskedSize, false, - UIScreen.mainScreen().scale + UIScreen.main.scale ) let rect = CGRect( x: 0, @@ -50,7 +50,7 @@ extension LTMorphingLabel { width: charLimbo.rect.size.width, height: maskedHeight ) - String(charLimbo.char).drawInRect(rect, withAttributes: [ + String(charLimbo.char).draw(in: rect, withAttributes: [ NSFontAttributeName: self.font, NSForegroundColorAttributeName: self.textColor ]) @@ -106,7 +106,7 @@ extension LTMorphingLabel { ) self.emitterView.createEmitter( - "c\(index)", + name: "c\(index)", particleName: "Fire", duration: self.morphingDuration ) { (layer, cell) in @@ -135,7 +135,7 @@ extension LTMorphingLabel { }.play() self.emitterView.createEmitter( - "s\(index)", + name: "s\(index)", particleName: "Smoke", duration: self.morphingDuration ) { (layer, cell) in @@ -181,10 +181,10 @@ extension LTMorphingLabel { if charLimbo.drawingProgress > 0.0 { let (charImage, rect) = self.burningImageForCharLimbo( - charLimbo, + charLimbo: charLimbo, withProgress: charLimbo.drawingProgress ) - charImage.drawInRect(rect) + charImage.draw(in: rect) return true } diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Evaporate.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Evaporate.swift index 6691d17..55e1434 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Evaporate.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Evaporate.swift @@ -42,7 +42,8 @@ extension LTMorphingLabel { effectClosures["Evaporate\(LTMorphingPhases.Disappear)"] = { char, index, progress in - let newProgress = LTEasing.easeOutQuint(progress, 0.0, 1.0, 1.0) + + let newProgress = LTEasing.easeOutQuint(t: progress, 0.0, 1.0, 1.0) let yOffset: CGFloat = -0.8 * CGFloat(self.font.pointSize) * CGFloat(newProgress) let currentRect = self.previousRects[index].offsetBy(dx: 0, dy: yOffset) let currentAlpha = CGFloat(1.0 - newProgress) @@ -58,7 +59,7 @@ extension LTMorphingLabel { effectClosures["Evaporate\(LTMorphingPhases.Appear)"] = { char, index, progress in - let newProgress = 1.0 - LTEasing.easeOutQuint(progress, 0.0, 1.0) + let newProgress = 1.0 - LTEasing.easeOutQuint(t: progress, 0.0, 1.0) let yOffset = CGFloat(self.font.pointSize) * CGFloat(newProgress) * 1.2 return LTCharacterLimbo( diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Fall.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Fall.swift index 3a5a6ff..fa64f50 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Fall.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Fall.swift @@ -68,7 +68,7 @@ extension LTMorphingLabel { char, index, progress in let currentFontSize = CGFloat( - LTEasing.easeOutQuint(progress, 0.0, Float(self.font.pointSize)) + LTEasing.easeOutQuint(t: progress, 0.0, Float(self.font.pointSize)) ) let yOffset = CGFloat(self.font.pointSize - currentFontSize) @@ -88,7 +88,7 @@ extension LTMorphingLabel { if limbo.drawingProgress > 0.0 { let context = UIGraphicsGetCurrentContext() var charRect = limbo.rect - CGContextSaveGState(context!) + context!.saveGState() let charCenterX = charRect.origin.x + (charRect.size.width / 2.0) var charBottomY = charRect.origin.y + charRect.size.height - self.font.pointSize / 6 var charColor = self.textColor @@ -96,11 +96,11 @@ extension LTMorphingLabel { // Fall down if drawingProgress is more than 50% if limbo.drawingProgress > 0.5 { let ease = CGFloat( - LTEasing.easeInQuint( + LTEasing.easeInQuint(t: Float(limbo.drawingProgress - 0.4), 0.0, - 1.0, - 0.5 + 0.5, + 1.0 ) ) charBottomY = charBottomY + ease * 10.0 @@ -111,7 +111,7 @@ extension LTMorphingLabel { limbo.drawingProgress * -2.0 + 2.0 + 0.01 ) ) - charColor = self.textColor.colorWithAlphaComponent(fadeOutAlpha) + charColor = self.textColor.withAlphaComponent(fadeOutAlpha) } charRect = CGRect( @@ -119,11 +119,11 @@ extension LTMorphingLabel { y: charRect.size.height * -1.0 + self.font.pointSize / 6, width: charRect.size.width, height: charRect.size.height) - CGContextTranslateCTM(context!, charCenterX, charBottomY) + context!.translateBy(x: charCenterX, y: charBottomY) let angle = Float(sin(Double(limbo.rect.origin.x)) > 0.5 ? 168 : -168) let rotation = CGFloat( - LTEasing.easeOutBack( + LTEasing.easeOutBack(t: min( 1.0, Float(limbo.drawingProgress) @@ -132,13 +132,13 @@ extension LTMorphingLabel { 1.0 ) * angle ) - CGContextRotateCTM(context!, rotation * CGFloat(M_PI) / 180.0) + context!.rotate(by: rotation * CGFloat(M_PI) / 180.0) let s = String(limbo.char) - s.drawInRect(charRect, withAttributes: [ - NSFontAttributeName: self.font.fontWithSize(limbo.size), + s.draw(in: charRect, withAttributes: [ + NSFontAttributeName: self.font.withSize(limbo.size), NSForegroundColorAttributeName: charColor ]) - CGContextRestoreGState(context!) + context!.restoreGState() return true } diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Pixelate.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Pixelate.swift index a6da56e..2366495 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Pixelate.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Pixelate.swift @@ -61,11 +61,11 @@ extension LTMorphingLabel { if limbo.drawingProgress > 0.0 { let charImage = self.pixelateImageForCharLimbo( - limbo, + charLimbo: limbo, withBlurRadius: limbo.drawingProgress * 6.0 ) - charImage.drawInRect(limbo.rect) + charImage.draw(in: limbo.rect) return true } @@ -78,7 +78,7 @@ extension LTMorphingLabel { charLimbo: LTCharacterLimbo, withBlurRadius blurRadius: CGFloat ) -> UIImage { - let scale = min(UIScreen.mainScreen().scale, 1.0 / blurRadius) + let scale = min(UIScreen.main.scale, 1.0 / blurRadius) UIGraphicsBeginImageContextWithOptions(charLimbo.rect.size, false, scale) let fadeOutAlpha = min(1.0, max(0.0, charLimbo.drawingProgress * -2.0 + 2.0 + 0.01)) let rect = CGRect( @@ -87,11 +87,11 @@ extension LTMorphingLabel { width: charLimbo.rect.size.width, height: charLimbo.rect.size.height ) - String(charLimbo.char).drawInRect(rect, withAttributes: [ + String(charLimbo.char).draw(in: rect, withAttributes: [ NSFontAttributeName: self.font, NSForegroundColorAttributeName: - self.textColor.colorWithAlphaComponent(fadeOutAlpha) + self.textColor.withAlphaComponent(fadeOutAlpha) ]) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Sparkle.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Sparkle.swift index 795ed21..9eabd36 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Sparkle.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel+Sparkle.swift @@ -42,7 +42,7 @@ extension LTMorphingLabel { UIGraphicsBeginImageContextWithOptions( maskedSize, false, - UIScreen.mainScreen().scale + UIScreen.main.scale ) let rect = CGRect( x: 0, @@ -50,7 +50,7 @@ extension LTMorphingLabel { width: charLimbo.rect.size.width, height: maskedHeight ) - String(charLimbo.char).drawInRect(rect, withAttributes: [ + String(charLimbo.char).draw(in: rect, withAttributes: [ NSFontAttributeName: self.font, NSForegroundColorAttributeName: self.textColor ]) @@ -111,7 +111,7 @@ extension LTMorphingLabel { ) self.emitterView.createEmitter( - "c\(index)", + name: "c\(index)", particleName: "Sparkle", duration: self.morphingDuration ) { (layer, cell) in @@ -123,7 +123,7 @@ extension LTMorphingLabel { cell.emissionLongitude = CGFloat(M_PI / 2.0) cell.scale = self.font.pointSize / 300.0 cell.scaleSpeed = self.font.pointSize / 300.0 * -1.5 - cell.color = self.textColor.CGColor + cell.color = self.textColor.cgColor cell.birthRate = Float(self.font.pointSize) * Float(arc4random_uniform(7) + 3) @@ -148,10 +148,10 @@ extension LTMorphingLabel { if charLimbo.drawingProgress > 0.0 { let (charImage, rect) = self.maskedImageForCharLimbo( - charLimbo, + charLimbo: charLimbo, withProgress: charLimbo.drawingProgress ) - charImage.drawInRect(rect) + charImage.draw(in: rect) return true } diff --git a/CountdownLabel/LTMorphingLabel/LTMorphingLabel.swift b/CountdownLabel/LTMorphingLabel/LTMorphingLabel.swift index 0cf4c2b..06d5089 100644 --- a/CountdownLabel/LTMorphingLabel/LTMorphingLabel.swift +++ b/CountdownLabel/LTMorphingLabel/LTMorphingLabel.swift @@ -39,22 +39,22 @@ typealias LTMorphingStartClosure = (Void) -> Void typealias LTMorphingEffectClosure = - (Character, index: Int, progress: Float) -> LTCharacterLimbo + (Character, _ index: Int, _ progress: Float) -> LTCharacterLimbo typealias LTMorphingDrawingClosure = - LTCharacterLimbo -> Bool + (LTCharacterLimbo) -> Bool typealias LTMorphingManipulateProgressClosure = - (index: Int, progress: Float, isNewChar: Bool) -> Float + (_ index: Int, _ progress: Float, _ isNewChar: Bool) -> Float typealias LTMorphingSkipFramesClosure = (Void) -> Int @objc public protocol LTMorphingLabelDelegate { - optional func morphingDidStart(label: LTMorphingLabel) - optional func morphingDidComplete(label: LTMorphingLabel) - optional func morphingOnProgress(label: LTMorphingLabel, progress: Float) + @objc optional func morphingDidStart(label: LTMorphingLabel) + @objc optional func morphingDidComplete(label: LTMorphingLabel) + @objc optional func morphingOnProgress(label: LTMorphingLabel, progress: Float) } @@ -111,7 +111,7 @@ typealias LTMorphingSkipFramesClosure = guard text != newValue else { return } previousText = text ?? "" - diffResults = previousText.diffWith(newValue) + diffResults = previousText.diffWith(anotherString: newValue) super.text = newValue ?? "" morphingProgress = 0.0 @@ -128,21 +128,21 @@ typealias LTMorphingSkipFramesClosure = morphingDuration = 0.01 morphingProgress = 0.5 } else if previousText != text { - displayLink.paused = false + displayLink.isPaused = false let closureKey = "\(morphingEffect.description)\(LTMorphingPhases.Start)" if let closure = startClosures[closureKey] { return closure() } - delegate?.morphingDidStart?(self) + delegate?.morphingDidStart?(label: self) } } } public override func setNeedsLayout() { super.setNeedsLayout() - previousRects = rectsOfEachCharacter(previousText, withFont: font) - newRects = rectsOfEachCharacter(text ?? "", withFont: font) + previousRects = rectsOfEachCharacter(textToDraw: previousText, withFont: font) + newRects = rectsOfEachCharacter(textToDraw: text ?? "", withFont: font) } override public var bounds: CGRect { @@ -165,13 +165,13 @@ typealias LTMorphingSkipFramesClosure = } } - private lazy var displayLink: CADisplayLink = { + internal lazy var displayLink: CADisplayLink = { let displayLink = CADisplayLink( target: self, selector: #selector(LTMorphingLabel.displayFrameTick)) - displayLink.addToRunLoop( - NSRunLoop.currentRunLoop(), - forMode: NSRunLoopCommonModes) + displayLink.add( + to: RunLoop.current, + forMode: RunLoopMode.commonModes) return displayLink }() @@ -211,12 +211,12 @@ extension LTMorphingLabel { } if let onProgress = delegate?.morphingOnProgress { - onProgress(self, progress: morphingProgress) + onProgress(self, morphingProgress) } } else { - displayLink.paused = true + displayLink.isPaused = true - delegate?.morphingDidComplete?(self) + delegate?.morphingDidComplete?(label: self) } } @@ -226,12 +226,12 @@ extension LTMorphingLabel { var charRects = [CGRect]() var leftOffset: CGFloat = 0.0 - charHeight = "Leg".sizeWithAttributes([NSFontAttributeName: font]).height + charHeight = "Leg".size(attributes: [NSFontAttributeName: font]).height let topOffset = (bounds.size.height - charHeight) / 2.0 - for (_, char) in textToDraw.characters.enumerate() { - let charSize = String(char).sizeWithAttributes([NSFontAttributeName: font]) + for (_, char) in textToDraw.characters.enumerated() { + let charSize = String(char).size(attributes: [NSFontAttributeName: font]) charRects.append( CGRect( origin: CGPoint( @@ -249,9 +249,9 @@ extension LTMorphingLabel { var stringLeftOffSet: CGFloat = 0.0 switch textAlignment { - case .Center: + case .center: stringLeftOffSet = CGFloat((Float(bounds.size.width) - totalWidth) / 2.0) - case .Right: + case .right: stringLeftOffSet = CGFloat(Float(bounds.size.width) - totalWidth) default: () @@ -283,17 +283,17 @@ extension LTMorphingLabel { case .Same: newX = Float(newRects[index].origin.x) currentRect.origin.x = CGFloat( - LTEasing.easeOutQuint(progress, oriX, newX - oriX) + LTEasing.easeOutQuint(t: progress, oriX, newX - oriX) ) case .Move(let offset): newX = Float(newRects[index + offset].origin.x) currentRect.origin.x = CGFloat( - LTEasing.easeOutQuint(progress, oriX, newX - oriX) + LTEasing.easeOutQuint(t: progress, oriX, newX - oriX) ) case .MoveAndAdd(let offset): newX = Float(newRects[index + offset].origin.x) currentRect.origin.x = CGFloat( - LTEasing.easeOutQuint(progress, oriX, newX - oriX) + LTEasing.easeOutQuint(t: progress, oriX, newX - oriX) ) default: // Otherwise, remove it @@ -302,11 +302,11 @@ extension LTMorphingLabel { if let closure = effectClosures[ "\(morphingEffect.description)\(LTMorphingPhases.Disappear)" ] { - return closure(char, index: index, progress: progress) + return closure(char, index, progress) } else { // And scale it by default let fontEase = CGFloat( - LTEasing.easeOutQuint( + LTEasing.easeOutQuint(t: progress, 0, Float(font.pointSize) ) ) @@ -336,16 +336,16 @@ extension LTMorphingLabel { let currentRect = newRects[index] var currentFontSize = CGFloat( - LTEasing.easeOutQuint(progress, 0, Float(font.pointSize)) + LTEasing.easeOutQuint(t: progress, 0, Float(font.pointSize)) ) if let closure = effectClosures[ "\(morphingEffect.description)\(LTMorphingPhases.Appear)" ] { - return closure(char, index: index, progress: progress) + return closure(char, index, progress) } else { currentFontSize = CGFloat( - LTEasing.easeOutQuint(progress, 0.0, Float(font.pointSize)) + LTEasing.easeOutQuint(t: progress, 0.0, Float(font.pointSize)) ) // For emojis currentFontSize = max(0.0001, currentFontSize) @@ -366,24 +366,24 @@ extension LTMorphingLabel { var limbo = [LTCharacterLimbo]() // Iterate original characters - for (i, character) in previousText.characters.enumerate() { + for (i, character) in previousText.characters.enumerated() { var progress: Float = 0.0 if let closure = progressClosures[ "\(morphingEffect.description)\(LTMorphingPhases.Progress)" ] { - progress = closure(index: i, progress: morphingProgress, isNewChar: false) + progress = closure(i, morphingProgress, false) } else { progress = min(1.0, max(0.0, morphingProgress + morphingCharacterDelay * Float(i))) } - let limboOfCharacter = limboOfOriginalCharacter(character, index: i, progress: progress) + let limboOfCharacter = limboOfOriginalCharacter(char: character, index: i, progress: progress) limbo.append(limboOfCharacter) } // Add new characters - for (i, character) in (text!).characters.enumerate() { - if i >= diffResults?.0.count { + for (i, character) in (text!).characters.enumerated() { + if i >= (diffResults?.0.count)! { break } @@ -392,7 +392,7 @@ extension LTMorphingLabel { if let closure = progressClosures[ "\(morphingEffect.description)\(LTMorphingPhases.Progress)" ] { - progress = closure(index: i, progress: morphingProgress, isNewChar: true) + progress = closure(i, morphingProgress, true) } else { progress = min(1.0, max(0.0, morphingProgress - morphingCharacterDelay * Float(i))) } @@ -406,7 +406,7 @@ extension LTMorphingLabel { switch diffResult { case .MoveAndAdd, .Replace, .Add, .Delete: let limboOfCharacter = limboOfNewCharacter( - character, + char: character, index: i, progress: progress ) @@ -430,19 +430,18 @@ extension LTMorphingLabel { if let s = text { text = s } - // Load all morphing effects for effectName: String in LTMorphingEffect.allValues { let effectFunc = Selector("\(effectName)Load") - if respondsToSelector(effectFunc) { - performSelector(effectFunc) + if LTMorphingLabel.responds(to: effectFunc) { + LTMorphingLabel.performSelector(onMainThread: effectFunc, with: nil, waitUntilDone: true) } } } - override public func drawTextInRect(rect: CGRect) { + override public func drawText(in rect: CGRect) { if !morphingEnabled || limboOfCharacters().count == 0 { - super.drawTextInRect(rect) + super.drawText(in: rect) return } @@ -460,11 +459,11 @@ extension LTMorphingLabel { if !willAvoidDefaultDrawing { let s = String(charLimbo.char) - s.drawInRect(charRect, withAttributes: [ + s.draw(in: charRect, withAttributes: [ NSFontAttributeName: UIFont.init(name: font.fontName, size: charLimbo.size)!, NSForegroundColorAttributeName: - textColor.colorWithAlphaComponent(charLimbo.alpha) + textColor.withAlphaComponent(charLimbo.alpha) ]) } } diff --git a/CountdownLabel/LTMorphingLabel/LTStringDiffResult.swift b/CountdownLabel/LTMorphingLabel/LTStringDiffResult.swift index e482946..f4ff7b8 100644 --- a/CountdownLabel/LTMorphingLabel/LTStringDiffResult.swift +++ b/CountdownLabel/LTMorphingLabel/LTStringDiffResult.swift @@ -37,20 +37,20 @@ public extension String { guard let anotherString = anotherString else { let diffResults: [LTCharacterDiffResult] = - Array(count: characters.count, repeatedValue: .Delete) - let skipDrawingResults: [Bool] = Array(count: characters.count, repeatedValue: false) + Array(repeating: .Delete, count: characters.count) + let skipDrawingResults: [Bool] = Array(repeating: false, count: characters.count) return (diffResults, skipDrawingResults) } - let newChars = anotherString.characters.enumerate() + let newChars = anotherString.characters.enumerated() let lhsLength = characters.count let rhsLength = anotherString.characters.count var skipIndexes = [Int]() let leftChars = Array(characters) let maxLength = max(lhsLength, rhsLength) - var diffResults: [LTCharacterDiffResult] = Array(count: maxLength, repeatedValue: .Add) - var skipDrawingResults: [Bool] = Array(count: maxLength, repeatedValue: false) + var diffResults: [LTCharacterDiffResult] = Array(repeating: .Add, count: maxLength) + var skipDrawingResults: [Bool] = Array(repeating: false, count: maxLength) for i in 0..