Skip to content

Commit

Permalink
Merge pull request #4 from maxsokolov/develop
Browse files Browse the repository at this point in the history
Support custom colors
  • Loading branch information
maxsokolov authored Jul 18, 2016
2 parents 62391af + a893cf7 commit 60a47ac
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cribble.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|
s.name = 'Cribble'
s.module_name = 'Cribble'

s.version = '1.0.1'
s.version = '1.1.0'

s.homepage = 'https://github.com/maxsokolov/Cribble'
s.summary = 'Swifty tool for visual testing iPhone and iPad apps'
Expand Down
3 changes: 3 additions & 0 deletions Demo/CribbleDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let options = CribbleOptions(horizontalStep: 10, verticalStep: 10, opacity: 0.7, color: UIColor.yellowColor())
Cribble.shared.options = options

return true
}

Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p align="left">
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift_2.2-compatible-4BC51D.svg?style=flat" alt="Swift 2.2 compatible" /></a>
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>
<a href="https://cocoapods.org/pods/cribble"><img src="https://img.shields.io/badge/pod-1.0.0-blue.svg" alt="CocoaPods compatible" /></a>
<a href="https://cocoapods.org/pods/cribble"><img src="https://img.shields.io/badge/pod-1.1.0-blue.svg" alt="CocoaPods compatible" /></a>
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
<a href="https://raw.githubusercontent.com/maxsokolov/cribble/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
</p>
Expand All @@ -16,8 +16,7 @@

An [example app](Demo) is included demonstrating Cribble's functionality.

#### Basic usage

#### Usage
Simply add the following lines into your app delegate:
```swift
import Cribble
Expand All @@ -30,6 +29,14 @@ override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
```
Shake a device and explore ui's roughness.

#### Custom options
You may want to use your own parameters for the grid. In that case simply use `CribbleOptions`:
```swift
let options = CribbleOptions(horizontalStep: 10, verticalStep: 10, opacity: 0.7, color: UIColor.redColor())

Cribble.shared.options = options
```

## Installation

#### CocoaPods
Expand Down
14 changes: 9 additions & 5 deletions Sources/Cribble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ class CribbleWindow: UIWindow {
}

