Update models
This commit is contained in:
61
CapCollector/Extensions/UIAlertControllerExtensions.swift
Normal file
61
CapCollector/Extensions/UIAlertControllerExtensions.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// UIAlertControllerExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 23.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIAlertController {
|
||||
|
||||
private struct AssociatedKeys {
|
||||
static var blurStyleKey = "UIAlertController.blurStyleKey"
|
||||
}
|
||||
|
||||
public var blurStyle: UIBlurEffect.Style {
|
||||
get {
|
||||
return objc_getAssociatedObject(self, &AssociatedKeys.blurStyleKey) as? UIBlurEffect.Style ?? .extraLight
|
||||
} set (style) {
|
||||
objc_setAssociatedObject(self, &AssociatedKeys.blurStyleKey, style, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
|
||||
view.setNeedsLayout()
|
||||
view.layoutIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
public var cancelButtonColor: UIColor? {
|
||||
return blurStyle == .dark ? UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0) : nil
|
||||
}
|
||||
|
||||
private var visualEffectView: UIVisualEffectView? {
|
||||
if let presentationController = presentationController, presentationController.responds(to: Selector(("popoverView"))), let view = presentationController.value(forKey: "popoverView") as? UIView // We're on an iPad and visual effect view is in a different place.
|
||||
{
|
||||
return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
|
||||
}
|
||||
|
||||
return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
|
||||
}
|
||||
|
||||
private var cancelActionView: UIView? {
|
||||
return view.recursiveSubviews.compactMap({
|
||||
$0 as? UILabel}
|
||||
).first(where: {
|
||||
$0.text == actions.first(where: { $0.style == .cancel })?.title
|
||||
})?.superview?.superview
|
||||
}
|
||||
|
||||
public convenience init(title: String?, message: String?, preferredStyle: UIAlertController.Style, blurStyle: UIBlurEffect.Style) {
|
||||
self.init(title: title, message: message, preferredStyle: preferredStyle)
|
||||
self.blurStyle = blurStyle
|
||||
}
|
||||
|
||||
open override func viewWillLayoutSubviews() {
|
||||
super.viewWillLayoutSubviews()
|
||||
|
||||
visualEffectView?.effect = UIBlurEffect(style: blurStyle)
|
||||
cancelActionView?.backgroundColor = cancelButtonColor
|
||||
}
|
||||
}
|
82
CapCollector/Extensions/UIImageExtensions.swift
Normal file
82
CapCollector/Extensions/UIImageExtensions.swift
Normal file
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// UIImageExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 13.02.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIImage {
|
||||
|
||||
static func templateImage(named: String) -> UIImage {
|
||||
return UIImage(named: named)!.withRenderingMode(.alwaysTemplate)
|
||||
}
|
||||
|
||||
/**
|
||||
Resize an image to the target size
|
||||
*/
|
||||
func resize(to targetSize: CGSize) -> UIImage {
|
||||
let rect = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
|
||||
self.draw(in: rect)
|
||||
let newImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
return newImage!
|
||||
}
|
||||
|
||||
/**
|
||||
Crop an image to a square, centered around the middle of the frame
|
||||
- parameter factor: The crop factor of the resulting image
|
||||
- returns: The cropped image
|
||||
*/
|
||||
func crop(factor: CGFloat) -> UIImage {
|
||||
let width = self.size.width * factor
|
||||
return crop(to: width)
|
||||
}
|
||||
|
||||
/**
|
||||
Crop an image to a square, centered around the middle of the frame
|
||||
- parameter size: The height and width of the resulting image
|
||||
- returns: The cropped image
|
||||
*/
|
||||
func crop(to size: CGFloat) -> UIImage {
|
||||
var rect = CGRect(
|
||||
x: (self.size.height - size) / 2,
|
||||
y: (self.size.width - size) / 2,
|
||||
width: size,
|
||||
height: size)
|
||||
rect.origin.x *= scale
|
||||
rect.origin.y *= scale
|
||||
rect.size.width *= scale
|
||||
rect.size.height *= scale
|
||||
|
||||
let imageRef = cgImage!.cropping(to: rect)
|
||||
return UIImage(cgImage: imageRef!, scale: scale, orientation: imageOrientation)
|
||||
}
|
||||
|
||||
/// The (circular) image masked by a circle
|
||||
var circleMasked: UIImage? {
|
||||
let width = size.width
|
||||
let height = size.height
|
||||
let isPortrait = height > width
|
||||
let breadth = min(width, height)
|
||||
let breadthSize = CGSize(width: breadth, height: breadth)
|
||||
let breadthRect = CGRect(origin: .zero, size: breadthSize)
|
||||
UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
|
||||
defer { UIGraphicsEndImageContext() }
|
||||
|
||||
let x = isPortrait ? 0 : floor((width - height) / 2)
|
||||
let y = isPortrait ? floor((height - width) / 2) : 0
|
||||
let rect = CGRect(origin: CGPoint( x: x, y: y), size: breadthSize)
|
||||
guard let cgImage = cgImage?.cropping(to: rect) else {
|
||||
return nil
|
||||
}
|
||||
UIBezierPath(ovalIn: breadthRect).addClip()
|
||||
UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation).draw(in: breadthRect)
|
||||
return UIGraphicsGetImageFromCurrentImageContext()
|
||||
}
|
||||
}
|
19
CapCollector/Extensions/UIViewExtensions.swift
Normal file
19
CapCollector/Extensions/UIViewExtensions.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// UIViewExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 23.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIView {
|
||||
|
||||
var recursiveSubviews: [UIView] {
|
||||
var subviews = self.subviews.compactMap{ $0 }
|
||||
subviews.forEach { subviews.append(contentsOf: $0.recursiveSubviews) }
|
||||
return subviews
|
||||
}
|
||||
}
|
28
CapCollector/Extensions/ViewControllerExtensions.swift
Normal file
28
CapCollector/Extensions/ViewControllerExtensions.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// ViewControllerExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 18.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIViewController {
|
||||
|
||||
// MARK: Alerts
|
||||
|
||||
/// Present an alert with a message to the user
|
||||
func showAlert(_ message: String, title: String = "Error") {
|
||||
let alertController = UIAlertController(
|
||||
title: title,
|
||||
message: message,
|
||||
preferredStyle: .alert,
|
||||
blurStyle: .dark)
|
||||
|
||||
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
|
||||
|
||||
self.present(alertController, animated: true, completion: nil)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user