This file contains 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
public override func layoutSubviews() { | |
super.layoutSubviews() | |
// If the view is animating apply the animation to the sublayer | |
CATransaction.begin() | |
if let animation = layer.animationForKey("position") { | |
CATransaction.setAnimationDuration(animation.duration) | |
CATransaction.setAnimationTimingFunction(animation.timingFunction) | |
} else { | |
CATransaction.disableActions() |
This file contains 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
import WebKit | |
class NativeWebViewController: UIViewController { | |
let viewportScriptString = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); meta.setAttribute('initial-scale', '1.0'); meta.setAttribute('maximum-scale', '1.0'); meta.setAttribute('minimum-scale', '1.0'); meta.setAttribute('user-scalable', 'no'); document.getElementsByTagName('head')[0].appendChild(meta);" | |
let disableSelectionScriptString = "document.documentElement.style.webkitUserSelect='none';" | |
let disableCalloutScriptString = "document.documentElement.style.webkitTouchCallout='none';" | |
override func viewDidLoad() { | |
// 1 - Make user scripts for injection |
This file contains 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
/// Generate thumbnail with AVAssetImageGenerator | |
func generateThumbnailFromAsset(asset: AVAsset, forTime time: CMTime) -> UIImage { | |
let imageGenerator = AVAssetImageGenerator(asset: asset) | |
imageGenerator.appliesPreferredTrackTransform = true | |
var actualTime: CMTime = kCMTimeZero | |
do { | |
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: &actualTime) | |
let image = UIImage(CGImage: imageRef) | |
return image | |
} catch let error as NSError { |
This file contains 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
/** | |
Batch request an array of images for background download task. This method fires completion block when ALL images are downloaded and returns an array of UIImage objects. | |
- Parameter imageURLs: Array of strings representing location of images as URL's | |
- Parameter completion: Block that provides a dictionary of downloaded images as argument. The dictionary contains images at the index they were passed in the original array and NSNull objects for images that failed to download. | |
- Parameter imageScale: Scale to interpret image data for UIImage. A scale of 1.0 will keep full size of image. Default (imageScale = nil) is scale factor of UIScreen resolution. | |
*/ | |
@objc | |
static func batchImageDownloadWithImageUrlArray(imageURLs: [String], imageScale: NSNumber?, completion: ((images: [Int : AnyObject]) -> Void)?) { | |
var imageDict = [Int : AnyObject]() // This should be UIImage? but Objective-C can't have nil in dictionaries | |
let manager = AFHTTPSessionManager() |
This file contains 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
public override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { | |
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) | |
// Before rotation (willTransitionToInterfaceOrientation) | |
// ... Do stuff that should happen before rotation ... // | |
coordinator.animateAlongsideTransition({ (context) in | |
// During rotation | |
// ... Do stuff that should happen during rotation ... // | |
}, | |
completion: { (context) in | |
// After rotation (didTransitionToInterfaceOrientation) |
This file contains 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
// 1.) Swift selectors must have @objc if they are not exposed to objective-c (i.e. they are private to a swift class) | |
// 2.) Make a failable initializer by adding an optional (?) after initializer (init?()) | |
// 3.) To cast in a for in loop use "case". Use "where" to unwrap optionals or check a condition. | |
for case let button as AClass in view.subviews { | |
} | |
for view in childViews where view is UIButton { | |
} | |
for case let view as MyCustomView in self.view.subviews { | |
view.removeFromSuperview | |
} |
This file contains 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
extension UIViewController { | |
func isVisible() -> Bool { | |
return self.isViewLoaded() && self.view.window != nil | |
} | |
} |
This file contains 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
// Perform selector | |
self.performSelector(#selector(MyViewController.myFunction(_:)), withObject: myObject, afterDelay: 1.0) | |
// Cancel perform requests | |
self.classForCoder.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(MyViewController.myFunction(_:)), object: myObject) | |
// Note: - | |
// myObject can be nil |
This file contains 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
/** | |
Function that locks an object while it is being acted upon in the closure parameter. | |
Rethrows allows the function to throw an error for throwing closures or not for non-throwing closures (do not need `try` for non-throwing closures). | |
parameter lock: Object to be sync'd | |
parameter closure: Code of critical section | |
*/ | |
public func synchronized<T>(lock: AnyObject, @noescape closure: () throws -> T) rethrows -> T { | |
objc_sync_enter(lock) | |
defer { | |
objc_sync_exit(lock) |
This file contains 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
public someFunction() { | |
({ self.someStringVariable = "Some value" })() | |
} |