diff --git a/Classes/YBAlertController.swift b/Classes/YBAlertController.swift index d4ba339..85cb1d2 100644 --- a/Classes/YBAlertController.swift +++ b/Classes/YBAlertController.swift @@ -6,6 +6,8 @@ // Copyright © 2016年 Yabuzaki Yuta. All rights reserved. // +import Foundation + import UIKit public enum YBAlertControllerStyle { @@ -26,17 +28,17 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { // titleLabel public var titleFont = UIFont(name: "Avenir Next", size: 15) - public var titleTextColor = UIColor.blackColor() + public var titleTextColor = UIColor.black // message public var message:String? public var messageLabel = UILabel() public var messageFont = UIFont(name: "Avenir Next", size: 13) - public var messageTextColor = UIColor.lightGrayColor() + public var messageTextColor = UIColor.lightGray // button public var buttonHeight:CGFloat = 50 - public var buttonTextColor = UIColor.blackColor() + public var buttonTextColor = UIColor.black public var buttonIconColor:UIColor? public var buttons = [YBButton]() public var buttonFont = UIFont(name: "Avenir Next", size: 15) @@ -44,7 +46,7 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { // cancelButton public var cancelButtonTitle:String? public var cancelButtonFont = UIFont(name: "Avenir Next", size: 14) - public var cancelButtonTextColor = UIColor.darkGrayColor() + public var cancelButtonTextColor = UIColor.darkGray public var animated = true public var containerView = UIView() @@ -68,17 +70,17 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { public init() { super.init(nibName: nil, bundle: nil) - view.frame = UIScreen.mainScreen().bounds + view.frame = UIScreen.main.bounds - containerView.backgroundColor = UIColor.whiteColor() + containerView.backgroundColor = UIColor.white containerView.clipsToBounds = true - let tapGesture = UITapGestureRecognizer(target: self, action: Selector("dismiss")) + let tapGesture = UITapGestureRecognizer(target: self, action: #selector((dismissView))) tapGesture.numberOfTapsRequired = 1 tapGesture.delegate = self self.view.addGestureRecognizer(tapGesture) - NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("changedOrientation:"), name: UIDeviceOrientationDidChangeNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector((changedOrientation)), name: UIDevice.orientationDidChangeNotification, object: nil) } public convenience init(style: YBAlertControllerStyle) { @@ -94,18 +96,18 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { } deinit { - NSNotificationCenter.defaultCenter().removeObserver(self) + NotificationCenter.default.removeObserver(self) } - func changedOrientation(notification: NSNotification) { + @objc func changedOrientation(notification: NSNotification) { if showing && style == YBAlertControllerStyle.ActionSheet { let value = currentOrientation?.rawValue - UIDevice.currentDevice().setValue(value, forKey: "orientation") + UIDevice.current.setValue(value, forKey: "orientation") return } } - public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if touchingOutsideDismiss == false { return false } if touch.view != gestureRecognizer.view { return false } return true @@ -124,19 +126,19 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { initContainerView() if style == YBAlertControllerStyle.ActionSheet { - UIView.animateWithDuration(0.3, + UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, - options: .CurveEaseIn, + options: .curveEaseIn, animations: { self.containerView.frame.origin.y = self.view.frame.height - self.containerView.frame.height - self.getTopViewController()?.view.transform = CGAffineTransformMakeScale(0.9, 0.9) + // self.getTopViewController()?.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9) }, completion: { (finished) in if self.animated { self.startButtonAppearAnimation() - if let cancelTitle = self.cancelButtonTitle where cancelTitle != "" { + if let cancelTitle = self.cancelButtonTitle, cancelTitle != "" { self.startCancelButtonAppearAnimation() } @@ -145,21 +147,21 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { ) } else { self.containerView.frame.origin.y = self.view.frame.height/2 - self.containerView.frame.height/2 - containerView.transform = CGAffineTransformMakeScale(0.5, 0.5) + containerView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5) containerView.alpha = 0.0 - UIView.animateWithDuration(0.3, + UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, - options: .CurveEaseIn, + options: .curveEaseIn, animations: { self.containerView.alpha = 1.0 - self.containerView.transform = CGAffineTransformMakeScale(1, 1) + self.containerView.transform = CGAffineTransform(scaleX: 1, y: 1) }, completion: { (finished) in if self.animated { self.startButtonAppearAnimation() - if let cancelTitle = self.cancelButtonTitle where cancelTitle != "" { + if let cancelTitle = self.cancelButtonTitle, cancelTitle != "" { self.startCancelButtonAppearAnimation() } @@ -169,37 +171,10 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { } } - public func dismiss() { - showing = false - if let statusBarStyle = currentStatusBarStyle { - UIApplication.sharedApplication().statusBarStyle = statusBarStyle - } - - if style == .ActionSheet { - UIView.animateWithDuration(0.2, - animations: { - self.containerView.frame.origin.y = self.view.frame.height - self.view.backgroundColor = UIColor(white: 0, alpha: 0) - self.getTopViewController()?.view.transform = CGAffineTransformMakeScale(1.0, 1.0) - }, - completion: { (finished) in - self.view.removeFromSuperview() - }) - } else { - UIView.animateWithDuration(0.2, - animations: { - self.view.backgroundColor = UIColor(white: 0, alpha: 0) - self.containerView.transform = CGAffineTransformMakeScale(0.5, 0.5) - self.containerView.alpha = 0 - }, - completion: { (finished) in - self.view.removeFromSuperview() - }) - } - } + private func getTopViewController() -> UIViewController? { - var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController + var topVC = UIApplication.shared.keyWindow?.rootViewController while topVC?.presentedViewController != nil { topVC = topVC?.presentedViewController } @@ -219,56 +194,56 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { instance = self let viewWidth = (style == .ActionSheet) ? view.frame.width : view.frame.width * 0.9 - currentOrientation = UIDevice.currentDevice().orientation - let orientation = UIApplication.sharedApplication().statusBarOrientation - if orientation == UIInterfaceOrientation.Portrait { - currentOrientation = UIDeviceOrientation.Portrait + currentOrientation = UIDevice.current.orientation + let orientation = UIApplication.shared.statusBarOrientation + if orientation == UIInterfaceOrientation.portrait { + currentOrientation = UIDeviceOrientation.portrait } var posY:CGFloat = 0 - if let title = title where title != "" { + if let title = title, title != "" { let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: viewWidth, height: buttonHeight*0.92)) titleLabel.text = title titleLabel.font = titleFont - titleLabel.textAlignment = .Center - titleLabel.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin] + titleLabel.textAlignment = .center + titleLabel.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin] titleLabel.textColor = titleTextColor containerView.addSubview(titleLabel) let line = UIView(frame: CGRect(x: 0, y: titleLabel.frame.height, width: viewWidth, height: 1)) line.backgroundColor = UIColor(white: 0.9, alpha: 1.0) - line.autoresizingMask = [.FlexibleWidth] + line.autoresizingMask = [.flexibleWidth] containerView.addSubview(line) posY = titleLabel.frame.height + line.frame.height } else { posY = 0 } - if let message = message where message != "" { + if let message = message, message != "" { let paddingY:CGFloat = 8 let paddingX:CGFloat = 10 messageLabel.font = messageFont - messageLabel.textColor = UIColor.grayColor() + messageLabel.textColor = UIColor.gray messageLabel.translatesAutoresizingMaskIntoConstraints = false messageLabel.text = message messageLabel.numberOfLines = 0 messageLabel.textColor = messageTextColor - let f = CGSizeMake(viewWidth - paddingX*2, CGFloat.max) + let f = CGSize(width: viewWidth - paddingX*2, height: CGFloat.greatestFiniteMagnitude) let rect = messageLabel.sizeThatFits(f) messageLabel.frame = CGRect(x: paddingX, y: posY + paddingY, width: rect.width, height: rect.height) containerView.addSubview(messageLabel) containerView.addConstraints([ - NSLayoutConstraint(item: messageLabel, attribute: .RightMargin, relatedBy: NSLayoutRelation.Equal, toItem: containerView, attribute: .RightMargin, multiplier: 1, constant: -paddingX), - NSLayoutConstraint(item: messageLabel, attribute: .LeftMargin, relatedBy: .Equal, toItem: containerView, attribute: .LeftMargin, multiplier: 1.0, constant: paddingX), - NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1.0, constant: posY + paddingY), - NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: rect.height) + NSLayoutConstraint(item: messageLabel, attribute: .rightMargin, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containerView, attribute: .rightMargin, multiplier: 1, constant: -paddingX), + NSLayoutConstraint(item: messageLabel, attribute: .leftMargin, relatedBy: .equal, toItem: containerView, attribute: .leftMargin, multiplier: 1.0, constant: paddingX), + NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: posY + paddingY), + NSLayoutConstraint(item: messageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: rect.height) ]) posY = messageLabel.frame.maxY + paddingY let line = UIView(frame: CGRect(x: 0, y: posY, width: viewWidth, height: 1)) line.backgroundColor = UIColor(white: 0.9, alpha: 1.0) - line.autoresizingMask = [.FlexibleWidth] + line.autoresizingMask = [.flexibleWidth] containerView.addSubview(line) posY += line.frame.height @@ -276,7 +251,7 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { for i in 0..Void) { + public func addButton(icon:UIImage?, title:String, action:@escaping ()->Void) { let button = YBButton(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: buttonHeight), icon: icon, text: title) button.actionType = YBButtonActionType.Closure button.action = action button.buttonColor = buttonIconColor button.buttonFont = buttonFont - button.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside) + button.addTarget(self, action: #selector((self.buttonTapped(button:))), for: .touchUpInside) buttons.append(button) } @@ -388,40 +397,40 @@ public class YBAlertController: UIViewController, UIGestureRecognizerDelegate { button.target = target button.selector = selector button.buttonFont = buttonFont - button.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside) + button.addTarget(self, action: #selector((self.buttonTapped(button:))), for: .touchUpInside) buttons.append(button) } - public func addButton(title:String, action:()->Void) { - addButton(nil, title: title, action: action) + public func addButton(title:String, action:@escaping ()->Void) { + addButton(icon: nil, title: title, action: action) } public func addButton(title:String, target:AnyObject, selector:Selector) { - addButton(nil, title: title, target: target, selector: selector) + addButton(icon: nil, title: title, target: target, selector: selector) } - func buttonTapped(button:YBButton) { + @objc func buttonTapped(button:YBButton) { if button.actionType == YBButtonActionType.Closure { button.action() } else if button.actionType == YBButtonActionType.Selector { let control = UIControl() - control.sendAction(button.selector, to: button.target, forEvent: nil) + control.sendAction(button.selector, to: button.target, for: nil) } - dismiss() + dismissView() } } public class YBButton : UIButton { - override public var highlighted : Bool { + override public var isHighlighted : Bool { didSet { - alpha = highlighted ? 0.3 : 1.0 + alpha = isHighlighted ? 0.3 : 1.0 } } var buttonColor:UIColor? { didSet { if let buttonColor = buttonColor { - iconImageView.image = icon?.imageWithRenderingMode(.AlwaysTemplate) + iconImageView.image = icon?.withRenderingMode(.alwaysTemplate) iconImageView.tintColor = buttonColor dotView.dotColor = buttonColor } else { @@ -454,33 +463,33 @@ public class YBButton : UIButton { addSubview(iconImageView) dotView.frame = iconImageView.frame - dotView.backgroundColor = UIColor.clearColor() - dotView.hidden = true + dotView.backgroundColor = UIColor.clear + dotView.isHidden = true addSubview(dotView) let labelHeight = frame.height * 0.8 textLabel.frame = CGRect(x: iconImageView.frame.maxX + 11, y: frame.midY - labelHeight/2, width: frame.width - iconImageView.frame.maxX, height: labelHeight) textLabel.text = text - textLabel.textColor = UIColor.blackColor() + textLabel.textColor = UIColor.black textLabel.font = buttonFont addSubview(textLabel) } func appear() { - iconImageView.transform = CGAffineTransformMakeScale(0, 0) - textLabel.transform = CGAffineTransformMakeScale(0, 0) - dotView.transform = CGAffineTransformMakeScale(0, 0) - dotView.hidden = false - UIView.animateWithDuration(0.2, animations: { - self.textLabel.transform = CGAffineTransformMakeScale(1.0, 1.0) + iconImageView.transform = CGAffineTransform(scaleX: 0, y: 0) + textLabel.transform = CGAffineTransform(scaleX: 0, y: 0) + dotView.transform = CGAffineTransform(scaleX: 0, y: 0) + dotView.isHidden = false + UIView.animate(withDuration: 0.2, animations: { + self.textLabel.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) }) - UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .CurveLinear, animations: { + UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveLinear, animations: { if self.iconImageView.image == nil { - self.dotView.transform = CGAffineTransformMakeScale(1.0, 1.0) + self.dotView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) } else { - self.iconImageView.transform = CGAffineTransformMakeScale(1.0, 1.0) + self.iconImageView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) } }, completion: nil) } @@ -489,20 +498,20 @@ public class YBButton : UIButton { fatalError("init(coder:) has not been implemented") } - override public func drawRect(rect: CGRect) { - UIColor(white: 0.85, alpha: 1.0).setStroke() + override public func draw(_ rect: CGRect) { + UIColor(white: 0.85, alpha: 0.7).setStroke() let line = UIBezierPath() line.lineWidth = 1 - line.moveToPoint(CGPoint(x: iconImageView.frame.maxX + 5, y: frame.height)) - line.addLineToPoint(CGPoint(x: frame.width , y: frame.height)) + line.move(to: CGPoint(x: iconImageView.frame.maxX + 5, y: frame.height)) + line.addLine(to: CGPoint(x: frame.width , y: frame.height)) line.stroke() } } class DotView:UIView { - var dotColor = UIColor.blackColor() + var dotColor = UIColor.black - override func drawRect(rect: CGRect) { + override func draw(_ rect: CGRect) { dotColor.setFill() let circle = UIBezierPath(arcCenter: CGPoint(x: frame.width/2, y: frame.height/2), radius: 3, startAngle: 0, endAngle: 360, clockwise: true) circle.fill() diff --git a/Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Example/Pods/Pods.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Example/Pods/Pods.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Example/Pods/Pods.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + +