Repository: beryu/CustomizableActionSheet Branch: master Commit: 37ac2ea69873 Files: 22 Total size: 68.9 KB Directory structure: gitextract_6e4rxpog/ ├── .gitignore ├── CustomizableActionSheet/ │ ├── CustomizableActionSheet.h │ ├── CustomizableActionSheet.swift │ └── Info.plist ├── CustomizableActionSheet.podspec ├── CustomizableActionSheet.xcodeproj/ │ ├── project.pbxproj │ ├── project.xcworkspace/ │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata/ │ │ └── IDEWorkspaceChecks.plist │ └── xcshareddata/ │ └── xcschemes/ │ └── CustomizableActionSheet.xcscheme ├── CustomizableActionSheetDemo/ │ ├── AppDelegate.swift │ ├── Assets.xcassets/ │ │ ├── AppIcon.appiconset/ │ │ │ └── Contents.json │ │ ├── Brand Assets.launchimage/ │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj/ │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── Info.plist │ ├── SampleView.swift │ ├── SampleView.xib │ └── ViewController.swift ├── LICENSE ├── README.md └── Source/ └── CustomizableActionSheet.swift ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ ./build/ ./build-test/ xcuserdata/ *.xccheckout *.gcno ================================================ FILE: CustomizableActionSheet/CustomizableActionSheet.h ================================================ // // CustomizableActionSheet.h // CustomizableActionSheet // // Created by beryu on 2016/04/22. // Copyright © 2016年 blk. All rights reserved. // #import //! Project version number for CustomizableActionSheet. FOUNDATION_EXPORT double CustomizableActionSheetVersionNumber; //! Project version string for CustomizableActionSheet. FOUNDATION_EXPORT const unsigned char CustomizableActionSheetVersionString[]; // In this header, you should import all the public headers of your framework using statements like #import ================================================ FILE: CustomizableActionSheet/CustomizableActionSheet.swift ================================================ // // CustomizableActionSheet.swift // CustomizableActionSheet // // Created by Ryuta Kibe on 2015/12/22. // Copyright 2015 blk. All rights reserved. // import UIKit @objc public enum CustomizableActionSheetItemType: Int { case button case view } // Can't define CustomizableActionSheetItem as struct because Obj-C can't see struct definition. public class CustomizableActionSheetItem: NSObject { // MARK: - Public properties public var type: CustomizableActionSheetItemType = .button public var height: CGFloat = CustomizableActionSheetItem.kDefaultHeight public static let kDefaultHeight: CGFloat = 44 // type = .View public var view: UIView? // type = .Button public var label: String? public var textColor: UIColor = UIColor(red: 0, green: 0.47, blue: 1.0, alpha: 1.0) public var backgroundColor: UIColor = UIColor.white public var font: UIFont? = nil public var selectAction: ((_ actionSheet: CustomizableActionSheet) -> Void)? = nil // MARK: - Private properties fileprivate var element: UIView? = nil public convenience init(type: CustomizableActionSheetItemType, height: CGFloat = CustomizableActionSheetItem.kDefaultHeight) { self.init() self.type = type self.height = height } } private class ActionSheetItemView: UIView { var subview: UIView? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! self.setting() } override init(frame: CGRect) { super.init(frame: frame) self.setting() } init() { super.init(frame: CGRect.zero) self.setting() } func setting() { self.clipsToBounds = true } override func addSubview(_ view: UIView) { super.addSubview(view) self.subview = view } override func layoutSubviews() { super.layoutSubviews() if let subview = self.subview { subview.frame = self.bounds } } } @objc public enum CustomizableActionSheetPosition: Int { case bottom case top } public class CustomizableActionSheet: NSObject { // MARK: - Private properties private static var actionSheets = [CustomizableActionSheet]() private static let kMarginSide: CGFloat = 8 private static let kItemInterval: CGFloat = 8 private static let kMarginTop: CGFloat = 20 private var items: [CustomizableActionSheetItem]? private let maskView = UIView() private let itemContainerView = UIView() private var closeBlock: (() -> Void)? // MARK: - Public properties public var defaultCornerRadius: CGFloat = 4 public var position: CustomizableActionSheetPosition = .bottom public func showInView(_ targetView: UIView, items: [CustomizableActionSheetItem], closeBlock: (() -> Void)? = nil) { // Save instance to reaction until closing this sheet CustomizableActionSheet.actionSheets.append(self) let targetBounds = targetView.bounds // Save closeBlock self.closeBlock = closeBlock // mask view let maskViewTapGesture = UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.maskViewWasTapped)) self.maskView.addGestureRecognizer(maskViewTapGesture) self.maskView.frame = targetBounds self.maskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5) targetView.addSubview(self.maskView) // set items for subview in self.itemContainerView.subviews { subview.removeFromSuperview() } var currentPosition: CGFloat = 0 let safeAreaLeft: CGFloat let safeAreaWidth: CGFloat let safeAreaTop: CGFloat let safeAreaBottom: CGFloat if #available(iOS 11.0, *) { safeAreaLeft = targetView.safeAreaInsets.left safeAreaWidth = targetView.safeAreaLayoutGuide.layoutFrame.width safeAreaTop = targetView.safeAreaInsets.top safeAreaBottom = targetView.safeAreaInsets.bottom } else { safeAreaLeft = 0 safeAreaWidth = targetBounds.width safeAreaTop = CustomizableActionSheet.kMarginTop safeAreaBottom = 0 } var availableHeight = targetBounds.height - safeAreaTop - safeAreaBottom // Calculate height of items for item in items { availableHeight = availableHeight - item.height - CustomizableActionSheet.kItemInterval } for item in items { // Apply height of items if availableHeight < 0 { let reduceNum = min(item.height, -availableHeight) item.height -= reduceNum availableHeight += reduceNum if item.height <= 0 { availableHeight += CustomizableActionSheet.kItemInterval continue } } // Add views switch (item.type) { case .button: let button = UIButton() button.layer.cornerRadius = defaultCornerRadius button.frame = CGRect( x: CustomizableActionSheet.kMarginSide, y: currentPosition, width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2), height: item.height) button.setTitle(item.label, for: UIControl.State()) button.backgroundColor = item.backgroundColor button.setTitleColor(item.textColor, for: UIControl.State()) if let font = item.font { button.titleLabel?.font = font } if let _ = item.selectAction { button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.buttonWasTapped(_:)))) } item.element = button self.itemContainerView.addSubview(button) currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval case .view: if let view = item.view { let containerView = ActionSheetItemView(frame: CGRect( x: CustomizableActionSheet.kMarginSide, y: currentPosition, width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2), height: item.height)) containerView.layer.cornerRadius = defaultCornerRadius containerView.addSubview(view) view.frame = view.bounds self.itemContainerView.addSubview(containerView) item.element = view currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval } } } let positionX: CGFloat = safeAreaLeft var positionY: CGFloat = targetBounds.minY + targetBounds.height - currentPosition - safeAreaBottom var moveY: CGFloat = positionY if self.position == .top { positionY = CustomizableActionSheet.kItemInterval moveY = -currentPosition } self.itemContainerView.frame = CGRect( x: positionX, y: positionY, width: safeAreaWidth, height: currentPosition) self.items = items // Show animation self.maskView.alpha = 0 targetView.addSubview(self.itemContainerView) self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY) UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { () -> Void in self.maskView.alpha = 1 self.itemContainerView.transform = CGAffineTransform.identity }, completion: nil) } public func dismiss() { guard let targetView = self.itemContainerView.superview else { return } // Hide animation self.maskView.alpha = 1 var moveY: CGFloat = targetView.bounds.height - self.itemContainerView.frame.origin.y if self.position == .top { moveY = -self.itemContainerView.frame.height } UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { () -> Void in self.maskView.alpha = 0 self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY) }) { (result: Bool) -> Void in // Remove views self.itemContainerView.removeFromSuperview() self.maskView.removeFromSuperview() // Remove this instance for i in 0 ..< CustomizableActionSheet.actionSheets.count { if CustomizableActionSheet.actionSheets[i] == self { CustomizableActionSheet.actionSheets.remove(at: i) break } } self.closeBlock?() } } // MARK: - Private methods @objc private func maskViewWasTapped() { self.dismiss() } @objc private func buttonWasTapped(_ sender: AnyObject) { guard let items = self.items else { return } for item in items { guard let element = item.element, let gestureRecognizer = sender as? UITapGestureRecognizer else { continue } if element == gestureRecognizer.view { item.selectAction?(self) } } } } ================================================ FILE: CustomizableActionSheet/Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType FMWK CFBundleShortVersionString 1.0.8 CFBundleSignature ???? CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass ================================================ FILE: CustomizableActionSheet.podspec ================================================ Pod::Spec.new do |s| s.name = "CustomizableActionSheet" s.version = "1.2.3" s.summary = "Action sheet allows including your custom views and buttons." s.homepage = "https://github.com/beryu/CustomizableActionSheet" s.screenshots = "https://github.com/beryu/CustomizableActionSheet/raw/master/assets/screenshot1.png" s.license = { :type => "MIT", :file => "LICENSE" } s.author = { "Ryuta Kibe" => "beryu@blk.jp" } s.social_media_url = "https://twitter.com/beryu" s.platform = :ios s.ios.deployment_target = "8.0" s.source = { :git => "https://github.com/beryu/CustomizableActionSheet.git", :tag => s.version } s.source_files = "Source/*" s.requires_arc = true end ================================================ FILE: CustomizableActionSheet.xcodeproj/project.pbxproj ================================================ // !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 46; objects = { /* Begin PBXBuildFile section */ 87B8409D1C27F3C100C84DCE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B8409C1C27F3C100C84DCE /* AppDelegate.swift */; }; 87B8409F1C27F3C100C84DCE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B8409E1C27F3C100C84DCE /* ViewController.swift */; }; 87B840A21C27F3C100C84DCE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 87B840A01C27F3C100C84DCE /* Main.storyboard */; }; 87B840A41C27F3C100C84DCE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 87B840A31C27F3C100C84DCE /* Assets.xcassets */; }; 87B840A71C27F3C100C84DCE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 87B840A51C27F3C100C84DCE /* LaunchScreen.storyboard */; }; 8825BEE41CCA5E4B00F7A17D /* CustomizableActionSheet.h in Headers */ = {isa = PBXBuildFile; fileRef = 8825BEE31CCA5E4B00F7A17D /* CustomizableActionSheet.h */; settings = {ATTRIBUTES = (Public, ); }; }; 8825BEE81CCA5E4B00F7A17D /* CustomizableActionSheet.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8825BEE11CCA5E4B00F7A17D /* CustomizableActionSheet.framework */; }; 8825BEE91CCA5E4B00F7A17D /* CustomizableActionSheet.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 8825BEE11CCA5E4B00F7A17D /* CustomizableActionSheet.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 8825BEEF1CCA5E8400F7A17D /* CustomizableActionSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8825BEEE1CCA5E8400F7A17D /* CustomizableActionSheet.swift */; }; 88FFDA211C30269D00FCBFF3 /* SampleView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 88FFDA201C30269D00FCBFF3 /* SampleView.xib */; }; 88FFDA231C3031F200FCBFF3 /* SampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88FFDA221C3031F200FCBFF3 /* SampleView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ 8825BEE61CCA5E4B00F7A17D /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 87B840911C27F3C100C84DCE /* Project object */; proxyType = 1; remoteGlobalIDString = 8825BEE01CCA5E4B00F7A17D; remoteInfo = CustomizableActionSheet; }; /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ 8825BEED1CCA5E4B00F7A17D /* Embed Frameworks */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; dstPath = ""; dstSubfolderSpec = 10; files = ( 8825BEE91CCA5E4B00F7A17D /* CustomizableActionSheet.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ 87B840991C27F3C100C84DCE /* CustomizableActionSheetDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CustomizableActionSheetDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 87B8409C1C27F3C100C84DCE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 87B8409E1C27F3C100C84DCE /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 87B840A11C27F3C100C84DCE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 87B840A31C27F3C100C84DCE /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 87B840A61C27F3C100C84DCE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 87B840A81C27F3C100C84DCE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 8825BEE11CCA5E4B00F7A17D /* CustomizableActionSheet.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CustomizableActionSheet.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 8825BEE31CCA5E4B00F7A17D /* CustomizableActionSheet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CustomizableActionSheet.h; sourceTree = ""; }; 8825BEE51CCA5E4B00F7A17D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 8825BEEE1CCA5E8400F7A17D /* CustomizableActionSheet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomizableActionSheet.swift; sourceTree = ""; }; 88FFDA201C30269D00FCBFF3 /* SampleView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SampleView.xib; sourceTree = ""; }; 88FFDA221C3031F200FCBFF3 /* SampleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SampleView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 87B840961C27F3C100C84DCE /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 8825BEE81CCA5E4B00F7A17D /* CustomizableActionSheet.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; 8825BEDD1CCA5E4B00F7A17D /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 87B840901C27F3C100C84DCE = { isa = PBXGroup; children = ( 87B8409B1C27F3C100C84DCE /* CustomizableActionSheetDemo */, 8825BEE21CCA5E4B00F7A17D /* CustomizableActionSheet */, 87B8409A1C27F3C100C84DCE /* Products */, ); sourceTree = ""; }; 87B8409A1C27F3C100C84DCE /* Products */ = { isa = PBXGroup; children = ( 87B840991C27F3C100C84DCE /* CustomizableActionSheetDemo.app */, 8825BEE11CCA5E4B00F7A17D /* CustomizableActionSheet.framework */, ); name = Products; sourceTree = ""; }; 87B8409B1C27F3C100C84DCE /* CustomizableActionSheetDemo */ = { isa = PBXGroup; children = ( 87B8409C1C27F3C100C84DCE /* AppDelegate.swift */, 87B8409E1C27F3C100C84DCE /* ViewController.swift */, 87B840A01C27F3C100C84DCE /* Main.storyboard */, 87B840A31C27F3C100C84DCE /* Assets.xcassets */, 87B840A51C27F3C100C84DCE /* LaunchScreen.storyboard */, 87B840A81C27F3C100C84DCE /* Info.plist */, 88FFDA201C30269D00FCBFF3 /* SampleView.xib */, 88FFDA221C3031F200FCBFF3 /* SampleView.swift */, ); path = CustomizableActionSheetDemo; sourceTree = ""; }; 8825BEE21CCA5E4B00F7A17D /* CustomizableActionSheet */ = { isa = PBXGroup; children = ( 8825BEEE1CCA5E8400F7A17D /* CustomizableActionSheet.swift */, 8825BEE31CCA5E4B00F7A17D /* CustomizableActionSheet.h */, 8825BEE51CCA5E4B00F7A17D /* Info.plist */, ); path = CustomizableActionSheet; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ 8825BEDE1CCA5E4B00F7A17D /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( 8825BEE41CCA5E4B00F7A17D /* CustomizableActionSheet.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ 87B840981C27F3C100C84DCE /* CustomizableActionSheetDemo */ = { isa = PBXNativeTarget; buildConfigurationList = 87B840AB1C27F3C100C84DCE /* Build configuration list for PBXNativeTarget "CustomizableActionSheetDemo" */; buildPhases = ( 87B840951C27F3C100C84DCE /* Sources */, 87B840961C27F3C100C84DCE /* Frameworks */, 87B840971C27F3C100C84DCE /* Resources */, 8825BEED1CCA5E4B00F7A17D /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( 8825BEE71CCA5E4B00F7A17D /* PBXTargetDependency */, ); name = CustomizableActionSheetDemo; productName = CustomizableActionSheet; productReference = 87B840991C27F3C100C84DCE /* CustomizableActionSheetDemo.app */; productType = "com.apple.product-type.application"; }; 8825BEE01CCA5E4B00F7A17D /* CustomizableActionSheet */ = { isa = PBXNativeTarget; buildConfigurationList = 8825BEEA1CCA5E4B00F7A17D /* Build configuration list for PBXNativeTarget "CustomizableActionSheet" */; buildPhases = ( 8825BEDC1CCA5E4B00F7A17D /* Sources */, 8825BEDD1CCA5E4B00F7A17D /* Frameworks */, 8825BEDE1CCA5E4B00F7A17D /* Headers */, 8825BEDF1CCA5E4B00F7A17D /* Resources */, ); buildRules = ( ); dependencies = ( ); name = CustomizableActionSheet; productName = CustomizableActionSheet; productReference = 8825BEE11CCA5E4B00F7A17D /* CustomizableActionSheet.framework */; productType = "com.apple.product-type.framework"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 87B840911C27F3C100C84DCE /* Project object */ = { isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; LastUpgradeCheck = 0800; ORGANIZATIONNAME = blk; TargetAttributes = { 87B840981C27F3C100C84DCE = { CreatedOnToolsVersion = 7.2; LastSwiftMigration = 0800; }; 8825BEE01CCA5E4B00F7A17D = { CreatedOnToolsVersion = 7.3; LastSwiftMigration = 0900; }; }; }; buildConfigurationList = 87B840941C27F3C100C84DCE /* Build configuration list for PBXProject "CustomizableActionSheet" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, Base, ); mainGroup = 87B840901C27F3C100C84DCE; productRefGroup = 87B8409A1C27F3C100C84DCE /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( 87B840981C27F3C100C84DCE /* CustomizableActionSheetDemo */, 8825BEE01CCA5E4B00F7A17D /* CustomizableActionSheet */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ 87B840971C27F3C100C84DCE /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( 88FFDA211C30269D00FCBFF3 /* SampleView.xib in Resources */, 87B840A71C27F3C100C84DCE /* LaunchScreen.storyboard in Resources */, 87B840A41C27F3C100C84DCE /* Assets.xcassets in Resources */, 87B840A21C27F3C100C84DCE /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; 8825BEDF1CCA5E4B00F7A17D /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ 87B840951C27F3C100C84DCE /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 88FFDA231C3031F200FCBFF3 /* SampleView.swift in Sources */, 87B8409F1C27F3C100C84DCE /* ViewController.swift in Sources */, 87B8409D1C27F3C100C84DCE /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 8825BEDC1CCA5E4B00F7A17D /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 8825BEEF1CCA5E8400F7A17D /* CustomizableActionSheet.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ 8825BEE71CCA5E4B00F7A17D /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 8825BEE01CCA5E4B00F7A17D /* CustomizableActionSheet */; targetProxy = 8825BEE61CCA5E4B00F7A17D /* PBXContainerItemProxy */; }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ 87B840A01C27F3C100C84DCE /* Main.storyboard */ = { isa = PBXVariantGroup; children = ( 87B840A11C27F3C100C84DCE /* Base */, ); name = Main.storyboard; sourceTree = ""; }; 87B840A51C27F3C100C84DCE /* LaunchScreen.storyboard */ = { isa = PBXVariantGroup; children = ( 87B840A61C27F3C100C84DCE /* Base */, ); name = LaunchScreen.storyboard; sourceTree = ""; }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ 87B840A91C27F3C100C84DCE /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 4.2; }; name = Debug; }; 87B840AA1C27F3C100C84DCE /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_VERSION = 4.2; VALIDATE_PRODUCT = YES; }; name = Release; }; 87B840AC1C27F3C100C84DCE /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = "Brand Assets"; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = CustomizableActionSheetDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.blk.CustomizableActionSheetDemo; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.2; }; name = Debug; }; 87B840AD1C27F3C100C84DCE /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = "Brand Assets"; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = CustomizableActionSheetDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.blk.CustomizableActionSheetDemo; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.2; }; name = Release; }; 8825BEEB1CCA5E4B00F7A17D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; CLANG_ENABLE_MODULES = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = CustomizableActionSheet/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.blk.CustomizableActionSheet; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_SWIFT3_OBJC_INFERENCE = On; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Debug; }; 8825BEEC1CCA5E4B00F7A17D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; CLANG_ENABLE_MODULES = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = CustomizableActionSheet/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.blk.CustomizableActionSheet; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_SWIFT3_OBJC_INFERENCE = On; SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ 87B840941C27F3C100C84DCE /* Build configuration list for PBXProject "CustomizableActionSheet" */ = { isa = XCConfigurationList; buildConfigurations = ( 87B840A91C27F3C100C84DCE /* Debug */, 87B840AA1C27F3C100C84DCE /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 87B840AB1C27F3C100C84DCE /* Build configuration list for PBXNativeTarget "CustomizableActionSheetDemo" */ = { isa = XCConfigurationList; buildConfigurations = ( 87B840AC1C27F3C100C84DCE /* Debug */, 87B840AD1C27F3C100C84DCE /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 8825BEEA1CCA5E4B00F7A17D /* Build configuration list for PBXNativeTarget "CustomizableActionSheet" */ = { isa = XCConfigurationList; buildConfigurations = ( 8825BEEB1CCA5E4B00F7A17D /* Debug */, 8825BEEC1CCA5E4B00F7A17D /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; rootObject = 87B840911C27F3C100C84DCE /* Project object */; } ================================================ FILE: CustomizableActionSheet.xcodeproj/project.xcworkspace/contents.xcworkspacedata ================================================ ================================================ FILE: CustomizableActionSheet.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist ================================================ IDEDidComputeMac32BitWarning ================================================ FILE: CustomizableActionSheet.xcodeproj/xcshareddata/xcschemes/CustomizableActionSheet.xcscheme ================================================ ================================================ FILE: CustomizableActionSheetDemo/AppDelegate.swift ================================================ // // AppDelegate.swift // CustomizableActionSheet // // Created by Ryuta Kibe on 2015/12/21. // Copyright © 2015年 blk. All rights reserved. // import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } 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) { // 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) { // 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) { // 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) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } ================================================ FILE: CustomizableActionSheetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json ================================================ { "images" : [ { "idiom" : "iphone", "size" : "20x20", "scale" : "2x" }, { "idiom" : "iphone", "size" : "20x20", "scale" : "3x" }, { "idiom" : "iphone", "size" : "29x29", "scale" : "2x" }, { "idiom" : "iphone", "size" : "29x29", "scale" : "3x" }, { "idiom" : "iphone", "size" : "40x40", "scale" : "2x" }, { "idiom" : "iphone", "size" : "40x40", "scale" : "3x" }, { "idiom" : "iphone", "size" : "60x60", "scale" : "2x" }, { "idiom" : "iphone", "size" : "60x60", "scale" : "3x" }, { "idiom" : "ios-marketing", "size" : "1024x1024", "scale" : "1x" } ], "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: CustomizableActionSheetDemo/Assets.xcassets/Brand Assets.launchimage/Contents.json ================================================ { "images" : [ { "orientation" : "portrait", "idiom" : "iphone", "extent" : "full-screen", "minimum-system-version" : "8.0", "subtype" : "736h", "scale" : "3x" }, { "orientation" : "portrait", "idiom" : "iphone", "extent" : "full-screen", "minimum-system-version" : "8.0", "subtype" : "667h", "scale" : "2x" }, { "orientation" : "portrait", "idiom" : "iphone", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "2x" }, { "orientation" : "portrait", "idiom" : "iphone", "extent" : "full-screen", "minimum-system-version" : "7.0", "subtype" : "retina4", "scale" : "2x" } ], "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: CustomizableActionSheetDemo/Assets.xcassets/Contents.json ================================================ { "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: CustomizableActionSheetDemo/Base.lproj/LaunchScreen.storyboard ================================================ ================================================ FILE: CustomizableActionSheetDemo/Base.lproj/Main.storyboard ================================================ ================================================ FILE: CustomizableActionSheetDemo/Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType APPL CFBundleShortVersionString 1.0.8 CFBundleSignature ???? CFBundleVersion 1 LSRequiresIPhoneOS UILaunchStoryboardName LaunchScreen UIMainStoryboardFile Main UIRequiredDeviceCapabilities armv7 UISupportedInterfaceOrientations UIInterfaceOrientationPortrait ================================================ FILE: CustomizableActionSheetDemo/SampleView.swift ================================================ // // SampleView.swift // CustomizableActionSheet // // Created by beryu on 2015/12/27. // Copyright © 2015年 blk. All rights reserved. // import UIKit @objc protocol SampleViewDelegate { func setColor(color: UIColor) } class SampleView: UIView { weak var delegate: SampleViewDelegate? @IBAction func color1WasTapped() { self.delegate?.setColor(color: UIColor(red: 0.89, green: 0.59, blue: 0.59, alpha: 1)) } @IBAction func color2WasTapped() { self.delegate?.setColor(color: UIColor(red: 0.88, green: 0.84, blue: 0.58, alpha: 1)) } @IBAction func color3WasTapped() { self.delegate?.setColor(color: UIColor(red: 0.81, green: 0.89, blue: 0.58, alpha: 1)) } @IBAction func color4WasTapped() { self.delegate?.setColor(color: UIColor(red: 0.58, green: 0.89, blue: 0.73, alpha: 1)) } @IBAction func color5WasTapped() { self.delegate?.setColor(color: UIColor(red: 0.58, green: 0.78, blue: 0.89, alpha: 1)) } } ================================================ FILE: CustomizableActionSheetDemo/SampleView.xib ================================================ ================================================ FILE: CustomizableActionSheetDemo/ViewController.swift ================================================ // // ViewController.swift // CustomizableActionSheet // // Created by Ryuta Kibe on 2015/12/21. // Copyright © 2015年 blk. All rights reserved. // import UIKit import CustomizableActionSheet class ViewController: UIViewController { var actionSheet: CustomizableActionSheet? override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func buttonShowWasTapped() { var items = [CustomizableActionSheetItem]() // First view if let sampleView = UINib(nibName: "SampleView", bundle: nil).instantiate(withOwner: self, options: nil)[0] as? SampleView { sampleView.delegate = self let sampleViewItem = CustomizableActionSheetItem(type: .view, height: 100) sampleViewItem.view = sampleView items.append(sampleViewItem) } // Second button let clearItem = CustomizableActionSheetItem(type: .button) clearItem.label = "Clear color" clearItem.backgroundColor = UIColor(red: 1, green: 0.41, blue: 0.38, alpha: 1) clearItem.textColor = UIColor.white clearItem.selectAction = { (actionSheet: CustomizableActionSheet) -> Void in self.view.backgroundColor = UIColor.white actionSheet.dismiss() } items.append(clearItem) // Third button let closeItem = CustomizableActionSheetItem(type: .button) closeItem.label = "Close" closeItem.textColor = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1) closeItem.selectAction = { (actionSheet: CustomizableActionSheet) -> Void in actionSheet.dismiss() } items.append(closeItem) let actionSheet = CustomizableActionSheet() self.actionSheet = actionSheet actionSheet.showInView(self.view, items: items) } } extension ViewController: SampleViewDelegate { func setColor(color: UIColor) { if let actionSheet = self.actionSheet { actionSheet.dismiss() } self.view.backgroundColor = color } } ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Ryuta Kibe (beryu@blk.jp) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # CustomizableActionSheet ![Platform](https://cocoapod-badges.herokuapp.com/p/CustomizableActionSheet/badge.svg) ![License](https://img.shields.io/cocoapods/l/CustomizableActionSheet.svg?style=flat) ![CocoaPods](https://cocoapod-badges.herokuapp.com/v/CustomizableActionSheet/badge.svg) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) Action sheet allows including your custom views and buttons. ![screenshot2](./assets/screenshot2.gif) ## Installation ### CocoaPods 1. Edit your Podfile: ```ruby pod 'CustomizableActionSheet' ``` 2. Run `pod install` #### Carthage 1. Edit your Cartfile: ``` github "beryu/CustomizableActionSheet" ``` 2. Run `carthage update` for more info, see [Carthage](https://github.com/carthage/carthage) ### Manually Add the [CustomizableActionSheet.swift](https://github.com/beryu/CustomizableActionSheet/blob/master/Source/CustomizableActionSheet.swift) file to your project. ## Usage ```swift var items = [CustomizableActionSheetItem]() // Setup custom view if let sampleView = UINib(nibName: "SampleView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as? SampleView { let sampleViewItem = CustomizableActionSheetItem() sampleViewItem.type = .view sampleViewItem.view = sampleView sampleViewItem.height = 100 items.append(sampleViewItem) } // Setup button let closeItem = CustomizableActionSheetItem() closeItem.type = .button closeItem.label = "Close" closeItem.selectAction = { (actionSheet: CustomizableActionSheet) -> Void in actionSheet.dismiss() } items.append(closeItem) // Show let actionSheet = CustomizableActionSheet() actionSheet.showInView(self.view, items: items) ``` You can change the positioning of the action sheet from the bottom to the top of the view as follows: ```swift actionSheet.position = .top ``` NOTE: If you have installed via CocoaPods, please import `CustomizableActionSheet` like below. ```swift import CustomizableActionSheet ``` ## Requirements * Swift4.0 * iOS 8.0 * ARC If you want to use even iOS7.0, please to import the code directly. ## License The MIT License (MIT) Copyright (c) 2015 Ryuta Kibe (beryu@blk.jp) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Source/CustomizableActionSheet.swift ================================================ // // CustomizableActionSheet.swift // CustomizableActionSheet // // Created by Ryuta Kibe on 2015/12/22. // Copyright 2015 blk. All rights reserved. // import UIKit @objc public enum CustomizableActionSheetItemType: Int { case button case view } // Can't define CustomizableActionSheetItem as struct because Obj-C can't see struct definition. public class CustomizableActionSheetItem: NSObject { // MARK: - Public properties public var type: CustomizableActionSheetItemType = .button public var height: CGFloat = CustomizableActionSheetItem.kDefaultHeight public static let kDefaultHeight: CGFloat = 44 // type = .View public var view: UIView? // type = .Button public var label: String? public var textColor: UIColor = UIColor(red: 0, green: 0.47, blue: 1.0, alpha: 1.0) public var backgroundColor: UIColor = UIColor.white public var font: UIFont? = nil public var selectAction: ((_ actionSheet: CustomizableActionSheet) -> Void)? = nil // MARK: - Private properties fileprivate var element: UIView? = nil public convenience init(type: CustomizableActionSheetItemType, height: CGFloat = CustomizableActionSheetItem.kDefaultHeight) { self.init() self.type = type self.height = height } } private class ActionSheetItemView: UIView { var subview: UIView? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! self.setting() } override init(frame: CGRect) { super.init(frame: frame) self.setting() } init() { super.init(frame: CGRect.zero) self.setting() } func setting() { self.clipsToBounds = true } override func addSubview(_ view: UIView) { super.addSubview(view) self.subview = view } override func layoutSubviews() { super.layoutSubviews() if let subview = self.subview { subview.frame = self.bounds } } } @objc public enum CustomizableActionSheetPosition: Int { case bottom case top } public class CustomizableActionSheet: NSObject { // MARK: - Private properties private static var actionSheets = [CustomizableActionSheet]() private static let kMarginSide: CGFloat = 8 private static let kItemInterval: CGFloat = 8 private static let kMarginTop: CGFloat = 20 private var items: [CustomizableActionSheetItem]? private let maskView = UIView() private let itemContainerView = UIView() private var closeBlock: (() -> Void)? // MARK: - Public properties public var defaultCornerRadius: CGFloat = 4 public var position: CustomizableActionSheetPosition = .bottom public func showInView(_ targetView: UIView, items: [CustomizableActionSheetItem], closeBlock: (() -> Void)? = nil) { // Save instance to reaction until closing this sheet CustomizableActionSheet.actionSheets.append(self) let targetBounds = targetView.bounds // Save closeBlock self.closeBlock = closeBlock // mask view let maskViewTapGesture = UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.maskViewWasTapped)) self.maskView.addGestureRecognizer(maskViewTapGesture) self.maskView.frame = targetBounds self.maskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5) targetView.addSubview(self.maskView) // set items for subview in self.itemContainerView.subviews { subview.removeFromSuperview() } var currentPosition: CGFloat = 0 let safeAreaLeft: CGFloat let safeAreaWidth: CGFloat let safeAreaTop: CGFloat let safeAreaBottom: CGFloat if #available(iOS 11.0, *) { safeAreaLeft = targetView.safeAreaInsets.left safeAreaWidth = targetView.safeAreaLayoutGuide.layoutFrame.width safeAreaTop = targetView.safeAreaInsets.top safeAreaBottom = targetView.safeAreaInsets.bottom } else { safeAreaLeft = 0 safeAreaWidth = targetBounds.width safeAreaTop = CustomizableActionSheet.kMarginTop safeAreaBottom = 0 } var availableHeight = targetBounds.height - safeAreaTop - safeAreaBottom // Calculate height of items for item in items { availableHeight = availableHeight - item.height - CustomizableActionSheet.kItemInterval } for item in items { // Apply height of items if availableHeight < 0 { let reduceNum = min(item.height, -availableHeight) item.height -= reduceNum availableHeight += reduceNum if item.height <= 0 { availableHeight += CustomizableActionSheet.kItemInterval continue } } // Add views switch (item.type) { case .button: let button = UIButton() button.layer.cornerRadius = defaultCornerRadius button.frame = CGRect( x: CustomizableActionSheet.kMarginSide, y: currentPosition, width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2), height: item.height) button.setTitle(item.label, for: UIControl.State()) button.backgroundColor = item.backgroundColor button.setTitleColor(item.textColor, for: UIControl.State()) if let font = item.font { button.titleLabel?.font = font } if let _ = item.selectAction { button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.buttonWasTapped(_:)))) } item.element = button self.itemContainerView.addSubview(button) currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval case .view: if let view = item.view { let containerView = ActionSheetItemView(frame: CGRect( x: CustomizableActionSheet.kMarginSide, y: currentPosition, width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2), height: item.height)) containerView.layer.cornerRadius = defaultCornerRadius containerView.addSubview(view) view.frame = view.bounds self.itemContainerView.addSubview(containerView) item.element = view currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval } } } let positionX: CGFloat = safeAreaLeft var positionY: CGFloat = targetBounds.minY + targetBounds.height - currentPosition - safeAreaBottom var moveY: CGFloat = positionY if self.position == .top { positionY = CustomizableActionSheet.kItemInterval moveY = -currentPosition } self.itemContainerView.frame = CGRect( x: positionX, y: positionY, width: safeAreaWidth, height: currentPosition) self.items = items // Show animation self.maskView.alpha = 0 targetView.addSubview(self.itemContainerView) self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY) UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { () -> Void in self.maskView.alpha = 1 self.itemContainerView.transform = CGAffineTransform.identity }, completion: nil) } public func dismiss() { guard let targetView = self.itemContainerView.superview else { return } // Hide animation self.maskView.alpha = 1 var moveY: CGFloat = targetView.bounds.height - self.itemContainerView.frame.origin.y if self.position == .top { moveY = -self.itemContainerView.frame.height } UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { () -> Void in self.maskView.alpha = 0 self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY) }) { (result: Bool) -> Void in // Remove views self.itemContainerView.removeFromSuperview() self.maskView.removeFromSuperview() // Remove this instance for i in 0 ..< CustomizableActionSheet.actionSheets.count { if CustomizableActionSheet.actionSheets[i] == self { CustomizableActionSheet.actionSheets.remove(at: i) break } } self.closeBlock?() } } // MARK: - Private methods @objc private func maskViewWasTapped() { self.dismiss() } @objc private func buttonWasTapped(_ sender: AnyObject) { guard let items = self.items else { return } for item in items { guard let element = item.element, let gestureRecognizer = sender as? UITapGestureRecognizer else { continue } if element == gestureRecognizer.view { item.selectAction?(self) } } } }