+added the files

This commit is contained in:
Goktug Yilmaz 2015-11-14 18:08:09 +02:00 committed by Goktug Yilmaz
parent 0747c284e7
commit dbb4c4447c
30 changed files with 2359 additions and 4 deletions

View File

@ -12,6 +12,34 @@
E1839DBE1BF79335002212C6 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E1839DBC1BF79335002212C6 /* Main.storyboard */; };
E1839DC01BF79335002212C6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E1839DBF1BF79335002212C6 /* Assets.xcassets */; };
E1839DC31BF79335002212C6 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E1839DC11BF79335002212C6 /* LaunchScreen.storyboard */; };
E1839DE61BF79359002212C6 /* EZSwiftExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCA1BF79359002212C6 /* EZSwiftExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DE71BF79359002212C6 /* NSObjectExtentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCB1BF79359002212C6 /* NSObjectExtentions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DE81BF79359002212C6 /* BoolExtentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCC1BF79359002212C6 /* BoolExtentions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DE91BF79359002212C6 /* IntExtentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCD1BF79359002212C6 /* IntExtentions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DEA1BF79359002212C6 /* StringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCE1BF79359002212C6 /* StringExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DEB1BF79359002212C6 /* ArrayExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DCF1BF79359002212C6 /* ArrayExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DEC1BF79359002212C6 /* DictionaryExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD01BF79359002212C6 /* DictionaryExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DED1BF79359002212C6 /* NSDateExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD11BF79359002212C6 /* NSDateExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DEE1BF79359002212C6 /* CGRectExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD21BF79359002212C6 /* CGRectExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DEF1BF79359002212C6 /* CGFloatExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD31BF79359002212C6 /* CGFloatExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF01BF79359002212C6 /* UIViewControllerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD41BF79359002212C6 /* UIViewControllerExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF11BF79359002212C6 /* UIViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD51BF79359002212C6 /* UIViewExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF21BF79359002212C6 /* UITextViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD61BF79359002212C6 /* UITextViewExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF31BF79359002212C6 /* UILabelExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD71BF79359002212C6 /* UILabelExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF41BF79359002212C6 /* UIImageViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD81BF79359002212C6 /* UIImageViewExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF51BF79359002212C6 /* UIImageExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DD91BF79359002212C6 /* UIImageExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF61BF79359002212C6 /* UIColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDA1BF79359002212C6 /* UIColorExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF71BF79359002212C6 /* UIFontExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDB1BF79359002212C6 /* UIFontExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF81BF79359002212C6 /* UIButtonExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDC1BF79359002212C6 /* UIButtonExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DF91BF79359002212C6 /* UIDeviceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDD1BF79359002212C6 /* UIDeviceExtensions.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFA1BF79359002212C6 /* UIColoredView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDE1BF79359002212C6 /* UIColoredView.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFB1BF79359002212C6 /* BlockButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DDF1BF79359002212C6 /* BlockButton.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFC1BF79359002212C6 /* BlockLongPress.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE01BF79359002212C6 /* BlockLongPress.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFD1BF79359002212C6 /* BlockPan.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE11BF79359002212C6 /* BlockPan.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFE1BF79359002212C6 /* BlockPinch.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE21BF79359002212C6 /* BlockPinch.swift */; settings = {ASSET_TAGS = (); }; };
E1839DFF1BF79359002212C6 /* BlockSwipe.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE31BF79359002212C6 /* BlockSwipe.swift */; settings = {ASSET_TAGS = (); }; };
E1839E001BF79359002212C6 /* BlockTap.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE41BF79359002212C6 /* BlockTap.swift */; settings = {ASSET_TAGS = (); }; };
E1839E011BF79359002212C6 /* BlockWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1839DE51BF79359002212C6 /* BlockWebView.swift */; settings = {ASSET_TAGS = (); }; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -22,6 +50,34 @@
E1839DBF1BF79335002212C6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
E1839DC21BF79335002212C6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
E1839DC41BF79335002212C6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E1839DCA1BF79359002212C6 /* EZSwiftExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EZSwiftExtensions.swift; sourceTree = "<group>"; };
E1839DCB1BF79359002212C6 /* NSObjectExtentions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSObjectExtentions.swift; sourceTree = "<group>"; };
E1839DCC1BF79359002212C6 /* BoolExtentions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoolExtentions.swift; sourceTree = "<group>"; };
E1839DCD1BF79359002212C6 /* IntExtentions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntExtentions.swift; sourceTree = "<group>"; };
E1839DCE1BF79359002212C6 /* StringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtensions.swift; sourceTree = "<group>"; };
E1839DCF1BF79359002212C6 /* ArrayExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayExtensions.swift; sourceTree = "<group>"; };
E1839DD01BF79359002212C6 /* DictionaryExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryExtensions.swift; sourceTree = "<group>"; };
E1839DD11BF79359002212C6 /* NSDateExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateExtensions.swift; sourceTree = "<group>"; };
E1839DD21BF79359002212C6 /* CGRectExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGRectExtensions.swift; sourceTree = "<group>"; };
E1839DD31BF79359002212C6 /* CGFloatExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloatExtensions.swift; sourceTree = "<group>"; };
E1839DD41BF79359002212C6 /* UIViewControllerExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewControllerExtensions.swift; sourceTree = "<group>"; };
E1839DD51BF79359002212C6 /* UIViewExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewExtensions.swift; sourceTree = "<group>"; };
E1839DD61BF79359002212C6 /* UITextViewExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextViewExtensions.swift; sourceTree = "<group>"; };
E1839DD71BF79359002212C6 /* UILabelExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILabelExtensions.swift; sourceTree = "<group>"; };
E1839DD81BF79359002212C6 /* UIImageViewExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewExtensions.swift; sourceTree = "<group>"; };
E1839DD91BF79359002212C6 /* UIImageExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageExtensions.swift; sourceTree = "<group>"; };
E1839DDA1BF79359002212C6 /* UIColorExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorExtensions.swift; sourceTree = "<group>"; };
E1839DDB1BF79359002212C6 /* UIFontExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIFontExtensions.swift; sourceTree = "<group>"; };
E1839DDC1BF79359002212C6 /* UIButtonExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButtonExtensions.swift; sourceTree = "<group>"; };
E1839DDD1BF79359002212C6 /* UIDeviceExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIDeviceExtensions.swift; sourceTree = "<group>"; };
E1839DDE1BF79359002212C6 /* UIColoredView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColoredView.swift; sourceTree = "<group>"; };
E1839DDF1BF79359002212C6 /* BlockButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockButton.swift; sourceTree = "<group>"; };
E1839DE01BF79359002212C6 /* BlockLongPress.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockLongPress.swift; sourceTree = "<group>"; };
E1839DE11BF79359002212C6 /* BlockPan.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockPan.swift; sourceTree = "<group>"; };
E1839DE21BF79359002212C6 /* BlockPinch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockPinch.swift; sourceTree = "<group>"; };
E1839DE31BF79359002212C6 /* BlockSwipe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockSwipe.swift; sourceTree = "<group>"; };
E1839DE41BF79359002212C6 /* BlockTap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockTap.swift; sourceTree = "<group>"; };
E1839DE51BF79359002212C6 /* BlockWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockWebView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -54,11 +110,39 @@
E1839DB71BF79335002212C6 /* EZSwiftExtensions */ = {
isa = PBXGroup;
children = (
E1839DB81BF79335002212C6 /* AppDelegate.swift */,
E1839DBA1BF79335002212C6 /* ViewController.swift */,
E1839DCA1BF79359002212C6 /* EZSwiftExtensions.swift */,
E1839DCB1BF79359002212C6 /* NSObjectExtentions.swift */,
E1839DCC1BF79359002212C6 /* BoolExtentions.swift */,
E1839DCD1BF79359002212C6 /* IntExtentions.swift */,
E1839DCE1BF79359002212C6 /* StringExtensions.swift */,
E1839DCF1BF79359002212C6 /* ArrayExtensions.swift */,
E1839DD01BF79359002212C6 /* DictionaryExtensions.swift */,
E1839DD11BF79359002212C6 /* NSDateExtensions.swift */,
E1839DD21BF79359002212C6 /* CGRectExtensions.swift */,
E1839DD31BF79359002212C6 /* CGFloatExtensions.swift */,
E1839DD41BF79359002212C6 /* UIViewControllerExtensions.swift */,
E1839DD51BF79359002212C6 /* UIViewExtensions.swift */,
E1839DD61BF79359002212C6 /* UITextViewExtensions.swift */,
E1839DD71BF79359002212C6 /* UILabelExtensions.swift */,
E1839DD81BF79359002212C6 /* UIImageViewExtensions.swift */,
E1839DD91BF79359002212C6 /* UIImageExtensions.swift */,
E1839DDA1BF79359002212C6 /* UIColorExtensions.swift */,
E1839DDB1BF79359002212C6 /* UIFontExtensions.swift */,
E1839DDC1BF79359002212C6 /* UIButtonExtensions.swift */,
E1839DDD1BF79359002212C6 /* UIDeviceExtensions.swift */,
E1839DDE1BF79359002212C6 /* UIColoredView.swift */,
E1839DDF1BF79359002212C6 /* BlockButton.swift */,
E1839DE01BF79359002212C6 /* BlockLongPress.swift */,
E1839DE11BF79359002212C6 /* BlockPan.swift */,
E1839DE21BF79359002212C6 /* BlockPinch.swift */,
E1839DE31BF79359002212C6 /* BlockSwipe.swift */,
E1839DE41BF79359002212C6 /* BlockTap.swift */,
E1839DE51BF79359002212C6 /* BlockWebView.swift */,
E1839DBC1BF79335002212C6 /* Main.storyboard */,
E1839DBF1BF79335002212C6 /* Assets.xcassets */,
E1839DC11BF79335002212C6 /* LaunchScreen.storyboard */,
E1839DB81BF79335002212C6 /* AppDelegate.swift */,
E1839DBA1BF79335002212C6 /* ViewController.swift */,
E1839DC41BF79335002212C6 /* Info.plist */,
);
path = EZSwiftExtensions;
@ -134,8 +218,36 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E1839DF31BF79359002212C6 /* UILabelExtensions.swift in Sources */,
E1839DF91BF79359002212C6 /* UIDeviceExtensions.swift in Sources */,
E1839DE81BF79359002212C6 /* BoolExtentions.swift in Sources */,
E1839DFC1BF79359002212C6 /* BlockLongPress.swift in Sources */,
E1839DF71BF79359002212C6 /* UIFontExtensions.swift in Sources */,
E1839DFD1BF79359002212C6 /* BlockPan.swift in Sources */,
E1839DE71BF79359002212C6 /* NSObjectExtentions.swift in Sources */,
E1839DEF1BF79359002212C6 /* CGFloatExtensions.swift in Sources */,
E1839DF01BF79359002212C6 /* UIViewControllerExtensions.swift in Sources */,
E1839DF81BF79359002212C6 /* UIButtonExtensions.swift in Sources */,
E1839E001BF79359002212C6 /* BlockTap.swift in Sources */,
E1839DFF1BF79359002212C6 /* BlockSwipe.swift in Sources */,
E1839DEC1BF79359002212C6 /* DictionaryExtensions.swift in Sources */,
E1839DF61BF79359002212C6 /* UIColorExtensions.swift in Sources */,
E1839DE91BF79359002212C6 /* IntExtentions.swift in Sources */,
E1839DFB1BF79359002212C6 /* BlockButton.swift in Sources */,
E1839DEA1BF79359002212C6 /* StringExtensions.swift in Sources */,
E1839DEE1BF79359002212C6 /* CGRectExtensions.swift in Sources */,
E1839DF51BF79359002212C6 /* UIImageExtensions.swift in Sources */,
E1839DFA1BF79359002212C6 /* UIColoredView.swift in Sources */,
E1839DF11BF79359002212C6 /* UIViewExtensions.swift in Sources */,
E1839DFE1BF79359002212C6 /* BlockPinch.swift in Sources */,
E1839DBB1BF79335002212C6 /* ViewController.swift in Sources */,
E1839DE61BF79359002212C6 /* EZSwiftExtensions.swift in Sources */,
E1839DB91BF79335002212C6 /* AppDelegate.swift in Sources */,
E1839DED1BF79359002212C6 /* NSDateExtensions.swift in Sources */,
E1839DEB1BF79359002212C6 /* ArrayExtensions.swift in Sources */,
E1839E011BF79359002212C6 /* BlockWebView.swift in Sources */,
E1839DF41BF79359002212C6 /* UIImageViewExtensions.swift in Sources */,
E1839DF21BF79359002212C6 /* UITextViewExtensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,49 @@
//
// ArrayExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension Array {
/// EZSwiftExtensions
func random() -> Element {
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
/// EZSwiftExtensions
func indexOfObject<T: Equatable>(object: T) -> Int? {
var i: Int
for index in 0..<self.count {
if let arrayObject = self[index] as? T {
if arrayObject == object {
return index
}
}
}
return nil
}
/// EZSwiftExtensions
mutating func removeObject<U: Equatable>(object: U) {
var index: Int?
for (idx, objectToCompare) in self.enumerate() {
if let to = objectToCompare as? U {
if object == to {
index = idx
}
}
}
if index != nil {
self.removeAtIndex(index!)
}
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
</dependencies>
<scenes>
<!--View Controller-->

View File

@ -0,0 +1,105 @@
//
// BlockButton.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
typealias BlockButtonAction = (sender: BlockButton) -> Void
class BlockButton: UIButton {
// MARK: Propeties
var highlightLayer: CALayer?
var action: BlockButtonAction?
// MARK: Init
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
defaultInit()
}
init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
super.init(frame: CGRect(x: x, y: y, width: w, height: h))
defaultInit()
}
init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, action: BlockButtonAction?) {
super.init (frame: CGRect(x: x, y: y, width: w, height: h))
self.action = action
defaultInit()
}
init(action: BlockButtonAction) {
super.init(frame: CGRectZero)
self.action = action
defaultInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
defaultInit()
}
init(frame: CGRect, action: BlockButtonAction) {
super.init(frame: frame)
self.action = action
defaultInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
private func defaultInit() {
addTarget(self, action: "didPressed:", forControlEvents: UIControlEvents.TouchUpInside)
addTarget(self, action: "highlight", forControlEvents: [UIControlEvents.TouchDown, UIControlEvents.TouchDragEnter])
addTarget(self, action: "unhighlight", forControlEvents: [UIControlEvents.TouchUpInside, UIControlEvents.TouchUpOutside, UIControlEvents.TouchCancel, UIControlEvents.TouchDragExit])
}
func addAction(action: BlockButtonAction) {
self.action = action
}
// MARK: Action
func didPressed(sender: BlockButton) {
action?(sender: sender)
}
// MARK: Highlight
func highlight() {
if action == nil {
return
}
let highlightLayer = CALayer()
highlightLayer.frame = layer.bounds
highlightLayer.backgroundColor = UIColor.blackColor().CGColor
highlightLayer.opacity = 0.5
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, false, 0)
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let maskImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let maskLayer = CALayer()
maskLayer.contents = maskImage.CGImage
maskLayer.frame = highlightLayer.frame
highlightLayer.mask = maskLayer
layer.addSublayer(highlightLayer)
self.highlightLayer = highlightLayer
}
func unhighlight() {
if action == nil {
return
}
highlightLayer?.removeFromSuperlayer()
highlightLayer = nil
}
}

View File

@ -0,0 +1,28 @@
//
// BlockLongPress.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockLongPress: UILongPressGestureRecognizer {
private var longPressAction: ((UILongPressGestureRecognizer) -> Void)?
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
}
convenience init (action: ((UILongPressGestureRecognizer) -> Void)?) {
self.init()
longPressAction = action
addTarget(self, action: "didLongPressed:")
}
func didLongPressed (longPress: UILongPressGestureRecognizer) {
longPressAction? (longPress)
}
}

View File

@ -0,0 +1,28 @@
//
// BlockPan.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockPan: UIPanGestureRecognizer {
private var panAction: ((UIPanGestureRecognizer) -> Void)?
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
}
convenience init (action: ((UIPanGestureRecognizer) -> Void)?) {
self.init()
self.panAction = action
self.addTarget(self, action: "didPan:")
}
func didPan (pan: UIPanGestureRecognizer) {
panAction? (pan)
}
}

View File

@ -0,0 +1,29 @@
//
// BlockPinch.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockPinch: UIPinchGestureRecognizer {
private var pinchAction: ((UIPinchGestureRecognizer) -> Void)?
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
}
convenience init (action: ((UIPinchGestureRecognizer) -> Void)?) {
self.init()
self.pinchAction = action
self.addTarget(self, action: "didPinch:")
}
func didPinch (pinch: UIPinchGestureRecognizer) {
pinchAction? (pinch)
}
}

View File

@ -0,0 +1,33 @@
//
// BlockSwipe.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockSwipe: UISwipeGestureRecognizer {
private var swipeAction: ((UISwipeGestureRecognizer) -> Void)?
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
}
convenience init (direction: UISwipeGestureRecognizerDirection,
fingerCount: Int,
action: ((UISwipeGestureRecognizer) -> Void)?) {
self.init()
self.direction = direction
numberOfTouchesRequired = fingerCount
swipeAction = action
addTarget(self, action: "didSwipe:")
}
func didSwipe (swipe: UISwipeGestureRecognizer) {
swipeAction? (swipe)
}
}

View File

@ -0,0 +1,34 @@
//
// BlockTap.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockTap: UITapGestureRecognizer {
private var tapAction: ((UITapGestureRecognizer) -> Void)?
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
}
convenience init (
tapCount: Int,
fingerCount: Int,
action: ((UITapGestureRecognizer) -> Void)?) {
self.init()
self.numberOfTapsRequired = tapCount
self.numberOfTouchesRequired = fingerCount
self.tapAction = action
self.addTarget(self, action: "didTap:")
}
func didTap (tap: UITapGestureRecognizer) {
tapAction? (tap)
}
}

View File

@ -0,0 +1,56 @@
//
// BlockWebView.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
class BlockWebView: UIWebView, UIWebViewDelegate {
var didStartLoad: ((NSURLRequest) -> ())?
var didFinishLoad: ((NSURLRequest) -> ())?
var didFailLoad: ((NSURLRequest, NSError) -> ())?
var shouldStartLoadingRequest: ((NSURLRequest) -> (Bool))?
override init(frame: CGRect) {
super.init(frame: frame)
delegate = self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func webViewDidStartLoad(webView: UIWebView) {
didStartLoad? (webView.request!)
}
func webViewDidFinishLoad(webView: UIWebView) {
didFinishLoad? (webView.request!)
}
func webView(
webView: UIWebView,
didFailLoadWithError error: NSError?) {
didFailLoad? (webView.request!, error!)
}
func webView(
webView: UIWebView,
shouldStartLoadWithRequest request: NSURLRequest,
navigationType: UIWebViewNavigationType) -> Bool {
if let should = shouldStartLoadingRequest {
return should (request)
} else {
return true
}
}
}

View File

@ -0,0 +1,21 @@
//
// BoolExtentions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 16/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
extension Bool {
/// EZSwiftExtensions
mutating func toggle() -> Bool {
self = !self
return self
}
}

View File

@ -0,0 +1,14 @@
//
// CEMKit+CGFloat.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
/// EZSwiftExtensions
func degreesToRadians (angle: CGFloat) -> CGFloat {
return (CGFloat (M_PI) * angle) / 180.0
}

View File

@ -0,0 +1,18 @@
//
// CGRectExtensions.swift
// Qorum
//
// Created by Goktug Yilmaz on 26/08/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension CGRect {
/// EZSwiftExtensions
init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
self.init(x: x, y: y, width: w, height: h)
}
}

View File

@ -0,0 +1,26 @@
//
// DictionaryExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension Dictionary {
/// EZSwiftExtensions, combines the first dictionary with the second and returns single dictionary
func union(other: Dictionary) -> Dictionary {
var temp = self
for (key,value) in other {
temp.updateValue(value, forKey:key)
}
return temp
}
}
/// EZSwiftExtensions
func += <KeyType, ValueType> (inout left: Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}

View File

@ -0,0 +1,207 @@
//
// EZSwiftExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 13/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
//TODO: others standart video, gif
/// EZSwiftExtensions
var appVersion: String {
return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
/// EZSwiftExtensions
var appBuild: String {
return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
/// EZSwiftExtensions
var appVersionAndBuild: String {
let version = appVersion, build = appBuild
return version == build ? "v\(version)" : "v\(version)(\(build))"
}
/// EZSwiftExtensions - Your normal appDelegate
var appDelegateVariable: AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
var topMostVC: UIViewController {
var topController = appDelegateVariable.window!.rootViewController
while topController?.presentedViewController != nil {
topController = topController?.presentedViewController
}
return topController!
}
/// EZSwiftExtensions
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
/// EZSwiftExtensions
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
/// EZSwiftExtensions
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
/// EZSwiftExtensions
var screenStatusBarHeight: CGFloat {
return UIApplication.sharedApplication().statusBarFrame.height
}
/// EZSwiftExtensions
var screenHeightWithoutStatusBar: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height - screenStatusBarHeight
} else {
return UIScreen.mainScreen().bounds.size.width - screenStatusBarHeight
}
}
/// EZSwiftExtensions
func forceCrash() {
//NSException(name:NSGenericException, reason:"Everything is ok. This is just a test crash.", userInfo:nil).raise()
while 1 == 1 {
var hey: [Int] = []
hey[0] = 1
}
}
/// EZSwiftExtensions
func prt(items: Any...) {
print(items)
}
/// EZSwiftExtensions
func prnt(items: Any...) {
print(items)
}
/// EZSwiftExtensions - Calls action when a screen shot is taken
func detectScreenShot(action: () -> ()) {
let mainQueue = NSOperationQueue.mainQueue()
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
// executes after screenshot
action()
}
}
// MARK: - Dispatch
/// EZSwiftExtensions
func runThisMany(times times: Int, block: () -> ()) {
for _ in 0..<times {
block()
}
}
/// EZSwiftExtensions
func runThisAfterDelay(seconds seconds: Double, after: () -> ()) {
runThisAfterDelay(seconds: seconds, queue: dispatch_get_main_queue(), after: after)
}
//TODO: Make this easier
/// EZSwiftExtensions - dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
func runThisAfterDelay(seconds seconds: Double, queue: dispatch_queue_t, after: ()->()) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(time, queue, after)
}
/// EZSwiftExtensions - Submits a block for asynchronous execution on the main queue
func runThisInMainThread(block: dispatch_block_t) {
dispatch_async(dispatch_get_main_queue(), block)
}
/// EZSwiftExtensions
func runThisBlock(block: () -> ()) {
block()
}
/// EZSwiftExtensions - Runs in Default priority queue
func runThisInBackground(block: () -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}
// MARK: - DownloadTask
/// EZSwiftExtensions
func requestImage(url: String, success: (UIImage?) -> Void) {
requestURL(url, success: { (data) -> Void in
if let d = data {
success(UIImage(data: d))
}
})
}
/// EZSwiftExtensions
func requestJSON(url: String, success: (AnyObject? -> Void), error: ((NSError) -> Void)?) {
requestURL(url,
success: { (data)->Void in
let json: AnyObject? = dataToJsonDict(data)
success(json)
},
error: { (err)->Void in
if let e = error {
e(err)
}
})
}
/// EZSwiftExtensions
private func dataToJsonDict(data: NSData?) -> AnyObject? {
if let d = data {
var error: NSError?
let json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(
d,
options: NSJSONReadingOptions.AllowFragments)
} catch let error1 as NSError {
error = error1
json = nil
}
if let _ = error {
return nil
} else {
return json
}
} else {
return nil
}
}
/// EZSwiftExtensions
private func requestURL(url: String, success: (NSData?) -> Void, error: ((NSError) -> Void)? = nil) {
NSURLConnection.sendAsynchronousRequest(
NSURLRequest(URL: NSURL (string: url)!),
queue: NSOperationQueue.mainQueue(),
completionHandler: { response, data, err in
if let e = err {
error?(e)
} else {
success(data)
}
})
}

View File

@ -0,0 +1,54 @@
//
// IntExtentions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 16/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension Int {
/// EZSwiftExtensions
var isEven: Bool { return (self % 2 == 0) }
/// EZSwiftExtensions
var isOdd: Bool { return (self % 2 != 0) }
/// EZSwiftExtensions
var isPositive: Bool { return (self >= 0) }
/// EZSwiftExtensions
var isNegative: Bool { return (self < 0) }
/// EZSwiftExtensions
var toDouble: Double { return Double(self) }
/// EZSwiftExtensions
var toFloat: Float { return Float(self) }
/// EZSwiftExtensions
var toCGFloat: CGFloat { return CGFloat(self) }
/// EZSwiftExtensions
var digits: Int {
if self == 0 {
return 1
} else if Int(fabs(Double(self))) <= LONG_MAX {
return Int(log10(fabs(Double(self)))) + 1
} else {
return -1; //out of bound
}
}
/// EZSwiftExtensions - Execute action this many times
func times(action: () -> ()) {
if self > 0 {
for _ in 0..<self {
action()
}
}
}
}
extension UInt {
/// EZSwiftExtensions
var toInt: Int { return Int(self) }
}

View File

@ -0,0 +1,97 @@
//
// NSDateExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension NSDate {
/// EZSwiftExtensions
func toString() -> String {
let formatter = NSDateFormatter()
formatter.timeStyle = NSDateFormatterStyle.NoStyle
formatter.timeStyle = NSDateFormatterStyle.MediumStyle
return formatter.stringFromDate(self)
}
//TODO: add in readme
/// EZSwiftExtensions
func fromString(format: String, string: String) -> NSDate? {
let formatter = NSDateFormatter()
formatter.dateFormat = format
return formatter.dateFromString(string)
}
/// EZSwiftExtensions
func daysInBetweenDate(date: NSDate) -> Double {
var diff = self.timeIntervalSinceNow - date.timeIntervalSinceNow
diff = fabs(diff/86400)
return diff
}
/// EZSwiftExtensions
func hoursInBetweenDate(date: NSDate) -> Double {
var diff = self.timeIntervalSinceNow - date.timeIntervalSinceNow
diff = fabs(diff/3600)
return diff
}
/// EZSwiftExtensions
func minutesInBetweenDate(date: NSDate) -> Double {
var diff = self.timeIntervalSinceNow - date.timeIntervalSinceNow
diff = fabs(diff/60)
return diff
}
/// EZSwiftExtensions
func secondsInBetweenDate(date: NSDate) -> Double {
var diff = self.timeIntervalSinceNow - date.timeIntervalSinceNow
diff = fabs(diff)
return diff
}
/// EZSwiftExtensions
func timePassed() -> String {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: self, toDate: date, options: [])
var str: String
if components.year >= 1 {
components.year == 1 ? (str = "year") : (str = "years")
return "\(components.year) \(str) ago"
} else if components.month >= 1 {
components.month == 1 ? (str = "month") : (str = "months")
return "\(components.month) \(str) ago"
} else if components.day >= 1 {
components.day == 1 ? (str = "day") : (str = "days")
return "\(components.day) \(str) ago"
} else if components.hour >= 1 {
components.hour == 1 ? (str = "hour") : (str = "hours")
return "\(components.hour) \(str) ago"
} else if components.minute >= 1 {
components.minute == 1 ? (str = "minute") : (str = "minutes")
return "\(components.minute) \(str) ago"
} else if components.second == 0 {
return "Just now"
} else {
return "\(components.second) seconds ago"
}
}
}
extension NSDate: Comparable {}
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.isEqualToDate(rhs)
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
}

View File

@ -0,0 +1,24 @@
//
// NSObjectExtentions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 16/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension NSObject {
var className: String {
return self.dynamicType.className
}
static var className: String {
return stringFromClass(self)
}
}
func stringFromClass(aClass: AnyClass) -> String {
return NSStringFromClass(aClass).componentsSeparatedByString(".").last!
}

View File

@ -0,0 +1,115 @@
//
// StringExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension String {
/// EZSwiftExtensions
subscript(integerIndex: Int) -> Character {
let index = startIndex.advancedBy(integerIndex)
return self[index]
}
/// EZSwiftExtensions
subscript(integerRange: Range<Int>) -> String {
let start = startIndex.advancedBy(integerRange.startIndex)
let end = startIndex.advancedBy(integerRange.endIndex)
let range = start..<end
return self[range]
}
/// EZSwiftExtensions - Character count
var length: Int {
return self.characters.count
}
/// EZSwiftExtensions
var capitalizeFirst: String {
var result = self
result.replaceRange(startIndex...startIndex, with: String(self[startIndex]).capitalizedString)
return result
}
/// EZSwiftExtensions - Counts whitespace & new lines
func isOnlyEmptySpacesAndNewLineCharacters() -> Bool {
let characterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
let newText = self.stringByTrimmingCharactersInSet(characterSet)
return newText.isEmpty
}
/// EZSwiftExtensions - Trims white space and new line characters
mutating func trim() {
self = self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
/// EZSwiftExtensions
var isEmail: Bool {
let dataDetector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue)
let firstMatch = dataDetector?.firstMatchInString(self, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, length))
return (firstMatch?.range.location != NSNotFound && firstMatch?.URL?.scheme == "mailto")
}
/// EZSwiftExtensions
var extractURLs: [NSURL] {
var urls : [NSURL] = []
let detector: NSDataDetector?
do {
detector = try NSDataDetector(types: NSTextCheckingType.Link.rawValue)
} catch _ as NSError {
detector = nil
}
let text = self
detector!.enumerateMatchesInString(text, options: [], range: NSMakeRange(0, text.characters.count), usingBlock: {
(result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
urls.append(result!.URL!)
})
return urls
}
/// EZSwiftExtensions
func contains(find: String) -> Bool {
return self.rangeOfString(find) != nil
}
/// EZSwiftExtensions
func contains(find: String, compareOption: NSStringCompareOptions) -> Bool {
return self.rangeOfString(find, options: compareOption) != nil
}
/// EZSwiftExtensions
func toInt() -> Int? {
if let num = NSNumberFormatter().numberFromString(self) {
return num.integerValue
} else {
return nil
}
}
/// EZSwiftExtensions
func toDouble() -> Double? {
if let num = NSNumberFormatter().numberFromString(self) {
return num.doubleValue
} else {
return nil
}
}
/// EZSwiftExtensions
func toFloat() -> Float? {
if let num = NSNumberFormatter().numberFromString(self) {
return num.floatValue
} else {
return nil
}
}
}

View File

@ -0,0 +1,28 @@
//
// UIButtonExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIButton {
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, target: AnyObject, action: Selector) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
}
/// EZSwiftExtensions
func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, forState: forState)
}
}

View File

@ -0,0 +1,60 @@
//
// UIColorExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIColor {
/// EZSwiftExtensions
convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1)
}
/// EZSwiftExtensions
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
/// EZSwiftExtensions
class func Gray(gray: CGFloat) -> UIColor {
return UIColor(r: gray, g: gray, b: gray)
}
/// EZSwiftExtensions
class func Gray(gray: CGFloat, alpha: CGFloat) -> UIColor {
return UIColor(r: gray, g: gray, b: gray, a: alpha)
}
/// EZSwiftExtensions
var red: Int {
var r: CGFloat = 0
getRed(&r, green: nil, blue: nil, alpha: nil)
return Int(r * 255)
}
/// EZSwiftExtensions
var green: Int {
var g: CGFloat = 0
getRed(nil, green: &g, blue: nil, alpha: nil)
return Int(g * 255)
}
/// EZSwiftExtensions
var blue: Int {
var b: CGFloat = 0
getRed(nil, green: nil, blue: &b, alpha: nil)
return Int(b * 255)
}
/// EZSwiftExtensions
var alpha: Int {
var a: CGFloat = 0
getRed(nil, green: nil, blue: nil, alpha: &a)
return Int(a)
}
}

View File

@ -0,0 +1,40 @@
//
// UIColoredView.swift
// InstaApp
//
// Created by Goktug Yilmaz on 26/10/15.
// Copyright © 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
//TODO: readme
class UIColoredView: UIView {
init() {
super.init(frame: CGRect(x: 100, y: 100, w: 100, h: 100))
backgroundColor = UIColor.randomColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension UIColor {
/// SwiftRandom extension
static func randomColor(randomAlpha: Bool = false) -> UIColor {
let randomRed = CGFloat.randomColor()
let randomGreen = CGFloat.randomColor()
let randomBlue = CGFloat.randomColor()
let alpha = randomAlpha ? CGFloat.randomColor() : 1.0
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: alpha)
}
}
private extension CGFloat {
/// SwiftRandom extension
static func randomColor(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
}
}

View File

@ -0,0 +1,106 @@
//
// UIDeviceExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
/// EZSwiftExtensions
private let DeviceList = [
/* iPod 5 */ "iPod5,1": "iPod Touch 5",
/* iPod 6 */ "iPod7,1": "iPod Touch 6",
/* iPhone 4 */ "iPhone3,1": "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4S */ "iPhone4,1": "iPhone 4S",
/* iPhone 5 */ "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
/* iPhone 5C */ "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",
/* iPhone 5S */ "iPhone6,1": "iPhone 5S", "iPhone6,2": "iPhone 5S",
/* iPhone 6 */ "iPhone7,2": "iPhone 6",
/* iPhone 6 Plus */ "iPhone7,1": "iPhone 6 Plus",
/* iPhone 6S */ "iPhone8,1": "iPhone 6S",
/* iPhone 6S Plus */ "iPhone8,2": "iPhone 6S Plus",
/* iPad 2 */ "iPad2,1": "iPad 2", "iPad2,2": "iPad 2", "iPad2,3": "iPad 2", "iPad2,4": "iPad 2",
/* iPad 3 */ "iPad3,1": "iPad 3", "iPad3,2": "iPad 3", "iPad3,3": "iPad 3",
/* iPad 4 */ "iPad3,4": "iPad 4", "iPad3,5": "iPad 4", "iPad3,6": "iPad 4",
/* iPad Air */ "iPad4,1": "iPad Air", "iPad4,2": "iPad Air", "iPad4,3": "iPad Air",
/* iPad Air 2 */ "iPad5,3": "iPad Air 2", "iPad5,4": "iPad Air 2",
/* iPad Mini */ "iPad2,5": "iPad Mini", "iPad2,6": "iPad Mini", "iPad2,7": "iPad Mini",
/* iPad Mini 2 */ "iPad4,4": "iPad Mini 2", "iPad4,5": "iPad Mini 2", "iPad4,6": "iPad Mini 2",
/* iPad Mini 3 */ "iPad4,7": "iPad Mini 3", "iPad4,8": "iPad Mini 3", "iPad4,9": "iPad Mini 3",
/* iPad Mini 4 */ "iPad5,1": "iPad Mini 4", "iPad5,2": "iPad Mini 4",
/* iPad Pro */ "iPad6,7": "iPad Pro", "iPad6,8": "iPad Pro",
/* AppleTV */ "AppleTV5,3": "AppleTV",
/* Simulator */ "x86_64": "Simulator", "i386": "Simulator"
]
extension UIDevice {
/// EZSwiftExtensions
class func idForVendor() -> String {
return UIDevice.currentDevice().identifierForVendor!.UUIDString
}
/// EZSwiftExtensions - Operating system name
class func systemName() -> String {
return UIDevice.currentDevice().systemName
}
/// EZSwiftExtensions - Operating system version
class func systemVersion() -> String {
return UIDevice.currentDevice().systemVersion
}
/// EZSwiftExtensions
class func deviceName() -> String {
return UIDevice.currentDevice().name
}
/// EZSwiftExtensions
class func deviceLanguage() -> String {
return NSBundle.mainBundle().preferredLocalizations[0]
}
/// EZSwiftExtensions
class func deviceModelReadable() -> String {
return DeviceList[deviceModel()] ?? deviceModel()
}
/// EZSwiftExtensions
class func deviceModel() -> String {
var systemInfo = utsname()
uname(&systemInfo)
let machine = systemInfo.machine
var identifier = ""
let mirror = Mirror(reflecting: machine)
for child in mirror.children {
let value = child.value
if let value = value as? Int8 where value != 0 {
identifier.append(UnicodeScalar(UInt8(value)))
}
}
return identifier
}
/// EZSwiftExtensions
class func isSystemVersionOver(requiredVersion: String) -> Bool {
switch systemVersion().compare(requiredVersion, options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
//println("iOS >= 8.0")
return true
case .OrderedAscending:
//println("iOS < 8.0")
return false
}
}
}

