Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DOAlertController-DEMO/DerivedData/
DerivedData
DOAlertController-DEMO/DOAlertController-DEMO.xcodeproj/xcuserdata
DOAlertController-DEMO/DOAlertController-DEMO.xcodeproj/project.xcworkspace/xcuserdata/
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@
4F47CAA31AE26BD100BF71E4 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0620;
ORGANIZATIONNAME = "Daiki Okumura";
TargetAttributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DOAlertAction : NSObject, NSCopying {
}

func copyWithZone(zone: NSZone) -> AnyObject {
let copy = self.dynamicType(title: title, style: style, handler: handler)
let copy = self.dynamicType.init(title: title, style: style, handler: handler)
copy.enabled = self.enabled
return copy
}
Expand All @@ -56,14 +56,14 @@ class DOAlertAction : NSObject, NSCopying {
// MARK: DOAlertAnimation Class

class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {

let isPresenting: Bool

init(isPresenting: Bool) {
self.isPresenting = isPresenting
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
if (isPresenting) {
return 0.45
} else {
Expand All @@ -81,8 +81,8 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {

func presentAnimateTransition(transitionContext: UIViewControllerContextTransitioning) {

var alertController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! DOAlertController
var containerView = transitionContext.containerView()
let alertController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! DOAlertController
let containerView = transitionContext.containerView()

alertController.overlayView.alpha = 0.0
if (alertController.isAlert()) {
Expand All @@ -92,7 +92,7 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
} else {
alertController.alertView.transform = CGAffineTransformMakeTranslation(0, alertController.alertView.frame.height)
}
containerView.addSubview(alertController.view)
containerView!.addSubview(alertController.view)

UIView.animateWithDuration(0.25,
animations: {
Expand All @@ -114,13 +114,13 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
if (finished) {
transitionContext.completeTransition(true)
}
})
})
})
})
}

func dismissAnimateTransition(transitionContext: UIViewControllerContextTransitioning) {

var alertController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! DOAlertController
let alertController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! DOAlertController

UIView.animateWithDuration(self.transitionDuration(transitionContext),
animations: {
Expand All @@ -134,7 +134,7 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
},
completion: { finished in
transitionContext.completeTransition(true)
})
})
}
}

Expand Down Expand Up @@ -311,15 +311,15 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
//------------------------------
// Layout Constraint
//------------------------------
overlayView.setTranslatesAutoresizingMaskIntoConstraints(false)
containerView.setTranslatesAutoresizingMaskIntoConstraints(false)
alertView.setTranslatesAutoresizingMaskIntoConstraints(false)
textAreaScrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
textAreaView.setTranslatesAutoresizingMaskIntoConstraints(false)
textContainer.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonAreaScrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonAreaView.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonContainer.setTranslatesAutoresizingMaskIntoConstraints(false)
overlayView.translatesAutoresizingMaskIntoConstraints = false
containerView.translatesAutoresizingMaskIntoConstraints = false
alertView.translatesAutoresizingMaskIntoConstraints = false
textAreaScrollView.translatesAutoresizingMaskIntoConstraints = false
textAreaView.translatesAutoresizingMaskIntoConstraints = false
textContainer.translatesAutoresizingMaskIntoConstraints = false
buttonAreaScrollView.translatesAutoresizingMaskIntoConstraints = false
buttonAreaView.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.translatesAutoresizingMaskIntoConstraints = false

// self.view
let overlayViewTopSpaceConstraint = NSLayoutConstraint(item: overlayView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1.0, constant: 0.0)
Expand Down Expand Up @@ -422,7 +422,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
super.viewDidAppear(animated)

if (!isAlert() && cancelButtonTag != 0) {
var tapGesture = UITapGestureRecognizer(target: self, action: "handleContainerViewTapGesture:")
let tapGesture = UITapGestureRecognizer(target: self, action: "handleContainerViewTapGesture:")
containerView.addGestureRecognizer(tapGesture)
}
}
Expand Down Expand Up @@ -489,7 +489,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
var textFieldContainerHeight: CGFloat = 0.0

