Last active
January 11, 2019 17:53
-
-
Save Valentin-Kalchev/e9cfe1a94768c1d2e9ee to your computer and use it in GitHub Desktop.
Swift Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Extensions.swift | |
| // | |
| // Created by Valentin Kalchev on 17/11/2015. | |
| // Copyright © 2015 Valentin Kalchev. All rights reserved. | |
| // | |
| import Foundation | |
| extension Array { | |
| // Shuffle the order of an array's elements. | |
| mutating func shuffle() { | |
| for _ in 0..<self.count { | |
| sortInPlace { | |
| (_,_) in arc4random() < arc4random() | |
| } | |
| } | |
| } | |
| func sum() -> Int { | |
| var sum = 0 | |
| for (_, obj) in self.enumerate() { | |
| sum += obj as! Int | |
| } | |
| return sum | |
| } | |
| } | |
| extension UIColor { | |
| // Init UIColor with hex value and alpha | |
| convenience init(hex: Int, alpha: CGFloat = 1.0) { | |
| let r = CGFloat((hex & 0xFF0000) >> 16) / 255.0 | |
| let g = CGFloat((hex & 0x00FF00) >> 08) / 255.0 | |
| let b = CGFloat((hex & 0x0000FF) >> 00) / 255.0 | |
| self.init(red:r, green:g, blue:b, alpha:alpha) | |
| } | |
| } | |
| // Zoom to point on UIScrollView | |
| extension UIScrollView { | |
| func zoomToPoint(var zoomPoint: CGPoint, withScale scale: CGFloat, animated: Bool) { | |
| var contentSize = CGSize() | |
| contentSize.width = (self.contentSize.width / self.zoomScale) | |
| contentSize.height = (self.contentSize.height / self.zoomScale) | |
| print("Content size: \(contentSize)") | |
| //translate the zoom point to relative to the content rect | |
| zoomPoint.x = (zoomPoint.x / self.bounds.size.width) * contentSize.width | |
| zoomPoint.y = (zoomPoint.y / self.bounds.size.height) * contentSize.height | |
| //derive the size of the region to zoom to | |
| var zoomSize = CGSize() | |
| zoomSize.width = self.bounds.size.width / scale | |
| zoomSize.height = self.bounds.size.height / scale | |
| //offset the zoom rect so the actual zoom point is in the middle of the rectangle | |
| var zoomRect = CGRect() | |
| zoomRect.origin.x = zoomPoint.x - zoomSize.width / 2.0 | |
| zoomRect.origin.y = zoomPoint.y - zoomSize.height / 2.0 | |
| zoomRect.size.width = zoomSize.width | |
| zoomRect.size.height = zoomSize.height | |
| self.zoomToRect(zoomRect, animated: animated) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where sortInPlace defined?