View File

@ -0,0 +1,86 @@
//
// CEMKit+UIFont.swift
//
//
// Created by Cem Olcay on 12/08/15.
//
//
import UIKit
/// EZSwiftExtensions
enum FontType: String {
case None = ""
case Regular = "Regular"
case Bold = "Bold"
case DemiBold = "DemiBold"
case Light = "Light"
case UltraLight = "UltraLight"
case Italic = "Italic"
case Thin = "Thin"
case Book = "Book"
case Roman = "Roman"
case Medium = "Medium"
case MediumItalic = "MediumItalic"
case CondensedMedium = "CondensedMedium"
case CondensedExtraBold = "CondensedExtraBold"
case SemiBold = "SemiBold"
case BoldItalic = "BoldItalic"
case Heavy = "Heavy"
}
/// EZSwiftExtensions
enum FontName: String {
case HelveticaNeue = "HelveticaNeue"
case Helvetica = "Helvetica"
case Futura = "Futura"
case Menlo = "Menlo"
case Avenir = "Avenir"
case AvenirNext = "AvenirNext"
case Didot = "Didot"
case AmericanTypewriter = "AmericanTypewriter"
case Baskerville = "Baskerville"
case Geneva = "Geneva"
case GillSans = "GillSans"
case SanFranciscoDisplay = "SanFranciscoDisplay"
case Seravek = "Seravek"
}
extension UIFont {
/// EZSwiftExtensions
class func PrintFontFamily(font: FontName) {
let arr = UIFont.fontNamesForFamilyName(font.rawValue)
for name in arr {
print(name)
}
}
/// EZSwiftExtensions
class func Font(name: FontName, type: FontType, size: CGFloat) -> UIFont {
if type == FontType.None {
return UIFont(name: name.rawValue, size: size)!
}
return UIFont(name: name.rawValue + "-" + type.rawValue, size: size)!
}
/// EZSwiftExtensions
class func HelveticaNeue(type type: FontType, size: CGFloat) -> UIFont {
return Font(.HelveticaNeue, type: type, size: size)
}
/// EZSwiftExtensions
class func AvenirNext(type type: FontType, size: CGFloat) -> UIFont {
return UIFont.Font(.AvenirNext, type: type, size: size)
}
/// EZSwiftExtensions
class func AvenirNextDemiBold(size size: CGFloat) -> UIFont {
return AvenirNext(type: .DemiBold, size: size)
}
/// EZSwiftExtensions
class func AvenirNextRegular(size size: CGFloat) -> UIFont {
return AvenirNext(type: .Regular, size: size)
}
}

