Last active
April 2, 2016 02:05
-
-
Save krimpedance/e85d738a2e8ae89f805f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MyAlertController.swift | |
// | |
// Copyright © 2016年 Krimpedance. All rights reserved. | |
// | |
import UIKit | |
enum MyAlertControllerType { | |
case SingleButton(String) | |
case DoubleButton(String, String) | |
} | |
enum MyAlertControllerAction { | |
case Cancel | |
case Accept | |
} | |
class MyAlertController: UIViewController { | |
@IBOutlet weak var contentView: UIView! | |
@IBOutlet weak var titleLabel: UILabel! | |
@IBOutlet weak var messageLabel: UILabel! | |
@IBOutlet var singleButtonView: UIView! | |
@IBOutlet var doubleButtonView: UIView! | |
@IBOutlet weak var singleViewAcceptButton: UIButton! | |
@IBOutlet weak var doubleViewCancelButton: UIButton! | |
@IBOutlet weak var doubleViewAcceptButton: UIButton! | |
var titleText: String = "" | |
var messageText: String = "" | |
var buttonType = MyAlertControllerType.SingleButton("") | |
typealias AlertAction = (action: MyAlertControllerAction)->() | |
var handler: AlertAction? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
singleViewAcceptButton.addTarget(self, action: #selector(acceptButtonTapped), forControlEvents: .TouchUpInside) | |
doubleViewAcceptButton.addTarget(self, action: #selector(acceptButtonTapped), forControlEvents: .TouchUpInside) | |
doubleViewCancelButton.addTarget(self, action: #selector(cancelButtonTapped), forControlEvents: .TouchUpInside) | |
titleLabel.text = titleText | |
messageLabel.text = messageText | |
switch buttonType { | |
case let .SingleButton(title) : | |
addSingleButton(title: title) | |
case let .DoubleButton(cancel, accept) : | |
addDoubleButton(cancelTitle: cancel, acceptTitle: accept) | |
} | |
animationContentView(true) | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
animationContentView(false) | |
} | |
private func animationContentView(hidden: Bool, completed: (()->())? = nil) { | |
let alpha: CGFloat | |
if hidden { alpha = 0 } | |
else { alpha = 1 } | |
UIView.animateWithDuration(0.2, animations: { | |
self.view.alpha = alpha | |
}) { _ in | |
completed?() | |
} | |
} | |
private func addSingleButton(title title: String) { | |
singleButtonView.frame.origin = CGPoint(x: 0, y: 120) | |
contentView.addSubview(singleButtonView) | |
singleViewAcceptButton.setTitle(title, forState: .Normal) | |
} | |
private func addDoubleButton(cancelTitle cancelTitle: String, acceptTitle: String) { | |
doubleButtonView.frame.origin = CGPoint(x: 0, y: 120) | |
contentView.addSubview(doubleButtonView) | |
doubleViewCancelButton.setTitle(cancelTitle, forState: .Normal) | |
doubleViewAcceptButton.setTitle(acceptTitle, forState: .Normal) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
/** | |
* Button actions --------------------- | |
*/ | |
extension MyAlertController { | |
func acceptButtonTapped() { | |
animationContentView(true) { | |
self.handler?(action: .Accept) | |
self.dismissViewControllerAnimated(false, completion: nil) | |
} | |
} | |
func cancelButtonTapped() { | |
animationContentView(true) { | |
self.handler?(action: .Cancel) | |
self.dismissViewControllerAnimated(false, completion: nil) | |
} | |
} | |
} | |
/** | |
* Show --------------------- | |
*/ | |
extension MyAlertController { | |
class func show(presentintViewController vc: UIViewController, title: String, message: String, buttonTitle: String, handler: AlertAction?) { | |
guard let alert = UIStoryboard(name: "MyAlertController", bundle: nil).instantiateInitialViewController() | |
as? MyAlertController else { return } | |
alert.titleText = title | |
alert.messageText = message | |
alert.buttonType = .SingleButton(buttonTitle) | |
alert.handler = handler | |
vc.presentViewController(alert, animated: false, completion: nil) | |
} | |
class func show(presentintViewController vc: UIViewController, title: String, message: String, cancelTitle: String, acceptTitle: String, handler: AlertAction?) { | |
guard let alert = UIStoryboard(name: "MyAlertController", bundle: nil).instantiateInitialViewController() | |
as? MyAlertController else { return } | |
alert.titleText = title | |
alert.messageText = message | |
alert.buttonType = .DoubleButton(cancelTitle, acceptTitle) | |
alert.handler = handler | |
vc.presentViewController(alert, animated: false, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment