-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.swift
executable file
·75 lines (60 loc) · 2.61 KB
/
Utilities.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// ALUtilities.swift
// ALCameraViewController
//
// Created by Alex Littlejohn on 2015/06/25.
// Copyright (c) 2015 zero. All rights reserved.
//
import UIKit
internal func radians(degrees: Double) -> Double {
return degrees / 180 * M_PI
}
internal func SpringAnimation(animations: () -> Void) {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.BeginFromCurrentState], animations: {
animations()
}, completion: nil)
}
internal func LocalizedString(key: String) -> String {
return NSLocalizedString(key, tableName: CameraGlobals.shared.stringsTable, bundle: CameraGlobals.shared.bundle, comment: key)
}
internal func currentRotation() -> Double {
var rotation: Double = 0
if UIDevice.currentDevice().orientation == .LandscapeLeft {
rotation = 90
} else if UIDevice.currentDevice().orientation == .LandscapeRight {
rotation = 270
} else if UIDevice.currentDevice().orientation == .PortraitUpsideDown {
rotation = 180
}
return rotation
}
internal func largestPhotoSize() -> CGSize {
let scale = UIScreen.mainScreen().scale
let screenSize = UIScreen.mainScreen().bounds.size
let size = CGSize(width: screenSize.width * scale, height: screenSize.height * scale)
return size
}
internal func errorWithKey(key: String, domain: String) -> NSError {
let errorString = LocalizedString(key)
let errorInfo = [NSLocalizedDescriptionKey: errorString]
let error = NSError(domain: domain, code: 0, userInfo: errorInfo)
return error
}
internal func normalizedRect(rect: CGRect, orientation: UIImageOrientation) -> CGRect {
let normalizedX = rect.origin.x
let normalizedY = rect.origin.y
let normalizedWidth = rect.width
let normalizedHeight = rect.height
var normalizedRect: CGRect
switch orientation {
case .Up, .UpMirrored:
normalizedRect = CGRect(x: normalizedX, y: normalizedY, width: normalizedWidth, height: normalizedHeight)
case .Down, .DownMirrored:
normalizedRect = CGRect(x: 1-normalizedX-normalizedWidth, y: 1-normalizedY-normalizedHeight, width: normalizedWidth, height: normalizedHeight)
case .Left, .LeftMirrored:
normalizedRect = CGRect(x: 1-normalizedY-normalizedHeight, y: normalizedX, width: normalizedHeight, height: normalizedWidth)
case .Right, .RightMirrored:
normalizedRect = CGRect(x: normalizedY, y: 1-normalizedX-normalizedWidth, width: normalizedHeight, height: normalizedWidth)
}
return normalizedRect
}