View File

@ -0,0 +1,82 @@
//
// UIImageExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIImage {
/// EZSwiftExtensions - rate: 0 to 1
func compressImage(rate rate: CGFloat) -> NSData {
let compressedImage = UIImageJPEGRepresentation(self, rate)
return compressedImage!
}
/// EZSwiftExtensions
func getSizeAsBytes() -> Int {
let imageData = NSData(data: UIImageJPEGRepresentation(self, 1)!)
return imageData.length
}
/// EZSwiftExtensions
func getSizeAsKilobytes() -> Int {
let imageData = NSData(data: UIImageJPEGRepresentation(self, 1)!)
return imageData.length / 1024
}
/// EZSwiftExtensions
class func scaleTo(image image: UIImage, w: CGFloat, h: CGFloat) -> UIImage {
let newSize = CGSize(width: w, height: h)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.drawInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
/// EZSwiftExtensions, might return low quality
func resizeWithWidth(width: CGFloat) -> UIImage {
let aspectSize = CGSize (width: width, height: aspectHeightForWidth(width))
UIGraphicsBeginImageContext(aspectSize)
self.drawInRect(CGRect(origin: CGPointZero, size: aspectSize))
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
/// EZSwiftExtensions, might return low quality
func resizeWithHeight(height: CGFloat) -> UIImage {
let aspectSize = CGSize (width: aspectWidthForHeight(height), height: height)
UIGraphicsBeginImageContext(aspectSize)
self.drawInRect(CGRect(origin: CGPointZero, size: aspectSize))
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
/// EZSwiftExtensions
func aspectHeightForWidth(width: CGFloat) -> CGFloat {
return (width * self.size.height) / self.size.width
}
/// EZSwiftExtensions
func aspectWidthForHeight(height: CGFloat) -> CGFloat {
return (height * self.size.width) / self.size.height
}
/// EZSwiftExtensions
func croppedImage(bound: CGRect) -> UIImage {
let scaledBounds: CGRect = CGRectMake(bound.origin.x * self.scale, bound.origin.y * self.scale, bound.size.width * self.scale, bound.size.height * self.scale)
let imageRef = CGImageCreateWithImageInRect(self.CGImage, scaledBounds)
let croppedImage: UIImage = UIImage(CGImage: imageRef!, scale: self.scale, orientation: UIImageOrientation.Up)
return croppedImage
}
}

View File

@ -0,0 +1,82 @@
//
// UIImageViewExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIImageView {
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, imageName: String) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
image = UIImage(named: imageName)
}
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, imageName: String, scaleToWidth: CGFloat) {
self.init(frame: CGRect(x: x, y: y, width: 0, height: 0))
image = UIImage(named: imageName)
scaleImageFrameToWidth(width: scaleToWidth)
}
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, image: UIImage) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
self.image = image
}
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, image: UIImage, scaleToWidth: CGFloat) {
self.init(frame: CGRect(x: x, y: y, width: 0, height: 0))
self.image = image
scaleImageFrameToWidth(width: scaleToWidth)
}
/// EZSwiftExtensions, scales this ImageView size to fit the given width
func scaleImageFrameToWidth(width width: CGFloat) {
let widthRatio = image!.size.width / width
let newWidth = image!.size.width / widthRatio
let newHeigth = image!.size.height / widthRatio
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: newWidth, height: newHeigth)
}
/// EZSwiftExtensions
func roundSquareImage() {
self.clipsToBounds = true
self.layer.cornerRadius = self.frame.size.width / 2
}
/// EZSwiftExtensions
func imageWithUrl(url url: String) {
requestImage(url, success: { (image) -> Void in
if let img = image {
self.image = img
}
})
}
/// EZSwiftExtensions
func imageWithUrl(url url: String, placeholder: UIImage) {
self.image = placeholder
requestImage(url, success: { (image) -> Void in
if let img = image {
self.image = img
}
})
}
/// EZSwiftExtensions
func imageWithUrl(url url: String, placeholderNamed: String) {
self.image = UIImage(named: placeholderNamed)
requestImage(url, success: { (image) -> Void in
if let img = image {
self.image = img
}
})
}
}

