Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Updated to swift 3.0 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 57 additions & 58 deletions Classes/WDCropBorderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,60 @@
import UIKit

internal class WDCropBorderView: UIView {
private let kNumberOfBorderHandles: CGFloat = 8
private let kHandleDiameter: CGFloat = 24

override init(frame: CGRect) {
super.init(frame: frame)

self.backgroundColor = UIColor.clearColor()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.backgroundColor = UIColor.clearColor()
}

override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()

CGContextSetStrokeColorWithColor(context,
UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).CGColor)
CGContextSetLineWidth(context, 1.5)
CGContextAddRect(context, CGRectMake(kHandleDiameter / 2, kHandleDiameter / 2,
rect.size.width - kHandleDiameter, rect.size.height - kHandleDiameter))
CGContextStrokePath(context)

CGContextSetRGBFillColor(context, 1, 1, 1, 0.95)
for handleRect in calculateAllNeededHandleRects() {
CGContextFillEllipseInRect(context, handleRect)
}
}

private func calculateAllNeededHandleRects() -> [CGRect] {

let width = self.frame.width
let height = self.frame.height

let leftColX: CGFloat = 0
let rightColX = width - kHandleDiameter
let centerColX = rightColX / 2

let topRowY: CGFloat = 0
let bottomRowY = height - kHandleDiameter
let middleRowY = bottomRowY / 2

//starting with the upper left corner and then following clockwise
let topLeft = CGRectMake(leftColX, topRowY, kHandleDiameter, kHandleDiameter)
let topCenter = CGRectMake(centerColX, topRowY, kHandleDiameter, kHandleDiameter)
let topRight = CGRectMake(rightColX, topRowY, kHandleDiameter, kHandleDiameter)
let middleRight = CGRectMake(rightColX, middleRowY, kHandleDiameter, kHandleDiameter)
let bottomRight = CGRectMake(rightColX, bottomRowY, kHandleDiameter, kHandleDiameter)
let bottomCenter = CGRectMake(centerColX, bottomRowY, kHandleDiameter, kHandleDiameter)
let bottomLeft = CGRectMake(leftColX, bottomRowY, kHandleDiameter, kHandleDiameter)
let middleLeft = CGRectMake(leftColX, middleRowY, kHandleDiameter, kHandleDiameter)

return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft,
middleLeft]
}
}
private let kNumberOfBorderHandles: CGFloat = 8
private let kHandleDiameter: CGFloat = 24

override init(frame: CGRect) {
super.init(frame: frame)

self.backgroundColor = UIColor.clear
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.backgroundColor = UIColor.clear
}

override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
context.setStrokeColor(UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).cgColor)
context.setLineWidth(1.5)
context.addRect(CGRect(x: kHandleDiameter / 2, y: kHandleDiameter / 2,
width: rect.size.width - kHandleDiameter, height: rect.size.height - kHandleDiameter))
context.strokePath()

context.setFillColor(red: 1, green: 1, blue: 1, alpha: 0.95)
for handleRect in calculateAllNeededHandleRects() {
context.fillEllipse(in: handleRect)
}
}
}

private func calculateAllNeededHandleRects() -> [CGRect] {

let width = self.frame.width
let height = self.frame.height

let leftColX: CGFloat = 0
let rightColX = width - kHandleDiameter
let centerColX = rightColX / 2

let topRowY: CGFloat = 0
let bottomRowY = height - kHandleDiameter
let middleRowY = bottomRowY / 2

//starting with the upper left corner and then following clockwise
let topLeft = CGRect(x: leftColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)
let topCenter = CGRect(x: centerColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)
let topRight = CGRect(x: rightColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)
let middleRight = CGRect(x: rightColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter)
let bottomRight = CGRect(x: rightColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)
let bottomCenter = CGRect(x: centerColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)
let bottomLeft = CGRect(x: leftColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)
let middleLeft = CGRect(x: leftColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter)

return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft,
middleLeft]
}
}
82 changes: 41 additions & 41 deletions Classes/WDImageCropOverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@
import UIKit

internal class WDImageCropOverlayView: UIView {

var cropSize: CGSize!
var toolbar: UIToolbar!

override init(frame: CGRect) {
super.init(frame: frame)

self.backgroundColor = UIColor.clearColor()
self.userInteractionEnabled = true
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.backgroundColor = UIColor.clearColor()
self.userInteractionEnabled = true
}

override func drawRect(rect: CGRect) {

let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54)

let width = CGRectGetWidth(self.frame)
let height = CGRectGetHeight(self.frame) - toolbarSize

let heightSpan = floor(height / 2 - self.cropSize.height / 2)
let widthSpan = floor(width / 2 - self.cropSize.width / 2)

// fill outer rect
UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).set()
UIRectFill(self.bounds)

// fill inner border
UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).set()
UIRectFrame(CGRectMake(widthSpan - 2, heightSpan - 2, self.cropSize.width + 4,
self.cropSize.height + 4))

// fill inner rect
UIColor.clearColor().set()
UIRectFill(CGRectMake(widthSpan, heightSpan, self.cropSize.width, self.cropSize.height))
}
var cropSize: CGSize!
var toolbar: UIToolbar!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.clear
self.isUserInteractionEnabled = true
}
override func draw(_ rect: CGRect) {
let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54)
let width = self.frame.width
let height = self.frame.height - toolbarSize
let heightSpan = floor(height / 2 - self.cropSize.height / 2)
let widthSpan = floor(width / 2 - self.cropSize.width / 2)
// fill outer rect
UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).set()
UIRectFill(self.bounds)
// fill inner border
UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).set()
UIRectFrame(CGRect(x: widthSpan - 2, y: heightSpan - 2, width: self.cropSize.width + 4,
height: self.cropSize.height + 4))
// fill inner rect
UIColor.clear.set()
UIRectFill(CGRect(x: widthSpan, y: heightSpan, width: self.cropSize.width, height: self.cropSize.height))
}
}
Loading