Skip to content

Commit

Permalink
touchUpInside closure, update readme.md, add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sjc-bui committed May 20, 2021
1 parent c8c8913 commit a0176d7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.
30 changes: 20 additions & 10 deletions Example/QBIndicatorButton/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 21,32 @@ class ViewController: UIViewController {
super.viewDidLoad()

button1.tag = 1
button1.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)

button1.touch { btn in
self.btnClick(btn)
}
button2.tag = 2
button2.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)

button2.touch { btn2 in
self.btnClick(btn2)
}
button3.tag = 3
button3.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)

button3.touch { btn3 in
self.btnClick(btn3)
}
button4.tag = 4
button4.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)

button4.touch { btn4 in
self.btnClick(btn4)
}
button5.tag = 5
button5.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
button5.touch { btn5 in
btn5.start {
DispatchQueue.main.asyncAfter(deadline: .now() 2) {
btn5.stop()
}
}
}
}

@objc func btnClick(_ sender: QBIndicatorButton) {
func btnClick(_ sender: QBIndicatorButton) {
sender.start {
print("button \(sender.tag) is starting...")
}
Expand Down
61 changes: 49 additions & 12 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,8 @@ import XCTest
import QBIndicatorButton

class Tests: XCTestCase {

var btn = QBIndicatorButton()

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
Expand All @@ -12,17 13,53 @@ class Tests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")

func testButtonProperties() {
btn = QBIndicatorButton(text: "test",
textColor: .white,
font: UIFont.systemFont(ofSize: 18),
backgroundColor: .green,
cornerRadius: 6.0)
XCTAssertTrue(btn.titleLabel?.text == "test")
XCTAssertTrue(btn.titleLabel?.textColor == .white)
XCTAssertTrue(btn.titleLabel?.font == .systemFont(ofSize: 18))
XCTAssertTrue(btn.backgroundColor == .green)
XCTAssertTrue(btn.layer.cornerRadius == 6.0)
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure() {
// Put the code you want to measure the time of here.
}

func testGradientBackground() {
btn = QBIndicatorButton(text: "test2")
btn.gradientEnabled = true
btn.gradientStartColor = .red
btn.gradientEndColor = .blue
btn.gradientDirection = 1
XCTAssertTrue(btn.titleLabel?.text == "test2")
XCTAssertTrue(btn.titleLabel?.textColor == .white)
XCTAssertTrue(btn.backgroundColor == .black)
XCTAssertTrue(btn.layer.cornerRadius == 4.0)
XCTAssertTrue(btn.gradientEnabled == true)
XCTAssertTrue(btn.gradientStartColor == UIColor.red)
XCTAssertTrue(btn.gradientEndColor == UIColor.blue)
XCTAssertTrue(btn.gradientDirection == 1)
}

func testIndicatorPosition() {
btn = QBIndicatorButton(text: "test3", textColor: .red)
btn.activityIndicatorPosition = 1
XCTAssertTrue(btn.titleLabel?.text == "test3")
XCTAssertTrue(btn.titleLabel?.textColor == .red)
XCTAssertTrue(btn.activityIndicatorPosition == 1)
}

func testStartLoading() {
btn.start()
XCTAssertTrue(btn.isLoading)
XCTAssertNotNil(btn.activityIndicator)
}

func testStopLoading() {
btn.stop()
XCTAssertFalse(btn.isLoading)
XCTAssertNil(btn.activityIndicator)
}

}
5 changes: 3 additions & 2 deletions QBIndicatorButton.podspec
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
Pod::Spec.new do |s|
s.name = 'QBIndicatorButton'
s.version = '0.1.1'
s.version = '0.1.2'
s.summary = 'Custom of UIButton in Swift'

s.description = <<-DESC
Expand All @@ -12,6 12,7 @@ Activity Indicator Button written in Swift
s.author = { 'sjc-bui' => '[email protected]' }
s.source = { :git => 'https://github.com/sjc-bui/QBIndicatorButton.git', :tag => s.version.to_s }

s.ios.deployment_target = '9.0'
s.ios.deployment_target = '12.0'
s.swift_version = '5.0'
s.source_files = 'QBIndicatorButton/Classes/**/*'
end
17 changes: 16 additions & 1 deletion QBIndicatorButton/Classes/QBIndicatorButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@ public typealias BtnCallBack = (() -> Void)?
@IBDesignable
open class QBIndicatorButton: UIButton {

private var activityIndicator: UIActivityIndicatorView!
public var activityIndicator: UIActivityIndicatorView!

private var activityIndicatorColor: UIColor = .white

Expand All @@ -22,6 22,8 @@ open class QBIndicatorButton: UIButton {

private var padding: CGFloat = 0.0

public var isLoading: Bool = false

@IBInspectable open var animatedScale: CGFloat = 1.0

@IBInspectable open var animatedScaleDuration: Double = 0.2
Expand Down Expand Up @@ -130,9 132,20 @@ open class QBIndicatorButton: UIButton {
self.layer.insertSublayer(gradient, below: self.imageView?.layer)
}

fileprivate var action: ((_ button: QBIndicatorButton) -> Void)?

open func touch(action: ((_ button: QBIndicatorButton) -> Void)? = nil) {
self.action = action
}

@objc func touchUpInsideEvent(sender: QBIndicatorButton) {
self.action?(sender)
}

override open func layoutSubviews() {
super.layoutSubviews()
gradient?.frame = self.layer.bounds
self.addTarget(self, action: #selector(touchUpInsideEvent), for: .touchUpInside)
}

public override init(frame: CGRect) {
Expand Down Expand Up @@ -180,6 193,7 @@ open class QBIndicatorButton: UIButton {
}

self.isUserInteractionEnabled = false
self.isLoading = true
activityIndicator.isUserInteractionEnabled = false
let indicatorWidth = activityIndicator.frame.width != 0 ?
activityIndicator.frame.width : 20
Expand Down Expand Up @@ -213,6 227,7 @@ open class QBIndicatorButton: UIButton {

self.activityIndicator.stopAnimating()
self.isUserInteractionEnabled = true
self.isLoading = false
self.activityIndicator.removeFromSuperview()

UIView.transition(with: self,
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 41,13 @@ loadingButton = QBIndicatorButton(text: "Tap me",
backgroundColor: .systemBlue,
cornerRadius: 4.0)
```
Touch Up Inside closure.
```swift
loadingButton.touch { btn in
// do stuff here
btn.start()
}
```

Show loading indicator.
```swift
Expand Down

0 comments on commit a0176d7

Please sign in to comment.