diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 2663a24..cfd5238 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -27,19 +27,19 @@ class ViewController: UIViewController { } @IBAction func customDidTap(_ sender: UIButton) { - NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.custom(image: UIImage(named: "love")!), - title: "参考になった", - message: "フィードバックをありがとう\nございました。")) + NativePopup.show(image: UIImage(named: "love")!, + title: "参考になった", + message: "フィードバックをありがとう\nございました。") } @IBAction func goodDidTap(_ sender: UIButton) { - NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.good, - title: "Helpful", - message: "Thanks for your feedback.")) + NativePopup.show(image: NativePopup.FeedbackImage.good, + title: "Helpful", + message: "Thanks for your feedback.") } @IBAction func badDidTap(_ sender: UIButton) { - NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.bad, - title: "Not Helpful", - message: "Thanks for your feedback.")) + NativePopup.show(image: NativePopup.FeedbackImage.bad, + title: "Not Helpful", + message: "Thanks for your feedback.") } } diff --git a/NativePopup.xcodeproj/project.pbxproj b/NativePopup.xcodeproj/project.pbxproj index 1a06ee2..6716de3 100644 --- a/NativePopup.xcodeproj/project.pbxproj +++ b/NativePopup.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ FECA87DA1EAC87B700D07CB1 /* NativePopup.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FE85338B1EAC7C92004BB756 /* NativePopup.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; FECA87DF1EAC94B300D07CB1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */; }; FECA87E11EACC77800D07CB1 /* NativePopup.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */; }; + FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -58,6 +59,7 @@ FECA87D71EAC7D9E00D07CB1 /* NativePopup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativePopup.swift; sourceTree = ""; }; FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativePopup.extension.swift; sourceTree = ""; }; + FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageConvertible.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,6 +106,7 @@ FE85338F1EAC7C92004BB756 /* Info.plist */, FECA87D71EAC7D9E00D07CB1 /* NativePopup.swift */, FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */, + FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */, FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */, ); path = NativePopup; @@ -240,6 +243,7 @@ buildActionMask = 2147483647; files = ( FECA87E11EACC77800D07CB1 /* NativePopup.extension.swift in Sources */, + FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */, FECA87D81EAC7D9E00D07CB1 /* NativePopup.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/NativePopup/Assets.xcassets/feedback/Contents.json b/NativePopup/Assets.xcassets/feedback/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/NativePopup/Assets.xcassets/feedback/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/NativePopup/Assets.xcassets/bad.imageset/Contents.json b/NativePopup/Assets.xcassets/feedback/feedback_bad.imageset/Contents.json similarity index 100% rename from NativePopup/Assets.xcassets/bad.imageset/Contents.json rename to NativePopup/Assets.xcassets/feedback/feedback_bad.imageset/Contents.json diff --git a/NativePopup/Assets.xcassets/bad.imageset/bad.pdf b/NativePopup/Assets.xcassets/feedback/feedback_bad.imageset/bad.pdf similarity index 100% rename from NativePopup/Assets.xcassets/bad.imageset/bad.pdf rename to NativePopup/Assets.xcassets/feedback/feedback_bad.imageset/bad.pdf diff --git a/NativePopup/Assets.xcassets/good.imageset/Contents.json b/NativePopup/Assets.xcassets/feedback/feedback_good.imageset/Contents.json similarity index 100% rename from NativePopup/Assets.xcassets/good.imageset/Contents.json rename to NativePopup/Assets.xcassets/feedback/feedback_good.imageset/Contents.json diff --git a/NativePopup/Assets.xcassets/good.imageset/good.pdf b/NativePopup/Assets.xcassets/feedback/feedback_good.imageset/good.pdf similarity index 100% rename from NativePopup/Assets.xcassets/good.imageset/good.pdf rename to NativePopup/Assets.xcassets/feedback/feedback_good.imageset/good.pdf diff --git a/NativePopup/NativePopup.extension.swift b/NativePopup/NativePopup.extension.swift index c31263f..f0404e4 100644 --- a/NativePopup/NativePopup.extension.swift +++ b/NativePopup/NativePopup.extension.swift @@ -9,33 +9,11 @@ import Foundation extension NativePopup { - public enum Image { + public enum FeedbackImage: String, HavingUIImageName { case good, - bad, - custom(image: UIImage) - - var image: UIImage { - switch self { - case .good: return type(of: self).createImage(named: "good") - case .bad: return type(of: self).createImage(named: "bad") - case .custom(let image): return image - } - } - - private static func createImage(named: String) -> UIImage { - return UIImage.init(named: named, in: Bundle(for: NativePopup.self), compatibleWith: nil)! - } - } - - public struct Info { - public let image: Image - public let title: String - public let message: String? - public init(image: Image, title: String, message: String?) { - self.image = image - self.title = title - self.message = message - } + bad + + public var imageName: String { return "feedback_\(rawValue)" } } } diff --git a/NativePopup/NativePopup.swift b/NativePopup/NativePopup.swift index ba98720..f93dafd 100644 --- a/NativePopup/NativePopup.swift +++ b/NativePopup/NativePopup.swift @@ -10,12 +10,11 @@ import Foundation import UIKit public class NativePopup: UIView { - private static let keyWindow = UIApplication.shared.keyWindow! - private weak static var currentView: NativePopup? + fileprivate weak static var currentView: NativePopup? - public static func show(info: Info) { - let view = NativePopup(info: info) + public static func show(image: UIImageConvertible, title: String, message: String?) { + let view = NativePopup(image: image, title: title, message: message) view.show() currentView = view } @@ -24,7 +23,7 @@ public class NativePopup: UIView { fatalError("should not be called") } - private init(info: Info) { + private init(image: UIImageConvertible, title: String, message: String?) { super.init(frame: CGRect.zero) layer.cornerRadius = 8 @@ -32,22 +31,21 @@ public class NativePopup: UIView { isUserInteractionEnabled = false - let image = info.image.image + let image = image.image let imageView = UIImageView(image: image) assert(image.size.width == image.size.height, "Aspect ratio should be 1:1.") imageView.contentMode = .scaleAspectFit - if case .custom = info.image { - imageView.layer.cornerRadius = 6 - imageView.clipsToBounds = true - } + + imageView.layer.cornerRadius = 6 + imageView.clipsToBounds = true let titleLabel = UILabel() - titleLabel.text = info.title + titleLabel.text = title // not dynamic size titleLabel.font = UIFont.boldSystemFont(ofSize: 20) let messageLabel = UILabel() - messageLabel.text = info.message + messageLabel.text = message messageLabel.font = UIFont.systemFont(ofSize: 15) [titleLabel, messageLabel].forEach { @@ -80,7 +78,7 @@ public class NativePopup: UIView { titleLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: sideSpace).isActive = true let bottomSpace: CGFloat = 28 - if info.message?.isEmpty ?? true { + if message?.isEmpty ?? true { titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -bottomSpace).isActive = true } else { addSubview(messageLabel) @@ -96,7 +94,7 @@ public class NativePopup: UIView { private func show() { let window = type(of: self).keyWindow // TODO: need to remove? - for v in window.subviews where v is NativePopup { v.removeFromSuperview() } + type(of: self).currentView?.removeFromSuperview() window.addSubview(self) widthAnchor.constraint(equalToConstant: 250).isActive = true centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true diff --git a/NativePopup/UIImageConvertible.swift b/NativePopup/UIImageConvertible.swift new file mode 100644 index 0000000..24a5c47 --- /dev/null +++ b/NativePopup/UIImageConvertible.swift @@ -0,0 +1,26 @@ +// +// UIImageConvertible.swift +// NativePopup +// +// Created by mono on 2017/04/23. +// Copyright © 2017 mono. All rights reserved. +// + +import Foundation +import UIKit + +public protocol UIImageConvertible { + var image: UIImage { get } +} + +extension UIImage: UIImageConvertible { + public var image: UIImage { return self } +} + +public protocol HavingUIImageName: UIImageConvertible { + var imageName: String { get } +} + +extension HavingUIImageName { + public var image: UIImage { return UIImage.init(named: imageName, in: Bundle(for: NativePopup.self), compatibleWith: nil)! } +}