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

Commit

Permalink
Fixed rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
ikorich committed Feb 12, 2017
1 parent b172103 commit f2cf53c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion IGRPhotoTweaks.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'IGRPhotoTweaks'
spec.version = '1.0.2'
spec.version = '1.0.3'
spec.platform = :ios, '9.0'

spec.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
2 changes: 1 addition & 1 deletion IGRPhotoTweaks/IGRPhotoTweakConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let kMinimumCropArea: CGFloat = 60

let kMaximumCanvasWidthRatio: CGFloat = 0.9
let kMaximumCanvasHeightRatio: CGFloat = 0.7
let kCanvasHeaderHeigth: CGFloat = 60
let kCanvasHeaderHeigth: CGFloat = 100

let kCropViewLineWidth: CGFloat = 1

Expand Down
59 changes: 45 additions & 14 deletions IGRPhotoTweaks/IGRPhotoTweakView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import UIKit

func isHighlightMask() -> Bool
func highlightMaskAlphaValue() -> CGFloat

func canvasHeaderHeigth() -> CGFloat
}

@objc public class IGRPhotoTweakView: UIView {
Expand Down Expand Up @@ -50,7 +52,7 @@ import UIKit
fileprivate var originalSize = CGSize.zero

fileprivate var manualZoomed = false

// masks
fileprivate var topMask: UIView!
fileprivate var leftMask: UIView!
Expand All @@ -64,23 +66,13 @@ import UIKit

init(frame: CGRect, image: UIImage, customizationDelegate: IGRPhotoTweakViewCustomizationDelegate!) {
super.init(frame: frame)

self.image = image

self.customizationDelegate = customizationDelegate

// scale the image
self.maximumCanvasSize = CGSize(width: (kMaximumCanvasWidthRatio * self.frame.size.width),
height: (kMaximumCanvasHeightRatio * self.frame.size.height - kCanvasHeaderHeigth))
let scaleX: CGFloat = image.size.width / self.maximumCanvasSize.width
let scaleY: CGFloat = image.size.height / self.maximumCanvasSize.height
let scale: CGFloat = max(scaleX, scaleY)
let bounds = CGRect(x: 0.0,
y: 0.0,
width: (image.size.width / scale),
height: (image.size.height / scale))
let bounds = self.maxBounds()
self.originalSize = bounds.size
self.centerY = self.maximumCanvasSize.height / 2.0 + kCanvasHeaderHeigth

self.scrollView = IGRPhotoScrollView(frame: bounds)
self.scrollView.center = CGPoint(x: (self.frame.width / 2.0), y: self.centerY)
Expand All @@ -98,7 +90,7 @@ import UIKit
height: self.scrollView.bounds.size.height)
self.scrollView.updateDelegate = self
self.addSubview(self.scrollView)

self.photoContentView = IGRPhotoContentView(frame: self.scrollView.bounds)
self.photoContentView.image = image
self.photoContentView.isUserInteractionEnabled = true
Expand Down Expand Up @@ -131,6 +123,37 @@ import UIKit
self.originalPoint = self.convert(self.scrollView.center, to: self)
}

public override func layoutSubviews() {
super.layoutSubviews()

self.originalSize = self.maxBounds().size
self.scrollView.center = CGPoint(x: (self.frame.width / 2.0), y: self.centerY)

self.cropView.center = self.scrollView.center
self.checkScrollViewContentOffset();

self.cropViewDidStopCrop(self.cropView)
}

func maxBounds() -> CGRect {
// scale the image
self.maximumCanvasSize = CGSize(width: (kMaximumCanvasWidthRatio * self.frame.size.width),
height: (kMaximumCanvasHeightRatio * self.frame.size.height - self.canvasHeaderHeigth()))

self.centerY = self.maximumCanvasSize.height / 2.0 + self.canvasHeaderHeigth()

let scaleX: CGFloat = self.image.size.width / self.maximumCanvasSize.width
let scaleY: CGFloat = self.image.size.height / self.maximumCanvasSize.height
let scale: CGFloat = max(scaleX, scaleY)

let bounds = CGRect(x: 0.0,
y: 0.0,
width: (self.image.size.width / scale),
height: (self.image.size.height / scale))

return bounds
}

func borderColor() -> UIColor {
return (self.customizationDelegate?.borderColor())!
}
Expand All @@ -155,6 +178,10 @@ import UIKit
return (self.customizationDelegate?.highlightMaskAlphaValue())!
}

func canvasHeaderHeigth() -> CGFloat {
return (self.customizationDelegate?.canvasHeaderHeigth())!
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down Expand Up @@ -240,6 +267,10 @@ import UIKit
self.angle = value
self.scrollView.transform = CGAffineTransform(rotationAngle: self.angle)

self.updatePosition()
}

fileprivate func updatePosition() {
// position scroll view
let width: CGFloat = fabs(cos(self.angle)) * self.cropView.frame.size.width + fabs(sin(self.angle)) * self.cropView.frame.size.height
let height: CGFloat = fabs(sin(self.angle)) * self.cropView.frame.size.width + fabs(cos(self.angle)) * self.cropView.frame.size.height
Expand Down
16 changes: 15 additions & 1 deletion IGRPhotoTweaks/IGRPhotoTweakViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import Photos
override open func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.isNavigationBarHidden = true
self.automaticallyAdjustsScrollViewInsets = false
self.view.clipsToBounds = true

Expand All @@ -69,6 +68,12 @@ import Photos
self.view.sendSubview(toBack: self.photoView)
}

override open func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

photoView.layoutSubviews()
}

// MARK: - Public

open func changedAngel(value: CGFloat) {
Expand All @@ -81,6 +86,7 @@ import Photos

open func resetView() {
self.photoView.resetView()
self.stopChangeAngel()
}

open func dismissAction() {
Expand Down Expand Up @@ -259,6 +265,10 @@ import Photos
open func customHighlightMaskAlphaValue() -> CGFloat {
return 0.3
}

open func customCanvasHeaderHeigth() -> CGFloat {
return kCanvasHeaderHeigth
}
}

extension IGRPhotoTweakViewController : IGRPhotoTweakViewCustomizationDelegate {
Expand All @@ -285,4 +295,8 @@ extension IGRPhotoTweakViewController : IGRPhotoTweakViewCustomizationDelegate {
public func highlightMaskAlphaValue() -> CGFloat {
return self.customHighlightMaskAlphaValue()
}

public func canvasHeaderHeigth() -> CGFloat {
return self.customCanvasHeaderHeigth()
}
}

0 comments on commit f2cf53c

Please sign in to comment.