Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to swift 5.0 #Hacktoberfest #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions CRNetworkButton/Classes/CRNetworkButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ open class CRNetworkButton: UIButton {
layer.fillColor = nil
layer.strokeColor = self.crDotColor.cgColor
layer.bounds = self.circleBounds
layer.path = UIBezierPath(arcCenter: self.boundsCenter, radius: self.boundsCenter.y - self.crLineWidth / 2,
startAngle: CGFloat(-M_PI_2), endAngle: 3*CGFloat(M_PI_2), clockwise: true).cgPath
layer.path = UIBezierPath(arcCenter: self.boundsCenter,
radius: self.boundsCenter.y - self.crLineWidth / 2,
startAngle: CGFloat(-.pi / 2.0),
endAngle: 3*CGFloat(.pi / 2.0),
clockwise: true).cgPath

layer.strokeEnd = 0
layer.lineCap = kCALineCapRound
layer.lineCap = .round
layer.lineWidth = self.crLineWidth

return layer
Expand Down Expand Up @@ -228,7 +231,7 @@ open class CRNetworkButton: UIButton {
progressLayer.removeFromSuperlayer()
progressLayer.strokeEnd = 0
CATransaction.commit()
setTitleColor(startTitleColor, for: UIControlState())
setTitleColor(startTitleColor, for: .normal)
}

