Skip to content

Commit

Permalink
Enable showing guidelines view constantly
Browse files Browse the repository at this point in the history
  • Loading branch information
mozharovsky committed Jul 20, 2016
1 parent 6d72192 commit d88f46b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
51 changes: 33 additions & 18 deletions Source/Cropable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ protocol Cropable {
var linesView: LinesView { get set }

/// Top offset for cropable content. If your `cropView`
/// is constrained with `UINavigationBar` or anything on
/// is constrained with `UINavigationBar` or anything on
/// the top, set this offset so the content can be properly
/// centered and scaled.
///
/// Default value is `0.0`.
var topOffset: CGFloat { get }

/// Determines whether the guidelines' grid should be
/// constantly showing on the cropping view.
/// Default value is `false`.
var alwaysShowGuidelines: Bool { get }

///
/// Adds a cropable view with its content to the provided
/// container view.
Expand Down Expand Up @@ -68,7 +73,7 @@ protocol Cropable {
/// **ATTENTION**, default implementation
/// is a placeholder!
///
func willEndZooming()
func willEndZooming()

///
/// Handles zoom gestures.
Expand All @@ -90,7 +95,7 @@ protocol Cropable {
func highlightArea(highlight: Bool, animated: Bool)
}

// MARK: - Default implementations for UIImageView childs
// MARK: - Default implementations for UIImageView childs

extension Cropable where ChildView == UIImageView {
///
Expand Down Expand Up @@ -125,7 +130,7 @@ extension Cropable where ChildView == UIImageView {
/// container view.
///
/// - parameter view: A container view.
/// - parameter image: An image to use.
/// - parameter image: An image to use.
///
func addCropable(to view: UIView, with image: UIImage) {
addCropable(to: view)
Expand All @@ -135,19 +140,24 @@ extension Cropable where ChildView == UIImageView {
///
/// Adds an image to the UIImageView child view.
///
/// - parameter image: An image to use.
/// - parameter image: An image to use.
/// - parameter adjustingContent: Indicates whether the content should be adjusted or not. Default value is `true`.
///
func addImage(image: UIImage) {
func addImage(image: UIImage, adjustingContent: Bool = true) {
childView.image = image
childView.sizeToFit()
childView.frame.origin = .zero
cropView.contentSize = childView.image!.size
updateContent()
highlightArea(false, animated: false)

if adjustingContent {
childView.sizeToFit()
childView.frame.origin = .zero
cropView.contentSize = childView.image!.size

updateContent()
highlightArea(false, animated: false)
}
}
}

// MARK: - Default implementations
// MARK: - Default implementations

extension Cropable {
/// Top offset for cropable content. If your `cropView`
Expand All @@ -160,6 +170,13 @@ extension Cropable {
return 0
}

/// Determines whether the guidelines' grid should be
/// constantly showing on the cropping view.
/// Default value is `false`.
var alwaysShowGuidelines: Bool {
return false
}

///
/// Updated the current cropable content area, zoom and scale.
///
Expand All @@ -180,7 +197,7 @@ extension Cropable {

centerContent()

highlightArea(false, animated: false)
highlightArea(alwaysShowGuidelines, animated: false)
}

///
Expand All @@ -190,7 +207,6 @@ extension Cropable {
let boundsSize = cropView.bounds.size
let contentFrame = childView.frame
var origin = contentFrame.origin
var size = contentFrame.size

if contentFrame.size.width < boundsSize.width {
origin.x = (boundsSize.width - contentFrame.width) / 2
Expand All @@ -204,12 +220,9 @@ extension Cropable {
origin.y = 0
}



origin.y -= topOffset
cropView.contentInset.bottom = -topOffset
childView.frame.origin = origin
childView.frame.size = size
}

///
Expand Down Expand Up @@ -244,6 +257,7 @@ extension Cropable {
/// Handles the end of zooming.
///
func didEndZooming() {
guard !alwaysShowGuidelines else { return }
highlightArea(false)
}

Expand Down Expand Up @@ -279,7 +293,7 @@ extension Cropable {
} else {
linesView.alpha = highlight ? 1 : 0
}

}

linesView.frame.size = CGSize(
Expand All @@ -292,3 +306,4 @@ extension Cropable {
linesView.frame = intersection
}
}

13 changes: 11 additions & 2 deletions Source/CropableScrollViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ final class CropableScrollViewDelegate<T: Cropable where T: AnyObject>: NSObject
}

func scrollViewDidScroll(scrollView: UIScrollView) {
if cropable.alwaysShowGuidelines {
cropable.highlightArea(true)
}

guard panning else {
return
}
Expand All @@ -47,11 +51,16 @@ final class CropableScrollViewDelegate<T: Cropable where T: AnyObject>: NSObject

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
panning = false
cropable.highlightArea(false, animated: !decelerate)

if cropable.alwaysShowGuidelines {
cropable.highlightArea(true, animated: false)
} else {
cropable.highlightArea(false, animated: !decelerate)
}
}

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

}

func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
Expand Down

0 comments on commit d88f46b

Please sign in to comment.