Skip to content

Commit

Permalink
ScaleToFill factor fixes & added childContainerView #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mozharovsky committed Nov 27, 2016
1 parent a877d8f commit b8070fc
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 73 deletions.
Binary file modified .DS_Store
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class CropViewController: UIViewController, FloatingViewLayout, Cropable {
// MARK: Cropable properties

var cropView = UIScrollView()
var childContainerView = UIView()
var childView = UIImageView()
var linesView = LinesView()

Expand Down
Binary file modified Source/.DS_Store
Binary file not shown.
39 changes: 39 additions & 0 deletions Source/CGAffineTransform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// CGAffineTransform.swift
// Pods
//
// Created by mac on 27/11/2016.
//
//

import UIKit

public extension CGAffineTransform {
public static func scalingFactor(toFill containerSize: CGSize, with contentSize: CGSize, atAngle angle: Double) -> Double {
var theta = fabs(angle - 2 * .pi * trunc(angle / .pi / 2) - .pi)

if theta > .pi / 2 {
theta = fabs(.pi - theta)
}

let h = Double(contentSize.height)
let H = Double(containerSize.height)
let w = Double(contentSize.width)
let W = Double(containerSize.width)

let scale1 = (H * cos(theta) + W * sin(theta)) / min(H, h)
let scale2 = (H * sin(theta) + W * cos(theta)) / min(W, w)

let scalingFactor = max(scale1, scale2)

return scalingFactor
}

func scaling(toFill containerSize: CGSize, with contentSize: CGSize, atAngle angle: Double) -> CGAffineTransform {
let factor = CGFloat(CGAffineTransform.scalingFactor(toFill: containerSize,
with: contentSize,
atAngle: angle))

return self.scaledBy(x: factor, y: factor)
}
}
36 changes: 23 additions & 13 deletions Source/Cropable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public protocol Cropable {
/// A cropable area containing the content.
var cropView: UIScrollView { get set }

/// A view containing the child. It is required to simplify
/// any transformations on the child.
var childContainerView: UIView { get set }

/// A cropable content view.
var childView: ChildView { get set }

Expand Down Expand Up @@ -125,7 +129,8 @@ public extension Cropable where ChildView == UIImageView {
cropView.showsVerticalScrollIndicator = false
cropView.contentSize = view.bounds.size

cropView.addSubview(childView)
childContainerView.addSubview(childView)
cropView.addSubview(childContainerView)
}

///
Expand All @@ -150,10 +155,14 @@ public extension Cropable where ChildView == UIImageView {
childView.image = image

if adjustingContent {
childView.sizeToFit()
childView.bounds.size = image.size
childContainerView.bounds.size = image.size

childContainerView.frame.origin = .zero
childView.frame.origin = .zero

cropView.contentOffset = .zero
cropView.contentSize = childView.image!.size
cropView.contentSize = image.size

updateContent()
highlightArea(false, animated: false)
Expand Down Expand Up @@ -185,8 +194,9 @@ public extension Cropable {
/// Updates the current cropable content area, zoom and scale.
///
func updateContent() {
let childViewSize = childView.bounds.size
let childViewSize = childContainerView.bounds.size
let scrollViewSize = cropView.bounds.size

let widthScale = scrollViewSize.width / childViewSize.width
let heightScale = scrollViewSize.height / childViewSize.height

Expand Down Expand Up @@ -215,16 +225,16 @@ public extension Cropable {
func centerContent(forcing: Bool = false) {
var (left, top): (CGFloat, CGFloat) = (0, 0)

if cropView.contentSize.width < cropView.bounds.width {
left = (cropView.bounds.width - cropView.contentSize.width) / 2
if cropView.contentSize.width < cropView.frame.width {
left = (cropView.frame.width - cropView.contentSize.width) / 2
} else if forcing {
cropView.contentOffset.x = abs(cropView.bounds.width - cropView.contentSize.width) / 2
cropView.contentOffset.x = abs(cropView.frame.width - cropView.contentSize.width) / 2
}

if cropView.contentSize.height < cropView.bounds.height {
top = (cropView.bounds.height - cropView.contentSize.height) / 2
if cropView.contentSize.height < cropView.frame.height {
top = (cropView.frame.height - cropView.contentSize.height) / 2
} else if forcing {
cropView.contentOffset.y = abs(cropView.bounds.height - cropView.contentSize.height) / 2
cropView.contentOffset.y = abs(cropView.frame.height - cropView.contentSize.height) / 2
}

cropView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
Expand Down Expand Up @@ -302,12 +312,12 @@ public extension Cropable {
}

linesView.frame.size = CGSize(
width: min(cropView.frame.width, childView.frame.width),
height: min(cropView.frame.height, childView.frame.height)
width: min(cropView.frame.width, childContainerView.frame.width),
height: min(cropView.frame.height, childContainerView.frame.height)
)

let visibleRect = CGRect(origin: cropView.contentOffset, size: cropView.bounds.size)
let intersection = visibleRect.intersection(childView.frame)
let intersection = visibleRect.intersection(childContainerView.frame)
linesView.frame = intersection
}
}
2 changes: 1 addition & 1 deletion Source/CropableScrollViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ open class CropableScrollViewDelegate<T: Cropable>: NSObject, UIScrollViewDelega
}

open func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return cropable.childView
return cropable.childContainerView
}
}
21 changes: 21 additions & 0 deletions Source/Radians.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Radians.swift
// Pods
//
// Created by mac on 27/11/2016.
//
//

import Foundation

public extension Double {
public func toRadians() -> Double {
return self * .pi / 180.0
}
}

public extension Float {
public func toRadians() -> Float {
return self * .pi / 180
}
}

0 comments on commit b8070fc

Please sign in to comment.