open func startAnimate() {
Expand Down Expand Up @@ -258,7 +261,7 @@ open class CRNetworkButton: UIButton {


// MARK: - Selector && Action
func touchUpInside(_ sender: CRNetworkButton) {
@objc func touchUpInside(_ sender: CRNetworkButton) {
guard crState != .finished else {
return
}
Expand Down Expand Up @@ -340,13 +343,13 @@ extension CRNetworkButton {
}

fileprivate func setupButton() {
setTitle(startText, for: UIControlState())
setTitle(startText, for: .normal)

layer.cornerRadius = bounds.midY
layer.borderColor = crBorderColor.cgColor
layer.addSublayer( borderLayer )

startTitleColor = titleColor(for: UIControlState())
startTitleColor = titleColor(for: .normal)
startBackgroundColor = backgroundColor
}

Expand All @@ -366,8 +369,8 @@ extension CRNetworkButton {
attribute: .notAnAttribute, multiplier: 1,
constant: bounds.height)

conWidth.priority = UILayoutPriorityDefaultLow
conHeight.priority = UILayoutPriorityDefaultLow
conWidth.priority = .defaultLow
conHeight.priority = .defaultLow

NSLayoutConstraint.activate( [conWidth, conHeight] )
}
Expand All @@ -377,13 +380,13 @@ extension CRNetworkButton {
//MARK: Update
fileprivate func updateText() {
guard !shouldAutoReverse else {
setTitle(startText, for: UIControlState())
setTitle(startText, for: .normal)
return
}

switch crState {
case .ready:
setTitle(startText, for: UIControlState())
setTitle(startText, for: .normal)

case .loading:
fallthrough
Expand All @@ -392,7 +395,7 @@ extension CRNetworkButton {
fallthrough

case .finished:
setTitle(stopedByError ? errorText : endText, for: UIControlState())
setTitle(stopedByError ? errorText : endText, for: .normal)
}
}
fileprivate func clearLayerContext() {
Expand Down Expand Up @@ -454,9 +457,9 @@ extension CRNetworkButton {
layerGroup.animations = [boundAnim,colorAnim]
layerGroup.duration = Constants.prepareLoadingAnimDuration
layerGroup.delegate = self
layerGroup.fillMode = kCAFillModeForwards
layerGroup.fillMode = .forwards
layerGroup.isRemovedOnCompletion = false
layerGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
layerGroup.timingFunction = CAMediaTimingFunction(name: .easeOut)
assignContext(.LoadingStart, anim: layerGroup)

layer.add(layerGroup, forKey: AnimKeys.bounds)
Expand All @@ -476,9 +479,9 @@ extension CRNetworkButton {
borderGroup.animations = [borderAnim,borderBounds,borderPosition]
borderGroup.duration = Constants.prepareLoadingAnimDuration
borderGroup.delegate = self
borderGroup.fillMode = kCAFillModeForwards
borderGroup.fillMode = .forwards
borderGroup.isRemovedOnCompletion = false
borderGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
borderGroup.timingFunction = CAMediaTimingFunction(name: .easeOut)
assignContext(.LoadingStart, anim: borderGroup)

borderLayer.add(borderGroup, forKey: nil)
Expand All @@ -500,7 +503,7 @@ extension CRNetworkButton {

titleLabel?.layer.opacity = 0
if !shouldAutoReverse {
setTitleColor(crDotColor, for: UIControlState())
setTitleColor(crDotColor, for: .normal)
}
}

Expand All @@ -509,8 +512,8 @@ extension CRNetworkButton {
let arCenter = boundsCenter
let radius = circleBounds.midX - crLineWidth / 2

var lines = [CAShapeLayer]()
let lineOffset:CGFloat = 2 * CGFloat(M_PI) / CGFloat(linesCount)
var lines: [CAShapeLayer] = []
let lineOffset: CGFloat = 2 * .pi / CGFloat(linesCount)

for i in 0..<linesCount {
let line = CAShapeLayer()
Expand All @@ -526,7 +529,7 @@ extension CRNetworkButton {
line.strokeColor = crDotColor.cgColor
line.lineWidth = crLineWidth
line.fillColor = crDotColor.cgColor
line.lineCap = kCALineCapRound
line.lineCap = .round

layer.insertSublayer(line, above: borderLayer)
line.position = arCenter
Expand All @@ -537,7 +540,7 @@ extension CRNetworkButton {
opacityAnim.fromValue = 0

let rotation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.byValue = NSNumber(value: 2*M_PI as Double)
rotation.byValue = NSNumber(value: 2 * .pi as Double)
rotation.duration = velocity
rotation.repeatCount = Float.infinity

Expand Down Expand Up @@ -596,13 +599,13 @@ extension CRNetworkButton {
animationScale.duration = Constants.resetLinesPositionAnimDuration
animationScale.toValue = NSNumber(value: Float(bounds.height * 2/3) as Float)
animationScale.isRemovedOnCompletion = false
animationScale.fillMode = kCAFillModeForwards
animationScale.fillMode = .forwards
animationScale.delegate = self
assignContext(.LoadingFinishing, anim: animationScale)

// dots will preparing
let dotStartSize = CGSize(width: 2, height: 2)
let angleOffset = 2*CGFloat(M_PI) / CGFloat(linesCount)
let angleOffset = 2 * .pi / CGFloat(linesCount)
let radius = circleBounds.midY - (crLineWidth * 2)

var dots = [CAShapeLayer]()
Expand Down Expand Up @@ -657,7 +660,7 @@ extension CRNetworkButton {
stopedByError ? errorCrossMarkLayer.add(opacityAnim, forKey: nil) : checkMarkLayer.add(opacityAnim, forKey: nil)
updateText()
if stopedByError {
setTitleColor(crErrorColor, for: UIControlState())
setTitleColor(crErrorColor, for: .normal)
}
borderLayer.borderColor = stopedByError ? crErrorColor.cgColor : crDotColor.cgColor
borderLayer.opacity = 1
Expand All @@ -679,7 +682,7 @@ extension CRNetworkButton {
borderBounce.beginTime = CACurrentMediaTime() + opacityAnim.duration
borderBounce.delegate = self
borderBounce.isRemovedOnCompletion = false
borderBounce.fillMode = kCAFillModeBoth
borderBounce.fillMode = .both
assignContext(.LoadingFinishing, anim: borderBounce)

let borderPosition = CABasicAnimation(keyPath: "position")
Expand All @@ -688,7 +691,7 @@ extension CRNetworkButton {
borderPosition.beginTime = CACurrentMediaTime() + opacityAnim.duration
borderPosition.delegate = self
borderPosition.isRemovedOnCompletion = false
borderPosition.fillMode = kCAFillModeBoth
borderPosition.fillMode = .both
assignContext(.LoadingFinishing, anim: borderPosition)

borderLayer.add(borderBounce, forKey: nil)
Expand All @@ -712,7 +715,7 @@ extension CRNetworkButton {
layerGroup.duration = Constants.bounceDuration * borderBounce.keyTimes![1].doubleValue
layerGroup.beginTime = borderBounce.beginTime
layerGroup.delegate = self
layerGroup.fillMode = kCAFillModeBoth
layerGroup.fillMode = .both
layerGroup.isRemovedOnCompletion = false
assignContext(.LoadingFinishing, anim: layerGroup)

Expand All @@ -725,7 +728,7 @@ extension CRNetworkButton {
opacityTitleAnim.toValue = 1
opacityTitleAnim.duration = layerGroup.duration
opacityTitleAnim.beginTime = layerGroup.beginTime
opacityTitleAnim.fillMode = kCAFillModeBoth
opacityTitleAnim.fillMode = .both
opacityTitleAnim.isRemovedOnCompletion = false

borderPosition.fromValue = NSValue(cgPoint: boundsCenter)
Expand Down Expand Up @@ -764,8 +767,8 @@ extension CRNetworkButton {
layer.opacity = 0
layer.fillColor = nil
layer.strokeColor = UIColor.white.cgColor
layer.lineCap = kCALineCapRound
layer.lineJoin = kCALineJoinRound
layer.lineCap = .round
layer.lineJoin = .round
layer.lineWidth = crLineWidth / 2

return layer
Expand All @@ -788,8 +791,8 @@ extension CRNetworkButton {
let firstRadius = 0.5 * circleBounds.midY
let lastRadius = 0.8 * circleBounds.midY

let firstAngle = CGFloat(-3 * M_PI_4)
let lastAngle = CGFloat(-1 * M_PI_4)
let firstAngle = CGFloat(-3 * .pi / 4.0)
let lastAngle = CGFloat(-1 * .pi / 4.0)

var startPoint = CGPoint(x: firstRadius * cos(firstAngle), y: firstRadius * sin(firstAngle))
var midPoint = CGPoint.zero
Expand Down
9 changes: 5 additions & 4 deletions Example/CRNetworkButton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -454,7 +455,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -469,7 +470,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -488,7 +489,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -503,7 +504,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>CRNetworkButton-Example.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
29 changes: 2 additions & 27 deletions Example/CRNetworkButton/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
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 throttle down OpenGL ES frame rates. 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 inactive 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:.
}



}

20 changes: 12 additions & 8 deletions Example/CRNetworkButton/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import CRNetworkButton

class ViewController: UIViewController {
final class ViewController: UIViewController {

@IBOutlet weak var button: CRNetworkButton!
@IBOutlet weak var failableButtton: CRNetworkButton!
Expand All @@ -29,8 +29,7 @@ class ViewController: UIViewController {
super.viewDidLoad()
}


func updateProgress() {
@objc func updateProgress() {
guard progress <= 1 else {
timer?.invalidate()
button.stopAnimate()
Expand All @@ -45,8 +44,10 @@ class ViewController: UIViewController {



//MARK: - Action
// MARK: - Action

extension ViewController {

@IBAction func topButtonTapped(_ sender: CRNetworkButton) {
guard !sender.isSelected else {
if sender.currState == .finished {
Expand All @@ -57,21 +58,24 @@ extension ViewController {
}

sender.isSelected = true
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(2*NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(Int64(2*NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
sender.stopAnimate()
}
}

@IBAction func secondButtonTapped(_ sender: CRNetworkButton) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(2*NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(Int64(2*NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
sender.stopAnimate()
}
}

@IBAction func buttonTapped(_ sender: CRNetworkButton) {
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: frequencyUpdate, target:self, selector: #selector(ViewController.updateProgress),
userInfo: nil, repeats: true)
timer = Timer.scheduledTimer(timeInterval: frequencyUpdate,
target:self,
selector: #selector(ViewController.updateProgress),
userInfo: nil,
repeats: true)
}

@IBAction func failableButtonTapped(_ sender: CRNetworkButton) {
Expand Down
Loading