From 2697bf4e512fe8617a0d75d63b3ef25a7ee5b192 Mon Sep 17 00:00:00 2001 From: Doug Harper Date: Thu, 17 Nov 2016 17:16:20 -0600 Subject: [PATCH 1/2] Convert to Swift 3 Used the Xcode conversion Tool --- HolyView.xcodeproj/project.pbxproj | 6 +++ HolyView/AppDelegate.swift | 12 ++--- HolyView/HolyView/HolyView.swift | 84 +++++++++++++++--------------- HolyView/ViewController.swift | 18 +++---- HolyViewTests/HolyViewTests.swift | 2 +- 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/HolyView.xcodeproj/project.pbxproj b/HolyView.xcodeproj/project.pbxproj index 9b2c457..6f8980b 100644 --- a/HolyView.xcodeproj/project.pbxproj +++ b/HolyView.xcodeproj/project.pbxproj @@ -175,9 +175,11 @@ D4273E4E1B96E88D007643D3 = { CreatedOnToolsVersion = 6.4; DevelopmentTeam = Q954R38652; + LastSwiftMigration = 0810; }; D4273E631B96E88D007643D3 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0810; TestTargetID = D4273E4E1B96E88D007643D3; }; }; @@ -363,6 +365,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.ruslanaskudriavcevas.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -375,6 +378,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.ruslanaskudriavcevas.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -394,6 +398,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.ruslanaskudriavcevas.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HolyView.app/HolyView"; }; name = Debug; @@ -410,6 +415,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.ruslanaskudriavcevas.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HolyView.app/HolyView"; }; name = Release; diff --git a/HolyView/AppDelegate.swift b/HolyView/AppDelegate.swift index 5d01d14..d38c130 100644 --- a/HolyView/AppDelegate.swift +++ b/HolyView/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/HolyView/HolyView/HolyView.swift b/HolyView/HolyView/HolyView.swift index bcef750..0d66280 100644 --- a/HolyView/HolyView/HolyView.swift +++ b/HolyView/HolyView/HolyView.swift @@ -10,18 +10,18 @@ import UIKit class HolyView: UIView { - typealias CompletionBlock = (dismissed: Bool) -> Void - private var completion: CompletionBlock? + typealias CompletionBlock = (_ dismissed: Bool) -> Void + fileprivate var completion: CompletionBlock? - private let tapRec = UITapGestureRecognizer() - private var holePosition = CGPointZero - private var holeRadius: CGFloat = 0.0 - private var holeSize: CGSize = CGSizeZero { + fileprivate let tapRec = UITapGestureRecognizer() + fileprivate var holePosition = CGPoint.zero + fileprivate var holeRadius: CGFloat = 0.0 + fileprivate var holeSize: CGSize = CGSize.zero { didSet { holeRadius = max(holeSize.height/2, holeSize.width/2) } } - private var bgView = UIView() + fileprivate var bgView = UIView() //MARK: - Public @@ -33,11 +33,11 @@ class HolyView: UIView { /// - Parameter message: (Optional) message for view description /// - Parameter completion: completion block for touch recognizer - class func show(withColor bgColor: UIColor, center: CGPoint, size: CGSize, cornerRadius: CGSize?, message: String?, completion: CompletionBlock) { + class func show(withColor bgColor: UIColor, center: CGPoint, size: CGSize, cornerRadius: CGSize?, message: String?, completion: @escaping CompletionBlock) { - let path = CGPathCreateMutable() + let path = CGMutablePath() if let cr = cornerRadius { - CGPathAddRoundedRect(path, nil, CGRect(x: center.x-size.width/2, y: center.y-size.height/2, width: size.width, height: size.height), cr.width, cr.height) + path.__addRoundedRect(transform: nil, rect: CGRect(x: center.x-size.width/2, y: center.y-size.height/2, width: size.width, height: size.height), cornerWidth: cr.width, cornerHeight: cr.height) } else { CGPathAddArc(path, nil, center.x, center.y, size.width/2, 0.0, 2*3.14, false) } @@ -49,22 +49,22 @@ class HolyView: UIView { view.setup(withColor: bgColor, holePath: path, message: message) - UIView.animateWithDuration(0.3, animations: { () -> Void in + UIView.animate(withDuration: 0.3, animations: { () -> Void in view.bgView.alpha = 0.7 }) } //MARK: - Private - private func setup(withColor bgColor: UIColor, holePath: CGMutablePathRef, message: String?) { - if let window = UIApplication.sharedApplication().keyWindow { + fileprivate func setup(withColor bgColor: UIColor, holePath: CGMutablePath, message: String?) { + if let window = UIApplication.shared.keyWindow { self.frame = window.bounds - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear window.addSubview(self) - self.tapRec.addTarget(self, action: "holyViewTapped") + self.tapRec.addTarget(self, action: #selector(HolyView.holyViewTapped)) self.addGestureRecognizer(self.tapRec) - self.userInteractionEnabled = true + self.isUserInteractionEnabled = true //MASK @@ -75,8 +75,8 @@ class HolyView: UIView { let maskLayer = CAShapeLayer() - CGPathAddRect(holePath, nil, CGRectMake(0, 0, self.bgView.bounds.width, self.bgView.bounds.height)) - maskLayer.backgroundColor = UIColor.blackColor().CGColor + CGPathAddRect(holePath, nil, CGRect(x: 0, y: 0, width: self.bgView.bounds.width, height: self.bgView.bounds.height)) + maskLayer.backgroundColor = UIColor.black.cgColor maskLayer.path = holePath; maskLayer.fillRule = kCAFillRuleEvenOdd @@ -88,61 +88,61 @@ class HolyView: UIView { } } - private func addMessage(message: String) { + fileprivate func addMessage(_ message: String) { let addToTop: Bool = holePosition.y > (self.bounds.height/2) let container = UIView() container.translatesAutoresizingMaskIntoConstraints = false - container.backgroundColor = UIColor.clearColor() + container.backgroundColor = UIColor.clear addSubview(container) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 0.0)) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 0.0)) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: (addToTop ? -(self.bounds.height-(holePosition.y-holeRadius)) : 0.0))) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: (addToTop ? 0.0 : holePosition.y+holeRadius))) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: (addToTop ? -(self.bounds.height-(holePosition.y-holeRadius)) : 0.0))) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: (addToTop ? 0.0 : holePosition.y+holeRadius))) let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false container.addSubview(label) - label.font = UIFont.systemFontOfSize(20.0) - label.textColor = UIColor.whiteColor() - label.textAlignment = .Center + label.font = UIFont.systemFont(ofSize: 20.0) + label.textColor = UIColor.white + label.textAlignment = .center label.numberOfLines = 0 label.text = message - container.addConstraint(NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: container, attribute: .Leading, multiplier: 1.0, constant: 16.0)) - container.addConstraint(NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: container, attribute: .Trailing, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: container, attribute: .Top, multiplier: 1.0, constant: 16.0)) + container.addConstraint(NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) + container.addConstraint(NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 16.0)) let buttonLabel = UILabel() buttonLabel.translatesAutoresizingMaskIntoConstraints = false container.addSubview(buttonLabel) buttonLabel.font = UIFont(name: "HelveticaNeue-Medium", size: 20.0) - buttonLabel.textColor = UIColor.whiteColor() - buttonLabel.textAlignment = .Center + buttonLabel.textColor = UIColor.white + buttonLabel.textAlignment = .center buttonLabel.numberOfLines = 1 buttonLabel.text = "GOT IT!" - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .Leading, relatedBy: .Equal, toItem: container, attribute: .Leading, multiplier: 1.0, constant: 16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .Trailing, relatedBy: .Equal, toItem: container, attribute: .Trailing, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .Bottom, relatedBy: .Equal, toItem: container, attribute: .Bottom, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1.0, constant: 16.0)) - buttonLabel.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 44.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .top, relatedBy: .equal, toItem: label, attribute: .bottom, multiplier: 1.0, constant: 16.0)) + buttonLabel.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 44.0)) } //MARK: - Actions - @objc private func holyViewTapped() { + @objc fileprivate func holyViewTapped() { if let c = completion { - UIView.animateWithDuration(0.3, animations: { () -> Void in + UIView.animate(withDuration: 0.3, animations: { () -> Void in for v in self.subviews { v.alpha = 0 } }, completion: { (complete: Bool) -> Void in self.removeFromSuperview() - c(dismissed: true) + c(true) }) } } @@ -152,7 +152,7 @@ class HolyView: UIView { extension UIBarItem { func center() -> CGPoint { - if let view = self.valueForKey("view") as? UIView { + if let view = self.value(forKey: "view") as? UIView { if let superView = view.superview { return CGPoint( x: superView.frame.origin.x + view.frame.origin.x + view.bounds.width/2, @@ -160,7 +160,7 @@ extension UIBarItem { ) } } - return CGPointZero + return CGPoint.zero } } diff --git a/HolyView/ViewController.swift b/HolyView/ViewController.swift index 0e3fa22..d774730 100644 --- a/HolyView/ViewController.swift +++ b/HolyView/ViewController.swift @@ -19,27 +19,27 @@ class ViewController: UIViewController { //MARK: - Actions - @IBAction func leftBarButtonPressed(sender: UIBarButtonItem) { - HolyView.show(withColor: UIColor.blackColor(), center: sender.center(), size: CGSize(width: 50.0, height: 50.0), cornerRadius: nil, message: "Test message") { (dismissed) -> Void in + @IBAction func leftBarButtonPressed(_ sender: UIBarButtonItem) { + HolyView.show(withColor: UIColor.black, center: sender.center(), size: CGSize(width: 50.0, height: 50.0), cornerRadius: nil, message: "Test message") { (dismissed) -> Void in } } - @IBAction func rightBarButtonPressed(sender: AnyObject) { - HolyView.show(withColor: UIColor.blackColor(), center: favoritesTabButton.center(), size: CGSize(width: 60.0, height: 60.0), cornerRadius: nil, message: "There is no chances to fail!") { (dismissed) -> Void in + @IBAction func rightBarButtonPressed(_ sender: AnyObject) { + HolyView.show(withColor: UIColor.black, center: favoritesTabButton.center(), size: CGSize(width: 60.0, height: 60.0), cornerRadius: nil, message: "There is no chances to fail!") { (dismissed) -> Void in } } - @IBAction func topLeftButtonPressed(sender: UIButton) { - HolyView.show(withColor: UIColor.blackColor(), center: sender.center, size: CGSize(width: 100.0, height: 100.0), cornerRadius: nil, message: "") { (dismissed) -> Void in + @IBAction func topLeftButtonPressed(_ sender: UIButton) { + HolyView.show(withColor: UIColor.black, center: sender.center, size: CGSize(width: 100.0, height: 100.0), cornerRadius: nil, message: "") { (dismissed) -> Void in } } - @IBAction func centerButtonPressed(sender: AnyObject) { - HolyView.show(withColor: UIColor.redColor(), center: sender.center, size: CGSize(width: 100.0, height: 60.0), cornerRadius: CGSizeZero, message: "Try to stop me!") { (dismissed) -> Void in - HolyView.show(withColor: UIColor.redColor(), center: sender.center, size: CGSize(width: 100.0, height: 60.0), cornerRadius: CGSize(width: 4.0, height: 4.0), message: "Rounded corners") { (dismissed) -> Void in + @IBAction func centerButtonPressed(_ sender: AnyObject) { + HolyView.show(withColor: UIColor.red, center: sender.center, size: CGSize(width: 100.0, height: 60.0), cornerRadius: CGSize.zero, message: "Try to stop me!") { (dismissed) -> Void in + HolyView.show(withColor: UIColor.red, center: sender.center, size: CGSize(width: 100.0, height: 60.0), cornerRadius: CGSize(width: 4.0, height: 4.0), message: "Rounded corners") { (dismissed) -> Void in } } diff --git a/HolyViewTests/HolyViewTests.swift b/HolyViewTests/HolyViewTests.swift index 73e8a12..dd52f0d 100644 --- a/HolyViewTests/HolyViewTests.swift +++ b/HolyViewTests/HolyViewTests.swift @@ -28,7 +28,7 @@ class HolyViewTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } } From ca475ccb0336626e1e0af4ea632ec1d951c0772d Mon Sep 17 00:00:00 2001 From: Doug Harper Date: Thu, 17 Nov 2016 17:28:36 -0600 Subject: [PATCH 2/2] Converted to Swift 3 Used the Xcode conversion tool to upgrade to Swift 3 --- HolyView.xcodeproj/project.pbxproj | 7 +- HolyView/HolyView/HolyView.swift | 268 ++++++++++++++--------------- 2 files changed, 140 insertions(+), 135 deletions(-) diff --git a/HolyView.xcodeproj/project.pbxproj b/HolyView.xcodeproj/project.pbxproj index 6f8980b..8e523c7 100644 --- a/HolyView.xcodeproj/project.pbxproj +++ b/HolyView.xcodeproj/project.pbxproj @@ -169,7 +169,7 @@ attributes = { LastSwiftMigration = 0700; LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0810; ORGANIZATIONNAME = "Ruslanas Kudriavcevas"; TargetAttributes = { D4273E4E1B96E88D007643D3 = { @@ -285,8 +285,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -331,8 +333,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -351,6 +355,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.4; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; diff --git a/HolyView/HolyView/HolyView.swift b/HolyView/HolyView/HolyView.swift index 0d66280..3e91e32 100644 --- a/HolyView/HolyView/HolyView.swift +++ b/HolyView/HolyView/HolyView.swift @@ -9,159 +9,159 @@ import UIKit class HolyView: UIView { + + typealias CompletionBlock = (_ dismissed: Bool) -> Void + fileprivate var completion: CompletionBlock? + + fileprivate let tapRec = UITapGestureRecognizer() + fileprivate var holePosition = CGPoint.zero + fileprivate var holeRadius: CGFloat = 0.0 + fileprivate var holeSize: CGSize = CGSize.zero { + didSet { + holeRadius = max(holeSize.height/2, holeSize.width/2) + } + } + fileprivate var bgView = UIView() + + //MARK: - Public + + /// Custom view with transparent circle/rounded rectangle hole and title/button subview + /// - Parameter bgColor: color for semi transparent background + /// - Parameter center: center position of rounded rectangle + /// - Parameter size: widht and height of rectangle (for circle radius using half of width) + /// - Parameter cornerRadius: (Optional) using for rectangle corner radius (set nil for circle) + /// - Parameter message: (Optional) message for view description + /// - Parameter completion: completion block for touch recognizer + + class func show(withColor bgColor: UIColor, center: CGPoint, size: CGSize, cornerRadius: CGSize?, message: String?, completion: @escaping CompletionBlock) { - typealias CompletionBlock = (_ dismissed: Bool) -> Void - fileprivate var completion: CompletionBlock? - - fileprivate let tapRec = UITapGestureRecognizer() - fileprivate var holePosition = CGPoint.zero - fileprivate var holeRadius: CGFloat = 0.0 - fileprivate var holeSize: CGSize = CGSize.zero { - didSet { - holeRadius = max(holeSize.height/2, holeSize.width/2) - } + let path = CGMutablePath() + if let cr = cornerRadius { + path.__addRoundedRect(transform: nil, rect: CGRect(x: center.x-size.width/2, y: center.y-size.height/2, width: size.width, height: size.height), cornerWidth: cr.width, cornerHeight: cr.height) + } else { + path.addArc(center: CGPoint.init(x: center.x, y: center.y), radius: size.width/2, startAngle: 0.0, endAngle: 2*3.14, clockwise: false) } - fileprivate var bgView = UIView() - //MARK: - Public + let view = HolyView() + view.completion = completion + view.holePosition = center + view.holeSize = size - /// Custom view with transparent circle/rounded rectangle hole and title/button subview - /// - Parameter bgColor: color for semi transparent background - /// - Parameter center: center position of rounded rectangle - /// - Parameter size: widht and height of rectangle (for circle radius using half of width) - /// - Parameter cornerRadius: (Optional) using for rectangle corner radius (set nil for circle) - /// - Parameter message: (Optional) message for view description - /// - Parameter completion: completion block for touch recognizer + view.setup(withColor: bgColor, holePath: path, message: message) - class func show(withColor bgColor: UIColor, center: CGPoint, size: CGSize, cornerRadius: CGSize?, message: String?, completion: @escaping CompletionBlock) { - - let path = CGMutablePath() - if let cr = cornerRadius { - path.__addRoundedRect(transform: nil, rect: CGRect(x: center.x-size.width/2, y: center.y-size.height/2, width: size.width, height: size.height), cornerWidth: cr.width, cornerHeight: cr.height) - } else { - CGPathAddArc(path, nil, center.x, center.y, size.width/2, 0.0, 2*3.14, false) - } - - let view = HolyView() - view.completion = completion - view.holePosition = center - view.holeSize = size - - view.setup(withColor: bgColor, holePath: path, message: message) - - UIView.animate(withDuration: 0.3, animations: { () -> Void in - view.bgView.alpha = 0.7 - }) + UIView.animate(withDuration: 0.3, animations: { () -> Void in + view.bgView.alpha = 0.7 + }) + } + + //MARK: - Private + + fileprivate func setup(withColor bgColor: UIColor, holePath: CGMutablePath, message: String?) { + if let window = UIApplication.shared.keyWindow { + self.frame = window.bounds + self.backgroundColor = UIColor.clear + window.addSubview(self) + + self.tapRec.addTarget(self, action: #selector(HolyView.holyViewTapped)) + self.addGestureRecognizer(self.tapRec) + self.isUserInteractionEnabled = true + + //MASK + + self.bgView.backgroundColor = bgColor + self.bgView.frame = self.bounds + self.bgView.alpha = 0.0 + self.addSubview(self.bgView) + + let maskLayer = CAShapeLayer() + + holePath.addRect(CGRect(x: 0, y: 0, width: self.bgView.bounds.width, height: self.bgView.bounds.height)) + maskLayer.backgroundColor = UIColor.black.cgColor + maskLayer.path = holePath; + maskLayer.fillRule = kCAFillRuleEvenOdd + + self.bgView.layer.mask = maskLayer + self.bgView.clipsToBounds = true + if let message = message { + self.addMessage(message) + } } + } + + fileprivate func addMessage(_ message: String) { + let addToTop: Bool = holePosition.y > (self.bounds.height/2) - //MARK: - Private + let container = UIView() + container.translatesAutoresizingMaskIntoConstraints = false + container.backgroundColor = UIColor.clear + addSubview(container) - fileprivate func setup(withColor bgColor: UIColor, holePath: CGMutablePath, message: String?) { - if let window = UIApplication.shared.keyWindow { - self.frame = window.bounds - self.backgroundColor = UIColor.clear - window.addSubview(self) - - self.tapRec.addTarget(self, action: #selector(HolyView.holyViewTapped)) - self.addGestureRecognizer(self.tapRec) - self.isUserInteractionEnabled = true - - //MASK - - self.bgView.backgroundColor = bgColor - self.bgView.frame = self.bounds - self.bgView.alpha = 0.0 - self.addSubview(self.bgView) - - let maskLayer = CAShapeLayer() - - CGPathAddRect(holePath, nil, CGRect(x: 0, y: 0, width: self.bgView.bounds.width, height: self.bgView.bounds.height)) - maskLayer.backgroundColor = UIColor.black.cgColor - maskLayer.path = holePath; - maskLayer.fillRule = kCAFillRuleEvenOdd - - self.bgView.layer.mask = maskLayer - self.bgView.clipsToBounds = true - if let message = message { - self.addMessage(message) - } - } - } + self.addConstraint(NSLayoutConstraint(item: container, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: (addToTop ? -(self.bounds.height-(holePosition.y-holeRadius)) : 0.0))) + self.addConstraint(NSLayoutConstraint(item: container, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: (addToTop ? 0.0 : holePosition.y+holeRadius))) - fileprivate func addMessage(_ message: String) { - let addToTop: Bool = holePosition.y > (self.bounds.height/2) - - let container = UIView() - container.translatesAutoresizingMaskIntoConstraints = false - container.backgroundColor = UIColor.clear - addSubview(container) - - self.addConstraint(NSLayoutConstraint(item: container, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: (addToTop ? -(self.bounds.height-(holePosition.y-holeRadius)) : 0.0))) - self.addConstraint(NSLayoutConstraint(item: container, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: (addToTop ? 0.0 : holePosition.y+holeRadius))) - - let label = UILabel() - label.translatesAutoresizingMaskIntoConstraints = false - container.addSubview(label) - - label.font = UIFont.systemFont(ofSize: 20.0) - label.textColor = UIColor.white - label.textAlignment = .center - label.numberOfLines = 0 - label.text = message - - container.addConstraint(NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) - container.addConstraint(NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 16.0)) - - let buttonLabel = UILabel() - buttonLabel.translatesAutoresizingMaskIntoConstraints = false - container.addSubview(buttonLabel) - - buttonLabel.font = UIFont(name: "HelveticaNeue-Medium", size: 20.0) - buttonLabel.textColor = UIColor.white - buttonLabel.textAlignment = .center - buttonLabel.numberOfLines = 1 - buttonLabel.text = "GOT IT!" - - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: -16.0)) - container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .top, relatedBy: .equal, toItem: label, attribute: .bottom, multiplier: 1.0, constant: 16.0)) - buttonLabel.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 44.0)) - } + let label = UILabel() + label.translatesAutoresizingMaskIntoConstraints = false + container.addSubview(label) + + label.font = UIFont.systemFont(ofSize: 20.0) + label.textColor = UIColor.white + label.textAlignment = .center + label.numberOfLines = 0 + label.text = message + + container.addConstraint(NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) + container.addConstraint(NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 16.0)) - //MARK: - Actions + let buttonLabel = UILabel() + buttonLabel.translatesAutoresizingMaskIntoConstraints = false + container.addSubview(buttonLabel) - @objc fileprivate func holyViewTapped() { - if let c = completion { - UIView.animate(withDuration: 0.3, animations: { () -> Void in - for v in self.subviews { - v.alpha = 0 - } - }, completion: { (complete: Bool) -> Void in - self.removeFromSuperview() - c(true) - }) + buttonLabel.font = UIFont(name: "HelveticaNeue-Medium", size: 20.0) + buttonLabel.textColor = UIColor.white + buttonLabel.textAlignment = .center + buttonLabel.numberOfLines = 1 + buttonLabel.text = "GOT IT!" + + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: -16.0)) + container.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .top, relatedBy: .equal, toItem: label, attribute: .bottom, multiplier: 1.0, constant: 16.0)) + buttonLabel.addConstraint(NSLayoutConstraint(item: buttonLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 44.0)) + } + + //MARK: - Actions + + @objc fileprivate func holyViewTapped() { + if let c = completion { + UIView.animate(withDuration: 0.3, animations: { () -> Void in + for v in self.subviews { + v.alpha = 0 } + }, completion: { (complete: Bool) -> Void in + self.removeFromSuperview() + c(true) + }) } + } } //MARK: - Extensions extension UIBarItem { - func center() -> CGPoint { - if let view = self.value(forKey: "view") as? UIView { - if let superView = view.superview { - return CGPoint( - x: superView.frame.origin.x + view.frame.origin.x + view.bounds.width/2, - y: superView.frame.origin.y + view.frame.origin.y + view.bounds.height/2 - ) - } - } - return CGPoint.zero + func center() -> CGPoint { + if let view = self.value(forKey: "view") as? UIView { + if let superView = view.superview { + return CGPoint( + x: superView.frame.origin.x + view.frame.origin.x + view.bounds.width/2, + y: superView.frame.origin.y + view.frame.origin.y + view.bounds.height/2 + ) + } } + return CGPoint.zero + } }