FiveUIUtils 1.0.0

FiveUIUtils 1.0.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布版本2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

Niko Mikulicic 维护。



  • iOS 库团队

FiveUIUtils

关于

此捆绑包包含我们倾向于在我们许多项目(我们是指Five Agency 的 iOS 团队)中重复使用的 Swift UI 工具函数。我们预计还会添加更多,但在此期间,您可用于/重新使用/升级所有现有代码。有关辅助函数、图像处理、分页、布局和其他有用的片段的任何一切。

要求

  • Xcode 8.0+
  • Swift 3.0
  • iOS 8.0+

安装

没有其他依赖项。

当前支持的安装选项

手动使用Git子模块

  • 将 FiveUIUtils 添加为子模块
$ git submodule add https://github.com/fiveagency/ios-five-ui-utils.git
  • FiveUIUtils.xcodeproj 拖入项目导航器。

  • 转到 项目 > 目标 > 构建阶段 > 与二进制文件链接库,点击 + 并选择 FiveUIUtils.framework 目标。

示例

要运行示例项目,请执行以下操作。

  • 通过以下方式克隆仓库
$ git clone https://github.com/fiveagency/ios-five-ui-utils.git YOUR_DESTINATION_FOLDER
  • 打开 FiveUIUtils.xcworkspace,选择 示例 方案并按 运行。此方法将构建一切并运行示例应用程序。

使用方法

DeviceInfo 模块

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

public static var sizeCategory: ScreenSizeCategory

示例使用

// 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

示例使用

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

UIImage+Five 模块

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

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

示例使用

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

示例使用

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

示例使用

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

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

示例使用

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

示例使用

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).
 */

公开函数 pixelColor(at normalizedCoordinates: CGPoint) -> UIColor

示例使用

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

ScrollViewPager 模块

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

公开函数 contentOffset(forPageAtIndex index: Int) -> CGPoint

示例使用

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.
 */

公开函数 pageIndex(forContentOffset contentOffset: CGPoint) -> Int

示例使用

// 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.
 */

公开函数 snapContentOffset(_ contentOffset: CGPoint, withVelocity velocity: CGPoint, sensitivity: CGFloat) -> CGPoint

示例使用

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.
 */

公开函数 distance(fromPageAtIndex index: Int, toContentOffset offset: CGPoint) -> CGFloat

示例使用

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.
 */

公开函数 normalizedDistance(fromPageAtIndex index: Int, toContentOffset offset: CGPoint) -> CGFloat

示例使用

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

KeyboardLayoutAdapter 模块

 /**
 - 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.
 */

公开构造函数 init(withBottomConstraint bottomConstraint: NSLayoutConstraint, inView view: UIView, targetConstant: CGFloat)

示例使用

// 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 模块

/**
 The number of active tasks.
 */

公开属性 taskCount: Int

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

公开函数 startActivity()

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

公开函数 endActivity()

示例使用

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

作者

五个 UI 工具库团队(按字母顺序排列)

  • Ivan Vranjić

  • Kristijan Rožanković

  • Miran Brajsa

  • Niko Mikuličić

许可证

FiveUIUtils 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。