QBIndicatorButton

June 10, 2021 ยท View on GitHub

Build Status Version License Platform

Custom of UIButton in Swift.

Screenshot

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Features

  • Edit button style from storyboard or code
  • Set a gradient background and gradient directions
  • Customize the corner radius, border color, border width
  • Customize the button animation: scale, scale duration
  • Edit the button shadows: color, opacity, offset, radius
  • Show, hide activity indicator inside the button

Requirements

  • Swift 5.0
  • iOS 11.0+

Getting started

Storyboard Setup

Drag and drop UIButton into your Storyboard and set its class and module to QBIndicatorButton.

custom class

Customize your button by setting properties from the Interface Builder.

custom properties

Code Setup

var loadingButton: QBIndicatorButton!
loadingButton = QBIndicatorButton(text: "Tap me",
                              textColor: UIColor.white,
                              font: UIFont.systemFont(ofSize: 18, weight: .semibold),
                              backgroundColor: .systemBlue,
                              cornerRadius: 4.0)

Button touch closure.

loadingButton.touch({ btn in
    // do stuff here
    btn.start()
}, for: .touchUpInside)

Show loading indicator.

loadingButton.start()
// or
loadingButton.start {
    // do something when start
    print("starting...")
}

Hide loading indicator.

loadingButton.stop()
// or
loadingButton.stop {
    // do something when stop
    print("stopping...")
}

Appearance

Supported appearance properties are:

PropertyTypeDescriptionDefault value
animatedScaleCGFloatAnimated scale1.0
animatedScaleDurationDoubleAnimated scale duration0.2
borderColorUIColorBorder colorUIColor.clear
borderWidthCGFloatBorder width0.0
cornerRadiusCGFloatCorner radius4.0
shadowColorUIColorThe color of the layer's shadowUIColor.clear
shadowOffsetCGSizeThe offset of the layer's shadow.zero
shadowOpacityFloatThe opacity of the layer's shadow0.0
shadowRadiusCGFloatThe blur radius of the layer's shadow0.0
gradientEnabledBoolEnable gradient background colorfalse
gradientStartColorUIColorStart of color gradientUIColor.clear
gradientEndColorUIColorEnd of color gradientUIColor.clear
gradientDirectionIntDirection of color gradient 0~71
activityIndicatorPositionIntPosition of activity indicator 0~21
titleFadeDurationDoubleButton title fade animated duration0.3
indicatorRotateDuration (v0.1.7)CFTimeIntervalThe activity indicator rotate duration1.0
indicatorStrokeColor (v0.1.7)UIColorCustom color of activity indicator.white
indicatorStrokeWidth (v0.1.7)CGFloatStroke width of activity indicator3.0

QBIndicatorButton also supported round every single corner of the button.

// round topLeft and topRight corner only
loadingButton.roundCorners(corners: [.topLeft, .topRight], radius: 20)
// or
// round all corner
loadingButton.roundCorners(corners: [.allCorners], radius: 20)

Predefined positions of activity indicator.

public enum IndicatorPosition: Int {
    case left   = 0
    case center = 1
    case right  = 2
}

Predefined directions for color gradient.

public enum GradientDirection: Int {
    case toTop         = 0
    case toRight       = 1
    case toBottom      = 2
    case toLeft        = 3
    case toTopRight    = 4
    case toTopLeft     = 5
    case toBottomRight = 6
    case toBottomLeft  = 7
}

Installation

QBIndicatorButton is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'QBIndicatorButton'

Contributing

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you need help, open an issue.
  • If you want to contribute, submit a pull request.

MIT License

QBIndicatorButton is available under the MIT license. See the LICENSE file for more info.

Made with :heart: by sjc-bui.