View File

@ -0,0 +1,63 @@
//
// UILabelExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UILabel {
/// EZSwiftExtensions: fontSize: 17
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
self.init(x: x, y: y, w: w, h: h, fontSize: 17)
}
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, fontSize: CGFloat) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
font = UIFont.HelveticaNeue(type: FontType.None, size: fontSize)
backgroundColor = UIColor.clearColor()
clipsToBounds = true
textAlignment = NSTextAlignment.Left
userInteractionEnabled = true
numberOfLines = 1
}
/// EZSwiftExtensions
func getEstimatedSize(width: CGFloat = CGFloat.max, height: CGFloat = CGFloat.max) -> CGSize {
return sizeThatFits(CGSize(width: width, height: height))
}
/// EZSwiftExtensions
func getEstimatedHeight() -> CGFloat {
return sizeThatFits(CGSize(width: w, height: CGFloat.max)).height
}
/// EZSwiftExtensions
func getEstimatedWidth() -> CGFloat {
return sizeThatFits(CGSize(width: CGFloat.max, height: h)).width
}
/// EZSwiftExtensions
func fitHeight() {
self.h = getEstimatedHeight()
}
/// EZSwiftExtensions
func fitWidth() {
self.w = getEstimatedWidth()
}
/// EZSwiftExtensions
func fitSize() {
self.fitWidth()
self.fitHeight()
sizeToFit()
}
}