public class Cribble {

public static let shared = Cribble()
public var options: CribbleOptions?

private var window: CribbleWindow?
private var cribbleController: CribbleController?

public var hidden: Bool = true {
didSet {
if !hidden {
Expand All @@ -59,13 +60,14 @@ public class Cribble {
}
}

public func display() {
private func display() {

if window != nil {
return
}

cribbleController = UIStoryboard(name: "Cribble", bundle: NSBundle.frameworkBundle).instantiateViewControllerWithIdentifier("CribbleController") as? CribbleController
cribbleController = CribbleController.storyboardController()
cribbleController?.options = options
cribbleController?.onChangeOptionsButtonFrame = { [weak self] frame in
self?.window?.touchableRect = frame
}
Expand All @@ -76,12 +78,14 @@ public class Cribble {
window?.makeKeyAndVisible()
}

public func hide() {
private func hide() {

if window == nil {
return
}

options = cribbleController?.options

window?.hidden = true
window?.rootViewController = nil
cribbleController?.presentedViewController?.dismissViewControllerAnimated(false, completion: nil)
Expand Down
28 changes: 19 additions & 9 deletions Sources/CribbleController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ class CribbleController: UIViewController {

@IBOutlet weak var optionsButton: UIButton!

var options: CribbleOptions?
var onChangeOptionsButtonFrame: ((frame: CGRect?) -> Void)?
var cribbleView: CribbleView? {
return view as? CribbleView
}

static func storyboardController() -> CribbleController? {
return UIStoryboard(name: "Cribble", bundle: NSBundle.frameworkBundle).instantiateViewControllerWithIdentifier(String(self)) as? CribbleController
}

override func viewDidLoad() {
super.viewDidLoad()

optionsButton.layer.masksToBounds = false
optionsButton.layer.shadowRadius = 5
optionsButton.layer.shadowOpacity = 0.2
optionsButton.layer.shadowOffset = CGSizeMake(0, 5)

setupOptions(CribbleOptions.defaultOptions())
setup(options: options ?? CribbleOptions.defaultOptions())
}

override func viewDidAppear(animated: Bool) {
Expand All @@ -55,14 +55,24 @@ class CribbleController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let optionsController = segue.destinationViewController as? CribbleOptionsController else { return }

optionsController.options = options
optionsController.onOptionsChanged = { [weak self] options in
self?.setupOptions(options)

self?.options = options
self?.setup(options: options)
}
}

func setupOptions(options: CribbleOptions) {
// MARK: - Setup -

func setup(options options: CribbleOptions) {

cribbleView?.options = options
optionsButton.backgroundColor = options.color

optionsButton.layer.masksToBounds = false
optionsButton.layer.shadowRadius = 5
optionsButton.layer.shadowOpacity = 0.2
optionsButton.layer.shadowOffset = CGSizeMake(0, 5)
optionsButton.backgroundColor = options.cribbleColor.color
}
}
32 changes: 24 additions & 8 deletions Sources/CribbleOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import UIKit

enum CribbleColor {
public enum CribbleColor {

case red
case orange
case green
case blue
case purple
case custom(UIColor)

var color: UIColor {
switch self {
Expand All @@ -40,6 +41,8 @@ enum CribbleColor {
return UIColor(red: 97.0 / 255.0, green: 182.0 / 255.0, blue: 1, alpha: 1)
case .purple:
return UIColor(red: 118.0 / 255.0, green: 113.0 / 255.0, blue: 244.0 / 255.0, alpha: 1)
case .custom(let color):
return color
}
}

Expand All @@ -55,6 +58,8 @@ enum CribbleColor {
return "Blue"
case .purple:
return "Purple"
case .custom:
return "Custom"
}
}
}
Expand All @@ -73,16 +78,27 @@ enum CribbleImage {

public struct CribbleOptions {

let horizontalStep: CGFloat
let verticalStep: CGFloat
let opacity: CGFloat
let color: UIColor

static func colors() -> [CribbleColor] {
public let horizontalStep: CGFloat
public let verticalStep: CGFloat
public let opacity: CGFloat
public let cribbleColor: CribbleColor
static func defaultColors() -> [CribbleColor] {
return [.red, .orange, .green, .blue, .purple]
}

static func defaultOptions() -> CribbleOptions {
return CribbleOptions(horizontalStep: 8, verticalStep: 8, opacity: 0.5, color: CribbleColor.red.color)
return CribbleOptions(horizontalStep: 8, verticalStep: 8, opacity: 0.5, cribbleColor: CribbleColor.red)
}
}

extension CribbleOptions {

public init(horizontalStep: CGFloat, verticalStep: CGFloat, opacity: CGFloat, color: UIColor) {

self.horizontalStep = horizontalStep
self.verticalStep = verticalStep
self.opacity = opacity
self.cribbleColor = .custom(color)
}
}
86 changes: 56 additions & 30 deletions Sources/CribbleOptionsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ class CribbleOptionsController: UIViewController {
@IBOutlet weak var separatorView1HeightConstraint: NSLayoutConstraint!
@IBOutlet weak var separatorView2HeightConstraint: NSLayoutConstraint!

var colors = CribbleOptions.colors()
var colorIndex = 0
private var currentColorIndex = 0
private var colors = CribbleOptions.defaultColors()

var options: CribbleOptions? {
didSet {
currentColorIndex = colors.indexOf { $0.title == options?.cribbleColor.title } ?? 0
}
}
var onOptionsChanged: ((options: CribbleOptions) -> Void)?

override func prefersStatusBarHidden() -> Bool {
Expand All @@ -46,9 +52,46 @@ class CribbleOptionsController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let separatorHeight = 1 / UIScreen.mainScreen().scale

separatorView1HeightConstraint.constant = 1 / UIScreen.mainScreen().scale
separatorView2HeightConstraint.constant = 1 / UIScreen.mainScreen().scale
separatorView1HeightConstraint.constant = separatorHeight
separatorView2HeightConstraint.constant = separatorHeight

setupShadows()
setup(color: colors[currentColorIndex])
}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)

view.endEditing(true)
}

// MARK: - Setup -

func setup(color cribbleColor: CribbleColor) {

view.backgroundColor = cribbleColor.color
separatorView1.backgroundColor = cribbleColor.color
separatorView2.backgroundColor = cribbleColor.color
sizeLabel.textColor = cribbleColor.color
sizeTextField.textColor = cribbleColor.color
colorLabel.textColor = cribbleColor.color
colorValueLabel.textColor = cribbleColor.color
opacityLabel.textColor = cribbleColor.color
opacityValueLabel.textColor = cribbleColor.color
opacitySlider.minimumTrackTintColor = cribbleColor.color
opacitySlider.maximumTrackTintColor = cribbleColor.color
closeButton.tintColor = cribbleColor.color

sizeTextField.text = "\(Int(options?.horizontalStep ?? 8))"
colorValueLabel.text = cribbleColor.title
opacitySlider.value = Float(options?.opacity ?? 0.5)
opacityValueLabel.text = String(format: "%.1f", options?.opacity ?? opacitySlider.value)
}

func setupShadows() {

optionsView.layer.masksToBounds = false
optionsView.layer.shadowRadius = 5
Expand All @@ -60,47 +103,30 @@ class CribbleOptionsController: UIViewController {
closeButton.layer.shadowOpacity = 0.1
closeButton.layer.shadowOffset = CGSizeMake(0, 10)
closeButton.setImage(CribbleImage.close.image, forState: .Normal)

setupColors(colors[colorIndex].color)
}

func setupColors(color: UIColor) {

view.backgroundColor = color
separatorView1.backgroundColor = color
separatorView2.backgroundColor = color
sizeLabel.textColor = color
sizeTextField.textColor = color
colorLabel.textColor = color
colorValueLabel.textColor = color
opacityLabel.textColor = color
opacityValueLabel.textColor = color
opacitySlider.minimumTrackTintColor = color
opacitySlider.maximumTrackTintColor = color
closeButton.tintColor = color
}

// MARK: - IB Actions -

@IBAction func colorButtonClicked(sender: UIButton) {

colorIndex += 1
if colorIndex == colors.count {
colorIndex = 0
currentColorIndex += 1
if currentColorIndex == colors.count {
currentColorIndex = 0
}

let color = colors[colorIndex]
colorValueLabel.text = color.title
let color = colors[currentColorIndex]

UIView.animateWithDuration(0.4) {
self.setupColors(color.color)
self.setup(color: color)
}
}

@IBAction func closeButtonClicked(sender: UIButton) {

let step = Float(sizeTextField.text ?? "") ?? 8
let opacity = opacitySlider.value
let options = CribbleOptions(horizontalStep: CGFloat(step), verticalStep: CGFloat(step), opacity: CGFloat(opacity), color: colors[colorIndex].color)

let options = CribbleOptions(horizontalStep: CGFloat(step), verticalStep: CGFloat(step), opacity: CGFloat(opacity), cribbleColor: colors[currentColorIndex])

onOptionsChanged?(options: options)

Expand Down
2 changes: 1 addition & 1 deletion Sources/CribbleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CribbleView: UIView {
let context = UIGraphicsGetCurrentContext()
CGContextSetShouldAntialias(context, false)
CGContextSetLineWidth(context, lineWidth)
CGContextSetStrokeColorWithColor(context, options.color.colorWithAlphaComponent(options.opacity).CGColor)
CGContextSetStrokeColorWithColor(context, options.cribbleColor.color.colorWithAlphaComponent(options.opacity).CGColor)

let columnWidth: CGFloat = options.horizontalStep
let rowHeight: CGFloat = options.verticalStep
Expand Down

0 comments on commit 60a47ac

Please sign in to comment.