FiveUIUtils 1.0.0

FiveUIUtils 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Niko Mikulicic.



  • By
  • iOS libraries team

FiveUIUtils

About

This bundle contains Swift UI utility functions we tend to re-use on many of our projects (us being Five Agency’s iOS Team). We expect there’ll be many more added, but in the mean time, you’re free to use/re-use/upgrade all of the existing code here. Everything and anything regarding helper functions, image manipulations, paging, layouting and other nice to have snippets.

Requirements

  • Xcode 8.0+
  • Swift 3.0
  • iOS 8.0+

Installation

There are no additional dependencies.

Currently supported installation options:

Manually using git submodules

  • Add FiveUIUtils as a submodule
$ git submodule add https://github.com/fiveagency/ios-five-ui-utils.git
  • Drag FiveUIUtils.xcodeproj into Project Navigator.

  • Go to Project > Targets > Build Phases > Link Binary With Libraries, click + and select FiveUIUtils.framework target.

Examples

To run the example project do the following.

  • Clone the repo by typing:
$ git clone https://github.com/fiveagency/ios-five-ui-utils.git YOUR_DESTINATION_FOLDER
  • Open FiveUIUtils.xcworkspace, choose Example scheme and hit run. This method will build everything and run the sample app.

Usage

DeviceInfo module

/**
 Screen size category this device falls into.
 */

public static var sizeCategory: ScreenSizeCategory

Example usage:

// on iPhone6
DeviceInfo.sizeCategory // iPhone47in
/**
 Returns true if this device has the screen size less than or equal to given size category.
 */

public static func hasScreenSizeLessOrEqual(to size: ScreenSizeCategory) -> Bool

Example usage:

// on iPhone6
DeviceInfo.hasScreenSizeLessOrEqual(to: .iPhone47in) // true

UIImage+Five module

/**
 Re-colors an image with respect to transparency.
 */

public func changeColor(to color: UIColor) -> UIImage

Example usage:

let image = UIImage(named: "original")
let color = UIColor(red: 0 / 255.0, 
                    green: 254 / 255.0, 
                    blue: 248 / 255.0, 
                    alpha: 1.0)
let recoloredImage = image.changeColor(to: color)
/**
 Translates an image by given amount.
 */

public func translate(by point: CGPoint) -> UIImage

Example usage:

let point = CGPoint(x: -40, y: 0)
let translatedImage = image.translate(by: point)
/**
 Rotates an image by given angle in degrees.
 */

public func rotate(by degrees: CGFloat) -> UIImage

Example usage:

let rotatedImage = image.rotate(by: 30)
/**
 Resizes an image to given size.
 */

public func resize(to size: CGSize) -> UIImage

Example usage:

let newSize = CGSize(width: 0.4 * image.size.width, 
                     height: 0.4 * image.size.height)
let resizedImage = image.resize(to: newSize)
/**
 Crops image with rectangle of given size at given position.
 */

public func crop(at point: CGPoint, size: CGSize) -> UIImage

Example usage:

let center = CGPoint(x: image.size.width/2, 
                     y: image.size.height/2)
let croppedImage = image.crop(at: center, size: newSize)
/**
 Color of a single image pixel at normalized coordinates. Lower left corner of the image is at (0,0).
 */

public func pixelColor(at normalizedCoordinates: CGPoint) -> UIColor

Example usage:

let sample = image.pixelColor(at: CGPoint(x: 0.5, y: 0.5))
let sampledImage = UIImage(color: sample)

ScrollViewPager module

/**
 Calculates content offset for the given page in enclosing scroll view.
 */

public func contentOffset(forPageAtIndex index: Int) -> CGPoint

Example usage:

let pager = ScrollViewPager(scrollAxis: .horizontal,
                            contentInset: 5,
                            pageLength: 13,
                            pageSpacing: 2)

pager.contentOffset(forPageAtIndex: 0)  // (-5, 0)
pager.contentOffset(forPageAtIndex: 1)  // (10, 0)

/**
 Calculates the index of a page at specified point in enclosing scroll view.
 */

public func pageIndex(forContentOffset contentOffset: CGPoint) -> Int

Example usage:

// halfway point between pages 0 and 1 is (2.5, 0)
pager.pageIndex(forContentOffset: CGPoint(x: 2.4, y: 0)) // 0
pager.pageIndex(forContentOffset: CGPoint(x: 2.6, y: 0)) // 1
/**
 Calculates content offset for the target page on which the scrolling should stop.
 */

public func snapContentOffset(_ contentOffset: CGPoint, withVelocity velocity: CGPoint, sensitivity: CGFloat) -> CGPoint

Example usage:

pager.snapContentOffset(CGPoint(x: 1.4, y: 0),
                        withVelocity: CGPoint(x: 1, y: 0),
                        sensitivity: 1) // (-5, 0)
pager.snapContentOffset(CGPoint(x: 1.6, y: 0),
                        withVelocity: CGPoint(x: 1, y: 0),
                        sensitivity: 1) // (10, 0)
/**
 Distance between the page and the given content offset in scroll view.
 */

public func distance(fromPageAtIndex index: Int, toContentOffset offset: CGPoint) -> CGFloat

Example usage:

pager.distance(fromPageAtIndex: 0,
               toContentOffset: CGPoint(x: 2.4, y: 0)) // 7.4
/**
 Normalized distance between the page and the given content offset in scroll view. Content offsets of two adjacent pages have normalized distance equal to 1.
 */

public func normalizedDistance(fromPageAtIndex index: Int, toContentOffset offset: CGPoint) -> CGFloat

Example usage:

pager.normalizedDistance(fromPageAtIndex: 0,
                         toContentOffset: CGPoint(x: 2.4, y: 0)) // 0.49333

KeyboardLayoutAdapter module

 /**
 - parameter bottomConstraint: Constraint to modify in order to fit in the keyboard.
 - parameter view: View which contains `bottomConstraint`.
 - parameter targetConstant: The value of `bottomConstraint` constant when keyboard is raised.
 */

public init(withBottomConstraint bottomConstraint: NSLayoutConstraint, inView view: UIView, targetConstant: CGFloat)

Example usage:

// inside view controller
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
private var keyboardLayoutAdapter: KeyboardLayoutAdapter!

override func viewDidLoad() {
    super.viewDidLoad()
    keyboardLayoutAdapter = KeyboardLayoutAdapter(
        withBottomConstraint: bottomConstraint,
        inView: view,
        targetConstant: 20)
    keyboardLayoutAdapter.registerForKeyboardNotifications()
}

NetworkActivityController module

/**
 The number of active tasks.
 */

public var taskCount: Int

/**
 Turns network activity indicator on if it wasn't previously activated.
 */

public func startActivity()

/**
 Turns network activity indicator off only if there aren't any currently active tasks.
 */

public func endActivity()

Example usage:

NetworkActivityController.shared.tasks // 0
NetworkActivityController.shared.startActivity()
NetworkActivityController.shared.tasks // 1
NetworkActivityController.shared.endActivity()
NetworkActivityController.shared.tasks // 0

Authors

Five UI Utils library team (listed alphabetically)

  • Ivan Vranjić

  • Kristijan Rožanković

  • Miran Brajsa

  • Niko Mikuličić

License

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