View File

@ -0,0 +1,29 @@
//
// UITextViewExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UITextView {
/// EZSwiftExtensions: Automatically sets these values: backgroundColor = clearColor, textColor = ThemeNicknameColor, clipsToBounds = true, textAlignment = Left, userInteractionEnabled = true, editable = false, scrollEnabled = false, font = ThemeFontName, fontsize = 17
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
self.init(x: x, y: y, w: w, h: h, fontSize: 17)
}
/// EZSwiftExtensions: Automatically sets these values: backgroundColor = clearColor, textColor = ThemeNicknameColor, clipsToBounds = true, textAlignment = Left, userInteractionEnabled = true, editable = false, scrollEnabled = false, font = ThemeFontName
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, fontSize: CGFloat) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
font = UIFont.HelveticaNeue(type: FontType.None, size: fontSize)
backgroundColor = UIColor.clearColor()
clipsToBounds = true
textAlignment = NSTextAlignment.Left
userInteractionEnabled = true
editable = false
scrollEnabled = false
}
}

View File

@ -0,0 +1,218 @@
//
// UIViewControllerExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIViewController {
// MARK: - Notifications
//TODO: Document this part
func addNotificationObserver(name: String, selector: Selector) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil)
}
func removeNotificationObserver(name: String) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: name, object: nil)
}
func removeNotificationObserver() {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func addKeyboardWillShowNotification() {
self.addNotificationObserver(UIKeyboardWillShowNotification, selector: "keyboardWillShowNotification:");
}
func addKeyboardDidShowNotification() {
self.addNotificationObserver(UIKeyboardDidShowNotification, selector: "keyboardDidShowNotification:");
}
func addKeyboardWillHideNotification() {
self.addNotificationObserver(UIKeyboardWillHideNotification, selector: "keyboardWillHideNotification:");
}
func addKeyboardDidHideNotification() {
self.addNotificationObserver(UIKeyboardDidHideNotification, selector: "keyboardDidHideNotification:");
}
func removeKeyboardWillShowNotification() {
self.removeNotificationObserver(UIKeyboardWillShowNotification);
}
func removeKeyboardDidShowNotification() {
self.removeNotificationObserver(UIKeyboardDidShowNotification);
}
func removeKeyboardWillHideNotification() {
self.removeNotificationObserver(UIKeyboardWillHideNotification);
}
func removeKeyboardDidHideNotification() {
self.removeNotificationObserver(UIKeyboardDidHideNotification);
}
func keyboardDidShowNotification(notification: NSNotification) {
let nInfo = notification.userInfo as! [String: NSValue]
let value = nInfo[UIKeyboardFrameEndUserInfoKey]
let frame = value?.CGRectValue()
keyboardDidShowWithFrame(frame!)
}
func keyboardWillShowNotification(notification: NSNotification) {
let nInfo = notification.userInfo as! [String: NSValue]
let value = nInfo[UIKeyboardFrameEndUserInfoKey]
let frame = value?.CGRectValue()
keyboardWillShowWithFrame(frame!)
}
func keyboardWillHideNotification(notification: NSNotification) {
let nInfo = notification.userInfo as! [String: NSValue]
let value = nInfo[UIKeyboardFrameEndUserInfoKey]
let frame = value?.CGRectValue()
keyboardWillHideWithFrame(frame!)
}
func keyboardDidHideNotification(notification: NSNotification) {
let nInfo = notification.userInfo as! [String: NSValue]
let value = nInfo[UIKeyboardFrameEndUserInfoKey]
let frame = value?.CGRectValue()
keyboardDidHideWithFrame(frame!)
}
func keyboardWillShowWithFrame(frame: CGRect) {
}
func keyboardDidShowWithFrame(frame: CGRect) {
}
func keyboardWillHideWithFrame(frame: CGRect) {
}
func keyboardDidHideWithFrame(frame: CGRect) {
}
// MARK: - VC Container
/// EZSwiftExtensions
var top: CGFloat {
get {
if let me = self as? UINavigationController {
return me.visibleViewController!.top
}
if let nav = self.navigationController {
if nav.navigationBarHidden {
return view.top
} else {
return nav.navigationBar.bottom
}
} else {
return view.top
}
}
}
/// EZSwiftExtensions
var bottom: CGFloat {
get {
if let me = self as? UINavigationController {
return me.visibleViewController!.bottom
}
if let tab = tabBarController {
if tab.tabBar.hidden {
return view.bottom
} else {
return tab.tabBar.top
}
} else {
return view.bottom
}
}
}
/// EZSwiftExtensions
var tabBarHeight: CGFloat {
get {
if let me = self as? UINavigationController {
return me.visibleViewController!.tabBarHeight
}
if let tab = self.tabBarController {
return tab.tabBar.frame.size.height
}
return 0
}
}
/// EZSwiftExtensions
var navigationBarHeight: CGFloat {
get {
if let me = self as? UINavigationController {
return me.visibleViewController!.navigationBarHeight
}
if let nav = self.navigationController {
return nav.navigationBar.h
}
return 0
}
}
/// EZSwiftExtensions
var navigationBarColor: UIColor? {
get {
if let me = self as? UINavigationController {
return me.visibleViewController!.navigationBarColor
}
return navigationController?.navigationBar.tintColor
} set(value) {
navigationController?.navigationBar.barTintColor = value
}
}
/// EZSwiftExtensions
var navBar: UINavigationBar? {
get {
return navigationController?.navigationBar
}
}
/// EZSwiftExtensions
var applicationFrame: CGRect {
get {
return CGRect(x: view.x, y: top, width: view.w, height: bottom - top)
}
}
// MARK: - VC Flow
/// EZSwiftExtensions
func pushVC(vc: UIViewController) {
navigationController?.pushViewController(vc, animated: true)
}
/// EZSwiftExtensions
func popVC() {
navigationController?.popViewControllerAnimated(true)
}
/// EZSwiftExtensions
func presentVC(vc: UIViewController) {
presentViewController(vc, animated: true, completion: nil)
}
/// EZSwiftExtensions
func dismissVC(completion completion: (() -> Void)? ) {
dismissViewControllerAnimated(true, completion: completion)
}
}

