Skip to content

Commit

Permalink
Update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
EyreFree committed Dec 11, 2016
1 parent 3dfa2e8 commit 671f994
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 57 deletions.
27 changes: 22 additions & 5 deletions EFCountingLabel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
125E86461DFD2542009BF300 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 125E86441DFD2542009BF300 /* Main.storyboard */; };
125E86481DFD2542009BF300 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 125E86471DFD2542009BF300 /* Assets.xcassets */; };
125E864B1DFD2542009BF300 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 125E86491DFD2542009BF300 /* LaunchScreen.storyboard */; };
125E86531DFD2558009BF300 /* UICountingLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 125E86521DFD2558009BF300 /* UICountingLabel.swift */; };
125E86531DFD2558009BF300 /* EFCountingLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 125E86521DFD2558009BF300 /* EFCountingLabel.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -23,7 +23,7 @@
125E86471DFD2542009BF300 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = EFCountingLabel/Assets.xcassets; sourceTree = "<group>"; };
125E864A1DFD2542009BF300 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
125E864C1DFD2542009BF300 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = EFCountingLabel/Info.plist; sourceTree = "<group>"; };
125E86521DFD2558009BF300 /* UICountingLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UICountingLabel.swift; sourceTree = "<group>"; };
125E86521DFD2558009BF300 /* EFCountingLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EFCountingLabel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -57,22 +57,38 @@
125E863F1DFD2542009BF300 /* EFCountingLabel */ = {
isa = PBXGroup;
children = (
125E86521DFD2558009BF300 /* UICountingLabel.swift */,
125E86521DFD2558009BF300 /* EFCountingLabel.swift */,
);
path = EFCountingLabel;
sourceTree = "<group>";
};
125E86541DFD644F009BF300 /* EFCountingLabelDemo */ = {
isa = PBXGroup;
children = (
125E86551DFD76D5009BF300 /* Res */,
125E86401DFD2542009BF300 /* AppDelegate.swift */,
125E86421DFD2542009BF300 /* ViewController.swift */,
125E86561DFD76F0009BF300 /* Supporting Files */,
);
name = EFCountingLabelDemo;
sourceTree = "<group>";
};
125E86551DFD76D5009BF300 /* Res */ = {
isa = PBXGroup;
children = (
125E86441DFD2542009BF300 /* Main.storyboard */,
125E86471DFD2542009BF300 /* Assets.xcassets */,
125E86491DFD2542009BF300 /* LaunchScreen.storyboard */,
);
name = Res;
sourceTree = "<group>";
};
125E86561DFD76F0009BF300 /* Supporting Files */ = {
isa = PBXGroup;
children = (
125E864C1DFD2542009BF300 /* Info.plist */,
);
name = EFCountingLabelDemo;
name = "Supporting Files";
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -149,7 +165,7 @@
buildActionMask = 2147483647;
files = (
125E86431DFD2542009BF300 /* ViewController.swift in Sources */,
125E86531DFD2558009BF300 /* UICountingLabel.swift in Sources */,
125E86531DFD2558009BF300 /* EFCountingLabel.swift in Sources */,
125E86411DFD2542009BF300 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -316,6 +332,7 @@
125E86511DFD2542009BF300 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
22 changes: 5 additions & 17 deletions EFCountingLabel/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// AppDelegate.swift
// EFCountingLabel
//
// Created by EyreFree on 16/12/11.
// Copyright © 2016年 EyreFree. All rights reserved.
//

import UIKit

Expand All @@ -15,32 +8,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func applicationWillTerminate(_ application: UIApplication) {

}
}

2 changes: 1 addition & 1 deletion EFCountingLabel/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="45" textAlignment="justified" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ec8-b5-HbT" customClass="UICountingLabel" customModule="EFCountingLabel" customModuleProvider="target">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="45" textAlignment="justified" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ec8-b5-HbT" customClass="EFCountingLabel" customModule="EFCountingLabel" customModuleProvider="target">
<rect key="frame" x="175" y="321.5" width="25" height="24"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/>
<nil key="textColor"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
//
// UICountingLabel.swift
// EFCountingLabel
// EFCountingLabel.swift
//
// Created by EyreFree on 16/12/11.
// Copyright © 2016年 EyreFree. All rights reserved.
// Copyright (c) 2016 EyreFree (https://www.eyrefree.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import UIKit

enum UILabelCountingMethod: Int {
enum EFLabelCountingMethod: Int {
case Linear = 0
case EaseIn = 1
case EaseOut = 2
Expand Down Expand Up @@ -51,11 +67,11 @@ class UILabelCounterEaseInOut: UILabelCounter {
}
}

//MARK: - UICountingLabel
class UICountingLabel: UILabel {
//MARK: - EFCountingLabel
class EFCountingLabel: UILabel {

var format = "%f"
var method = UILabelCountingMethod.Linear
var method = EFLabelCountingMethod.Linear
var animationDuration = TimeInterval(2)
var formatBlock: ((CGFloat) -> String)?
var attributedFormatBlock: ((CGFloat) -> NSAttributedString)?
Expand Down Expand Up @@ -110,7 +126,7 @@ class UICountingLabel: UILabel {
break
}

let timer = CADisplayLink(target: self, selector: #selector(UICountingLabel.updateValue(timer:)))
let timer = CADisplayLink(target: self, selector: #selector(EFCountingLabel.updateValue(timer:)))
timer.frameInterval = 2
timer.add(to: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
timer.add(to: RunLoop.main, forMode: RunLoopMode.UITrackingRunLoopMode)
Expand Down
6 changes: 2 additions & 4 deletions EFCountingLabel/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>1.0.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand All @@ -31,8 +31,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
69 changes: 48 additions & 21 deletions EFCountingLabel/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
//
// ViewController.swift
// EFCountingLabel
//
// Created by EyreFree on 16/12/11.
// Copyright © 2016年 EyreFree. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var label: UICountingLabel!
@IBOutlet weak var label: EFCountingLabel!

var myLabel: EFCountingLabel!
var countPercentageLabel: EFCountingLabel!
var scoreLabel: EFCountingLabel!
var attributedLabel: EFCountingLabel!

override func viewDidLoad() {
super.viewDidLoad()

setupLabels()
setupButton()

startCount()
}

func setupLabels() {
// make one that counts up
let myLabel = UICountingLabel(frame: CGRect(x: 10, y: 10, width: 200, height: 40))
let myLabel = EFCountingLabel(frame: CGRect(x: 10, y: 10, width: 200, height: 40))
myLabel.method = .Linear
myLabel.format = "%d"
self.view.addSubview(myLabel)
myLabel.countFrom(startValue: 1, to: 10, withDuration: 3.0)
self.myLabel = myLabel

// make one that counts up from 5% to 10%, using ease in out (the default)
let countPercentageLabel = UICountingLabel(frame: CGRect(x: 10, y: 50, width: 200, height: 40))
self.view.addSubview(countPercentageLabel)
let countPercentageLabel = EFCountingLabel(frame: CGRect(x: 10, y: 50, width: 200, height: 40))
countPercentageLabel.format = "%.1f%%"
countPercentageLabel.countFrom(startValue: 5, to: 10)
self.view.addSubview(countPercentageLabel)
self.countPercentageLabel = countPercentageLabel

// count up using a string that uses a number formatter
let scoreLabel = UICountingLabel(frame: CGRect(x: 10, y: 90, width: 200, height: 40))
self.view.addSubview(scoreLabel)
let scoreLabel = EFCountingLabel(frame: CGRect(x: 10, y: 90, width: 200, height: 40))
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
scoreLabel.formatBlock = {
(value) in
return "Score: " + (formatter.string(from: NSNumber(value: Int(value))) ?? "")
}
scoreLabel.method = .EaseOut
scoreLabel.countFrom(startValue: 0, to: 10000, withDuration: 2.5)
self.view.addSubview(scoreLabel)
self.scoreLabel = scoreLabel

// count up with attributed string
let toValue = 100.0
let attributedLabel = UICountingLabel(frame: CGRect(x: 10, y: 130, width: 200, height: 40))
self.view.addSubview(attributedLabel)
let attributedLabel = EFCountingLabel(frame: CGRect(x: 10, y: 130, width: 200, height: 40))
attributedLabel.attributedFormatBlock = {
(value) in
let highlight = [NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 20)]
let normal = [NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 20)]

let prefix = String(format: "%d", Int(value))
let postfix = String(format: "/%d", Int(toValue))
let postfix = String(format: "/%d", 100)

let prefixAttr = NSMutableAttributedString(string: prefix, attributes: highlight)
let postfixAttr = NSAttributedString(string: postfix, attributes: normal)

prefixAttr.append(postfixAttr)
return prefixAttr
}
attributedLabel.countFrom(startValue: 0, to: CGFloat(toValue), withDuration: 2.5)
self.view.addSubview(attributedLabel)
self.attributedLabel = attributedLabel

//storyboard
self.label.method = .EaseInOut
Expand All @@ -67,6 +71,29 @@ class ViewController: UIViewController {
[weak self] () in
self?.label.textColor = UIColor(red: 0, green: 0.5, blue: 0, alpha: 1)
}
}

func setupButton() {
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

let button = UIButton(
frame: CGRect(x: (screenWidth - 72) / 2, y: (screenHeight - 24 - 100), width: 72, height: 24)
)
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor(red: 0, green: 122.0 / 255.0, blue: 1, alpha: 1).cgColor
button.layer.cornerRadius = 4.0
button.setTitleColor(UIColor(red: 0, green: 122.0 / 255.0, blue: 1, alpha: 1), for: .normal)
button.setTitle("Repeat", for: .normal)
button.addTarget(self, action: #selector(ViewController.startCount), for: .touchUpInside)
self.view.addSubview(button)
}

func startCount() {
myLabel.countFrom(startValue: 1, to: 10, withDuration: 3.0)
countPercentageLabel.countFrom(startValue: 5, to: 10)
scoreLabel.countFrom(startValue: 0, to: 10000, withDuration: 2.5)
attributedLabel.countFrom(startValue: 0, to: 100, withDuration: 2.5)
self.label.countFrom(startValue: 0, to: 100)
}
}
Expand Down

0 comments on commit 671f994

Please sign in to comment.