Skip to content

Documention

Sadeq edited this page Mar 28, 2022 · 1 revision

MagicTimer

Version License Gitter

MagicTimerLogo

MagicTimer is a UIView based timer that displays a countdown or count-up timer.
Sample_MagicTimer

Features

  • Fully customizable design
  • Support stop watch / count down mode
  • Support custom time formatter
  • Suppport in background time calculation
  • Modifable time interval / step
  • Fully Managable: start, pause, reset
  • Available in interface builder or using code
  • Support Monospace
  • Easy to use

Requirements

  • iOS 11.0+
  • Swift 5.0+

Communication

  • If you need help with magic timer use Stack Overflow and tag magictimer.
  • If you found a bug, open an issue here on GitHub and follow the guide. The more detail the better!
  • If you want to contribute, submit a pull request!

Installation

Cocoapod

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website.

You can install Cocoapods with the following command:

$ gem install cocoapods

To integrate MagicTimer into your Xcode project using CocoaPods, specify it in your Podfile.

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target <'Your target name'> do
    pod 'MagicTimer'
end

Basic Usage

Interface builder

  1. Drag a UIView onto your view controller and set the view's class to MagicTimerView in the Identity Inspector:

  2. Connect your timer view to your view controller with an IBOutlet:

  3. Import MagicTimer to your View Controller

import MagicTimer
  1. Call startCounting to Begins updates the timer’s display.

Code

import MagicTimer

let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let timer = MagicTimerView(frame: frame)
timer.startCounting() // Begins updates to the timer’s display.

Documention

MagicTimerView(IBDesignable)

An object that displays a countdown or count-up timer. Use a timer object to configure the amount of time and the appearance of the timer text. When you start the timer, Magic timer updates the displayed text automatically on the user’s device without further interactions from your extension.

open class MagicTimerView : UIView

Control

Control timer within the below methods.

startCounting

Begins updates to the timer’s display.

public func startCounting()

stopCounting

Stops updates to the timer’s display.

public func stopCounting()

reset

Reset timer to zero.

public func reset()

resetToDefault

Reset timer to the default value.

public func resetToDefault()

currentState

The current state of the timer.

public var currentState: MGStateManager.TimerState { get }

mode

The mode of the timer. default is stop watch.

public var mode: MGCountMode { get set }

didStateChange

A callback that notifies when timer state did change.

public var didStateChange: ((MGStateManager.TimerState) -> Void)?

Formatter

MagicTimerView can accept custom time formatter. Any time formatter should confrom MGTimeFormatter.

MGTimeFormatter

public protocol MGTimeFormatter

Sample custom time formatter

class CustomTimeFormatter: MGTimeFormatter {
    let dateComponent = DateComponentsFormatter()
    
    init() {
        dateComponent.unitsStyle = .positional
        dateComponent.allowedUnits = [.second]
    }
    func converToValidFormat(ti: TimeInterval) -> String? {
        dateComponent.string(from: ti)
    }
}

Observe

You can observe elapsed time in two ways.
First: using elapsedTime property.

private(set) var elapsedTime: TimeInterval?

Second: using Delegate to observe elapsedTime when elapsed time did change.

protocol MagicTimerViewDelegate: AnyObject {
    func timerElapsedTimeDidChange(timer: MagicTimerView, elapsedTime: TimeInterval)
}

Behavior

Customize timer behavior within the below properties.

timeInterval

An interval that affects to observing time. Default is 1.

public var timeInterval: Int { get set }

effectiveValue

A value that affects to counting. Default is 1.

public var effectiveValue: Int { get set }

defaultValue

The default value of the timer. The initial value is 0.

public var defaultValue: Int { get set }

Background mode

isActiveInBackground

A Boolean value that determines whether the calculation of timer active in the background.

public var isActiveInBackground: Bool { get set }

Design

Gradient

Set gradient with a custom angle.

startColor(IBInspectable)

The Start color of the gradient. The default is nil.

public var startColor: UIColor? { get set }

endColor(IBInspectable)

The end color of the gradient. The default is nil.

public var endColor: UIColor? { get set }

angle(IBInspectable)

The angle of the gradient. The default is 270 degrees.

public var angle: CGFloat { get set }

Border

borderColor(IBInspectable)

The color of the border. Default is clear.

public var borderColor: UIColor { get set }

borderWidth(IBInspectable)

The width of the border. Default is 0.0.

public var borderWidth: CGFloat { get set }

CornerRadius

cornerRadius(IBInspectable)

The radius to use when drawing rounded corners for the timer view background. Default is 0.0.

public var cornerRadius: CGFloat { get set }

Text

fontSize(IBInspectable)

Font size of timer label. Just available in interface builder.

font

The font used to display the timer label text.

public var font: UIFont? { get set }

textColor

The color of the timer label.

public var textColor: UIColor! { get set }

BackgroundDesign

backgroundImage

Background image of the timer. Default is nil.

public var backgroundImage: UIImage? { get set }

Author

sadegh bitarafan(sadeghgoo), [email protected]

License

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