View File

@ -0,0 +1,511 @@
//
// UIViewExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
// MARK: Custom UIView Initilizers
extension UIView {
/// EZSwiftExtensions
convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
}
/// EZSwiftExtensions, puts padding around the view
convenience init(superView: UIView, padding: CGFloat) {
self.init(frame: CGRect(x: superView.x + padding, y: superView.y + padding, width: superView.w - padding*2, height: superView.h - padding*2))
}
/// EZSwiftExtensions - Copies size of superview
convenience init(superView: UIView) {
self.init(frame: CGRect(origin: CGPointZero, size: superView.size))
}
}
// MARK: Frame Extensions
extension UIView {
//TODO: Multipe addsubview
//TODO: Add pics to readme
/// EZSwiftExtensions, resizes this view so it fits the largest subview
func resizeToFitSubviews() {
var width: CGFloat = 0
var height: CGFloat = 0
for someView in self.subviews {
let aView = someView
let newWidth = aView.x + aView.w
let newHeight = aView.y + aView.h
width = max(width, newWidth)
height = max(height, newHeight)
}
frame = CGRect(x: x, y: y, width: width, height: height)
}
/// EZSwiftExtensions, resizes this view so it fits the largest subview
func resizeToFitSubviews(tagsToIgnore: [Int]) {
var width: CGFloat = 0
var height: CGFloat = 0
for someView in self.subviews {
let aView = someView
if !tagsToIgnore.contains(someView.tag) {
let newWidth = aView.x + aView.w
let newHeight = aView.y + aView.h
width = max(width, newWidth)
height = max(height, newHeight)
}
}
frame = CGRect(x: x, y: y, width: width, height: height)
}
/// EZSwiftExtensions
func resizeToFitWidth() {
let currentHeight = self.h
self.sizeToFit()
self.h = currentHeight
}
/// EZSwiftExtensions
func resizeToFitHeight() {
let currentWidth = self.w
self.sizeToFit()
self.w = currentWidth
}
/// EZSwiftExtensions
var x: CGFloat {
get {
return self.frame.origin.x
} set(value) {
self.frame = CGRect(x: value, y: self.y, width: self.w, height: self.h)
}
}
/// EZSwiftExtensions
var y: CGFloat {
get {
return self.frame.origin.y
} set(value) {
self.frame = CGRect(x: self.x, y: value, width: self.w, height: self.h)
}
}
/// EZSwiftExtensions
var w: CGFloat {
get {
return self.frame.size.width
} set(value) {
self.frame = CGRect(x: self.x, y: self.y, width: value, height: self.h)
}
}
/// EZSwiftExtensions
var h: CGFloat {
get {
return self.frame.size.height
} set(value) {
self.frame = CGRect(x: self.x, y: self.y, width: self.w, height: value)
}
}
/// EZSwiftExtensions
var left: CGFloat {
get {
return self.x
} set(value) {
self.x = value
}
}
/// EZSwiftExtensions
var right: CGFloat {
get {
return self.x + self.w
} set(value) {
self.x = value - self.w
}
}
/// EZSwiftExtensions
var top: CGFloat {
get {
return self.y
} set(value) {
self.y = value
}
}
/// EZSwiftExtensions
var bottom: CGFloat {
get {
return self.y + self.h
} set(value) {
self.y = value - self.h
}
}
/// EZSwiftExtensions
var origin: CGPoint {
get {
return self.frame.origin
} set(value) {
self.frame = CGRect(origin: value, size: self.frame.size)
}
}
/// EZSwiftExtensions
var centerX: CGFloat {
get {
return self.center.x
} set(value) {
self.center.x = value
}
}
/// EZSwiftExtensions
var centerY: CGFloat {
get {
return self.center.y
} set(value) {
self.center.y = value
}
}
/// EZSwiftExtensions
var size: CGSize {
get {
return self.frame.size
} set(value) {
self.frame = CGRect(origin: self.frame.origin, size: value)
}
}
/// EZSwiftExtensions
func leftOffset(offset: CGFloat) -> CGFloat {
return self.left - offset
}
/// EZSwiftExtensions
func rightOffset(offset: CGFloat) -> CGFloat {
return self.right + offset
}
/// EZSwiftExtensions
func topOffset(offset: CGFloat) -> CGFloat {
return self.top - offset
}
/// EZSwiftExtensions
func bottomOffset(offset: CGFloat) -> CGFloat {
return self.bottom + offset
}
//TODO: Add to readme
/// EZSwiftExtensions
func alignRight(offset: CGFloat) -> CGFloat {
return self.w - offset
}
/// EZSwiftExtensions
func reorderSubViews(reorder: Bool = false, tagsToIgnore: [Int] = []) -> CGFloat {
var currentHeight: CGFloat = 0
for someView in subviews {
if !tagsToIgnore.contains(someView.tag) && !(someView ).hidden {
if reorder {
let aView = someView
aView.frame = CGRect(x: aView.frame.origin.x, y: currentHeight, width: aView.frame.width, height: aView.frame.height)
}
currentHeight += someView.frame.height
}
}
return currentHeight
}
func removeSubviews() {
for subview in subviews {
subview.removeFromSuperview()
}
}
}
// MARK: Transform Extensions
extension UIView {
/// EZSwiftExtensions
func setRotationX(x: CGFloat) {
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -1000.0
transform = CATransform3DRotate(transform, degreesToRadians(x), 1.0, 0.0, 0.0)
self.layer.transform = transform
}
/// EZSwiftExtensions
func setRotationY(y: CGFloat) {
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -1000.0
transform = CATransform3DRotate(transform, degreesToRadians(y), 0.0, 1.0, 0.0)
self.layer.transform = transform
}
/// EZSwiftExtensions
func setRotationZ(z: CGFloat) {
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -1000.0
transform = CATransform3DRotate(transform, degreesToRadians(z), 0.0, 0.0, 1.0)
self.layer.transform = transform
}
/// EZSwiftExtensions
func setRotation(x x: CGFloat, y: CGFloat, z: CGFloat) {
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -1000.0
transform = CATransform3DRotate(transform, degreesToRadians(x), 1.0, 0.0, 0.0)
transform = CATransform3DRotate(transform, degreesToRadians(y), 0.0, 1.0, 0.0)
transform = CATransform3DRotate(transform, degreesToRadians(z), 0.0, 0.0, 1.0)
self.layer.transform = transform
}
/// EZSwiftExtensions
func setScale(x x: CGFloat, y: CGFloat) {
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -1000.0
transform = CATransform3DScale(transform, x, y, 1)
self.layer.transform = transform
}
}
// MARK: Layer Extensions
extension UIView {
/// EZSwiftExtensions
func setCornerRadius(radius radius: CGFloat) {
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
//TODO: add this to readme
/// EZSwiftExtensions
func addShadow(offset offset: CGSize, radius: CGFloat, color: UIColor, opacity: Float, cornerRadius: CGFloat? = nil) {
self.layer.shadowOffset = offset
self.layer.shadowRadius = radius
self.layer.shadowOpacity = opacity
self.layer.shadowColor = color.CGColor
if let r = cornerRadius {
self.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: r).CGPath
}
}
/// EZSwiftExtensions
func addBorder(width width: CGFloat, color: UIColor) {
layer.borderWidth = width
layer.borderColor = color.CGColor
layer.masksToBounds = true
}
/// EZSwiftExtensions
func addBorderTop(size size: CGFloat, color: UIColor) {
addBorderUtility(x: 0, y: 0, width: frame.width, height: size, color: color)
}
/// EZSwiftExtensions
func addBorderTopWithPadding(size size: CGFloat, color: UIColor, padding: CGFloat) {
addBorderUtility(x: padding, y: 0, width: frame.width - padding*2, height: size, color: color)
}
/// EZSwiftExtensions
func addBorderBottom(size size: CGFloat, color: UIColor) {
addBorderUtility(x: 0, y: frame.height - size, width: frame.width, height: size, color: color)
}
/// EZSwiftExtensions
func addBorderLeft(size size: CGFloat, color: UIColor) {
addBorderUtility(x: 0, y: 0, width: size, height: frame.height, color: color)
}
/// EZSwiftExtensions
func addBorderRight(size size: CGFloat, color: UIColor) {
addBorderUtility(x: frame.width - size, y: 0, width: size, height: frame.height, color: color)
}
/// EZSwiftExtensions
private func addBorderUtility(x x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: UIColor) {
let border = CALayer()
border.backgroundColor = color.CGColor
border.frame = CGRect(x: x, y: y, width: width, height: height)
layer.addSublayer(border)
}
//TODO: add this to readme
/// EZSwiftExtensions
func drawCircle(fillColor fillColor: UIColor, strokeColor: UIColor, strokeWidth: CGFloat) {
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.w, height: self.w), cornerRadius: self.w/2)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.fillColor = fillColor.CGColor
shapeLayer.strokeColor = strokeColor.CGColor
shapeLayer.lineWidth = strokeWidth
self.layer.addSublayer(shapeLayer)
}
//TODO: add this to readme
/// EZSwiftExtensions
func drawStroke(width width: CGFloat, color: UIColor) {
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.w, height: self.w), cornerRadius: self.w/2)
let shapeLayer = CAShapeLayer ()
shapeLayer.path = path.CGPath
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.strokeColor = color.CGColor
shapeLayer.lineWidth = width
self.layer.addSublayer(shapeLayer)
}
}
private let UIViewAnimationDuration: NSTimeInterval = 1
private let UIViewAnimationSpringDamping: CGFloat = 0.5
private let UIViewAnimationSpringVelocity: CGFloat = 0.5
//TODO: add this to readme
// MARK: Animation Extensions
extension UIView {
/// EZSwiftExtensions
func spring(animations animations: (() -> Void), completion: ((Bool) -> Void)? = nil) {
spring(duration: UIViewAnimationDuration, animations: animations, completion: completion)
}
/// EZSwiftExtensions
func spring(duration duration: NSTimeInterval, animations: (() -> Void), completion: ((Bool) -> Void)? = nil) {
UIView.animateWithDuration(UIViewAnimationDuration, delay: 0, usingSpringWithDamping: UIViewAnimationSpringDamping, initialSpringVelocity: UIViewAnimationSpringVelocity, options: UIViewAnimationOptions.AllowAnimatedContent, animations: animations, completion: completion)
}
/// EZSwiftExtensions
func animate(duration duration: NSTimeInterval, animations: (() -> Void), completion: ((Bool) -> Void)? = nil) {
UIView.animateWithDuration(duration, animations: animations, completion: completion)
}
/// EZSwiftExtensions
func animate(animations animations: (()->Void), completion: ((Bool) -> Void)? = nil) {
animate(duration: UIViewAnimationDuration, animations: animations, completion: completion)
}
/// EZSwiftExtensions
func pop() {
setScale(x: 1.1, y: 1.1)
spring(duration: 0.2, animations: { [unowned self] () -> Void in
self.setScale(x: 1, y: 1)
})
}
/// EZSwiftExtensions
func popBig() {
setScale(x: 1.25, y: 1.25)
spring(duration: 0.2, animations: { [unowned self] () -> Void in
self.setScale(x: 1, y: 1)
})
}
}
//TODO: add this to readme
// MARK: Render Extensions
extension UIView {
/// EZSwiftExtensions
func toImage () -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, 0.0)
drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
// MARK: Gesture Extensions
extension UIView {
/// http://stackoverflow.com/questions/4660371/how-to-add-a-touch-event-to-a-uiview/32182866#32182866
/// EZSwiftExtensions
func addTapGesture(tapNumber tapNumber: Int, target: AnyObject, action: Selector) {
let tap = UITapGestureRecognizer(target: target, action: action)
tap.numberOfTapsRequired = tapNumber
addGestureRecognizer(tap)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addTapGesture(tapNumber tapNumber: Int, action: ((UITapGestureRecognizer) -> ())?) {
let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action)
addGestureRecognizer(tap)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addSwipeGesture(direction direction: UISwipeGestureRecognizerDirection, numberOfTouches: Int, target: AnyObject, action: Selector) {
let swipe = UISwipeGestureRecognizer(target: target, action: action)
swipe.direction = direction
swipe.numberOfTouchesRequired = numberOfTouches
addGestureRecognizer(swipe)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addSwipeGesture(direction direction: UISwipeGestureRecognizerDirection, numberOfTouches: Int, action: ((UISwipeGestureRecognizer) -> ())?) {
let swipe = BlockSwipe(direction: direction, fingerCount: numberOfTouches, action: action)
addGestureRecognizer(swipe)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addPanGesture(target target: AnyObject, action: Selector) {
let pan = UIPanGestureRecognizer(target: target, action: action)
addGestureRecognizer(pan)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addPanGesture(action action: ((UIPanGestureRecognizer) -> ())?) {
let pan = BlockPan(action: action)
addGestureRecognizer(pan)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addPinchGesture(target target: AnyObject, action: Selector) {
let pinch = UIPinchGestureRecognizer(target: target, action: action)
addGestureRecognizer(pinch)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addPinchGesture(action action: ((UIPinchGestureRecognizer) -> ())?) {
let pinch = BlockPinch(action: action)
addGestureRecognizer(pinch)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addLongPressGesture(target target: AnyObject, action: Selector) {
let longPress = UILongPressGestureRecognizer(target: target, action: action)
addGestureRecognizer(longPress)
userInteractionEnabled = true
}
/// EZSwiftExtensions
func addLongPressGesture(action action: ((UILongPressGestureRecognizer) -> ())?) {
let longPress = BlockLongPress(action: action)
addGestureRecognizer(longPress)
userInteractionEnabled = true
}
}
//TODO: add to readme
extension UIView {
/// [UIRectCorner.TopLeft, UIRectCorner.TopRight]
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.CGPath
self.layer.mask = mask
}
}