// TextFields
for (i, obj) in enumerate(textFields!) {
for (_, obj) in textFields!.enumerate() {
let textField = obj as! UITextField
textField.frame = CGRectMake(0.0, textFieldContainerHeight, innerContentWidth, textField.frame.height)
textFieldContainerHeight += textField.frame.height + 0.5
Expand Down Expand Up @@ -550,7 +550,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
if (!isAlert() && buttons.count > 1) {
buttonAreaPositionY += buttonMargin
}
var button = buttonAreaScrollView.viewWithTag(cancelButtonTag) as! UIButton
let button = buttonAreaScrollView.viewWithTag(cancelButtonTag) as! UIButton
let action = actions[cancelButtonTag - 1] as! DOAlertAction
button.titleLabel?.font = buttonFont[action.style]
button.setTitleColor(buttonTextColor[action.style], forState: .Normal)
Expand Down Expand Up @@ -634,10 +634,10 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
}

// UIColor -> UIImage
func createImageFromUIColor(var color: UIColor) -> UIImage {
func createImageFromUIColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContext(rect.size)
let contextRef: CGContextRef = UIGraphicsGetCurrentContext()
let contextRef = UIGraphicsGetCurrentContext()!
CGContextSetFillColorWithColor(contextRef, color.CGColor)
CGContextFillRect(contextRef, rect)
let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
Expand Down Expand Up @@ -687,8 +687,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
if (action.style == DOAlertActionStyle.Cancel) {
for ac in actions as! [DOAlertAction] {
if (ac.style == DOAlertActionStyle.Cancel) {
var error: NSError?
NSException.raise("NSInternalInconsistencyException", format:"DOAlertController can only have one action with a style of DOAlertActionStyleCancel", arguments:getVaList([error ?? "nil"]))
assert(false, "DOAlertController can only have one action with a style of DOAlertActionStyleCancel")
return
}
}
Expand All @@ -713,15 +712,14 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle

// You can add a text field only if the preferredStyle property is set to DOAlertControllerStyle.Alert.
if (!isAlert()) {
var error: NSError?
NSException.raise("NSInternalInconsistencyException", format: "Text fields can only be added to an alert controller of style DOAlertControllerStyleAlert", arguments:getVaList([error ?? "nil"]))
assert(false, "Text fields can only be added to an alert controller of style DOAlertControllerStyleAlert")
return
}
if (textFields == nil) {
textFields = []
}

var textField = UITextField()
let textField = UITextField()
textField.frame.size = CGSizeMake(innerContentWidth, textFieldHeight)
textField.borderStyle = UITextBorderStyle.None
textField.backgroundColor = textFieldBgColor
Expand Down Expand Up @@ -757,4 +755,4 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return DOAlertAnimation(isPresenting: false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class ViewController : UITableViewController, UITextFieldDelegate {
textField.keyboardAppearance = UIKeyboardAppearance.Dark
textField.returnKeyType = UIReturnKeyType.Next

var label:UILabel = UILabel(frame: CGRectMake(0, 0, 50, 30))
let label:UILabel = UILabel(frame: CGRectMake(0, 0, 50, 30))
label.text = "ID"
label.font = UIFont(name: "GillSans-Bold", size: 15.0)
textField.leftView = label
Expand All @@ -256,7 +256,7 @@ class ViewController : UITableViewController, UITextFieldDelegate {
textField.keyboardAppearance = UIKeyboardAppearance.Dark
textField.returnKeyType = UIReturnKeyType.Send

var label:UILabel = UILabel(frame: CGRectMake(0, 0, 50, 30))
let label:UILabel = UILabel(frame: CGRectMake(0, 0, 50, 30))
label.text = "PASS"
label.font = UIFont(name: "GillSans-Bold", size: 15.0)
textField.leftView = label
Expand Down Expand Up @@ -395,7 +395,7 @@ class ViewController : UITableViewController, UITextFieldDelegate {
let textField = notification.object as! UITextField

// Enforce a minimum length of >= 5 characters for secure text alerts.
secureTextAlertAction!.enabled = count(textField.text) >= 5
secureTextAlertAction!.enabled = textField.text!.characters.count >= 5
}

// MARK: UITextFieldDelegate Methods
Expand Down
56 changes: 27 additions & 29 deletions DOAlertController/DOAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DOAlertAction : NSObject, NSCopying {
}

func copyWithZone(zone: NSZone) -> AnyObject {
let copy = self.dynamicType(title: title, style: style, handler: handler)
let copy = self.dynamicType.init(title: title, style: style, handler: handler)
copy.enabled = self.enabled
return copy
}
Expand All @@ -56,14 +56,14 @@ class DOAlertAction : NSObject, NSCopying {
// MARK: DOAlertAnimation Class

class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {

let isPresenting: Bool

init(isPresenting: Bool) {
self.isPresenting = isPresenting
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
if (isPresenting) {
return 0.45
} else {
Expand All @@ -81,8 +81,8 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {

func presentAnimateTransition(transitionContext: UIViewControllerContextTransitioning) {

var alertController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! DOAlertController
var containerView = transitionContext.containerView()
let alertController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! DOAlertController
let containerView = transitionContext.containerView()

alertController.overlayView.alpha = 0.0
if (alertController.isAlert()) {
Expand All @@ -92,7 +92,7 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
} else {
alertController.alertView.transform = CGAffineTransformMakeTranslation(0, alertController.alertView.frame.height)
}
containerView.addSubview(alertController.view)
containerView!.addSubview(alertController.view)

UIView.animateWithDuration(0.25,
animations: {
Expand All @@ -114,13 +114,13 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
if (finished) {
transitionContext.completeTransition(true)
}
})
})
})
})
}

func dismissAnimateTransition(transitionContext: UIViewControllerContextTransitioning) {

var alertController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! DOAlertController
let alertController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! DOAlertController

UIView.animateWithDuration(self.transitionDuration(transitionContext),
animations: {
Expand All @@ -134,7 +134,7 @@ class DOAlertAnimation : NSObject, UIViewControllerAnimatedTransitioning {
},
completion: { finished in
transitionContext.completeTransition(true)
})
})
}
}

Expand Down Expand Up @@ -311,15 +311,15 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
//------------------------------
// Layout Constraint
//------------------------------
overlayView.setTranslatesAutoresizingMaskIntoConstraints(false)
containerView.setTranslatesAutoresizingMaskIntoConstraints(false)
alertView.setTranslatesAutoresizingMaskIntoConstraints(false)
textAreaScrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
textAreaView.setTranslatesAutoresizingMaskIntoConstraints(false)
textContainer.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonAreaScrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonAreaView.setTranslatesAutoresizingMaskIntoConstraints(false)
buttonContainer.setTranslatesAutoresizingMaskIntoConstraints(false)
overlayView.translatesAutoresizingMaskIntoConstraints = false
containerView.translatesAutoresizingMaskIntoConstraints = false
alertView.translatesAutoresizingMaskIntoConstraints = false
textAreaScrollView.translatesAutoresizingMaskIntoConstraints = false
textAreaView.translatesAutoresizingMaskIntoConstraints = false
textContainer.translatesAutoresizingMaskIntoConstraints = false
buttonAreaScrollView.translatesAutoresizingMaskIntoConstraints = false
buttonAreaView.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.translatesAutoresizingMaskIntoConstraints = false

// self.view
let overlayViewTopSpaceConstraint = NSLayoutConstraint(item: overlayView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1.0, constant: 0.0)
Expand Down Expand Up @@ -422,7 +422,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
super.viewDidAppear(animated)

if (!isAlert() && cancelButtonTag != 0) {
var tapGesture = UITapGestureRecognizer(target: self, action: "handleContainerViewTapGesture:")
let tapGesture = UITapGestureRecognizer(target: self, action: "handleContainerViewTapGesture:")
containerView.addGestureRecognizer(tapGesture)
}
}
Expand Down Expand Up @@ -489,7 +489,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
var textFieldContainerHeight: CGFloat = 0.0

// TextFields
for (i, obj) in enumerate(textFields!) {
for (_, obj) in textFields!.enumerate() {
let textField = obj as! UITextField
textField.frame = CGRectMake(0.0, textFieldContainerHeight, innerContentWidth, textField.frame.height)
textFieldContainerHeight += textField.frame.height + 0.5
Expand Down Expand Up @@ -550,7 +550,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
if (!isAlert() && buttons.count > 1) {
buttonAreaPositionY += buttonMargin
}
var button = buttonAreaScrollView.viewWithTag(cancelButtonTag) as! UIButton
let button = buttonAreaScrollView.viewWithTag(cancelButtonTag) as! UIButton
let action = actions[cancelButtonTag - 1] as! DOAlertAction
button.titleLabel?.font = buttonFont[action.style]
button.setTitleColor(buttonTextColor[action.style], forState: .Normal)
Expand Down Expand Up @@ -634,10 +634,10 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
}

// UIColor -> UIImage
func createImageFromUIColor(var color: UIColor) -> UIImage {
func createImageFromUIColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContext(rect.size)
let contextRef: CGContextRef = UIGraphicsGetCurrentContext()
let contextRef = UIGraphicsGetCurrentContext()!
CGContextSetFillColorWithColor(contextRef, color.CGColor)
CGContextFillRect(contextRef, rect)
let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
Expand Down Expand Up @@ -687,8 +687,7 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle
if (action.style == DOAlertActionStyle.Cancel) {
for ac in actions as! [DOAlertAction] {
if (ac.style == DOAlertActionStyle.Cancel) {
var error: NSError?
NSException.raise("NSInternalInconsistencyException", format:"DOAlertController can only have one action with a style of DOAlertActionStyleCancel", arguments:getVaList([error ?? "nil"]))
assert(false, "DOAlertController can only have one action with a style of DOAlertActionStyleCancel")
return
}
}
Expand All @@ -713,15 +712,14 @@ class DOAlertController : UIViewController, UITextFieldDelegate, UIViewControlle

// You can add a text field only if the preferredStyle property is set to DOAlertControllerStyle.Alert.
if (!isAlert()) {
var error: NSError?
NSException.raise("NSInternalInconsistencyException", format: "Text fields can only be added to an alert controller of style DOAlertControllerStyleAlert", arguments:getVaList([error ?? "nil"]))
assert(false, "Text fields can only be added to an alert controller of style DOAlertControllerStyleAlert")
return
}
if (textFields == nil) {
textFields = []
}

var textField = UITextField()
let textField = UITextField()
textField.frame.size = CGSizeMake(innerContentWidth, textFieldHeight)
textField.borderStyle = UITextBorderStyle.None
textField.backgroundColor = textFieldBgColor
Expand Down