Commit 386c10a5 by Daniel Dahan

development: adding hitTest to Menu

parent 0bfdd805
......@@ -8,7 +8,6 @@
/* Begin PBXBuildFile section */
961730361E0E156400A9A297 /* SpringMotion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961730351E0E156400A9A297 /* SpringMotion.swift */; };
9617304C1E11931400A9A297 /* MenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9617304B1E11931400A9A297 /* MenuItem.swift */; };
9617B07D1DFCA8CF00410F8F /* Application.swift in Headers */ = {isa = PBXBuildFile; fileRef = 961E6BDE1DDA2A95004E6C93 /* Application.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B07E1DFCA8CF00410F8F /* Card.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB75D1CB40DC500C806FE /* Card.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B07F1DFCA8CF00410F8F /* ImageCard.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7621CB40DC500C806FE /* ImageCard.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -102,7 +101,6 @@
965E810B1DD4D5C800D61E4B /* RobotoFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7941CB40DC500C806FE /* RobotoFont.swift */; };
965E810C1DD4D5C800D61E4B /* DynamicFontType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9628645E1D540AF300690B69 /* DynamicFontType.swift */; };
965E810D1DD4D5C800D61E4B /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* Menu.swift */; };
965E810E1DD4D5C800D61E4B /* FABMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9697F7B61D8F22A4004741EC /* FABMenuItem.swift */; };
965E81101DD4D5C800D61E4B /* NavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7901CB40DC500C806FE /* NavigationBar.swift */; };
965E81111DD4D5C800D61E4B /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7911CB40DC500C806FE /* NavigationController.swift */; };
965E81121DD4D5C800D61E4B /* NavigationItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7921CB40DC500C806FE /* NavigationItem.swift */; };
......@@ -135,7 +133,6 @@
9697F7C11D8F2572004741EC /* Material+Array.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96C1C8801D42C62800E6608F /* Material+Array.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7C21D8F2572004741EC /* Material+UIWindow.swift in Headers */ = {isa = PBXBuildFile; fileRef = 962864591D53FE3E00690B69 /* Material+UIWindow.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7C31D8F2572004741EC /* DynamicFontType.swift in Headers */ = {isa = PBXBuildFile; fileRef = 9628645E1D540AF300690B69 /* DynamicFontType.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7C51D8F2573004741EC /* FABMenuItem.swift in Headers */ = {isa = PBXBuildFile; fileRef = 9697F7B61D8F22A4004741EC /* FABMenuItem.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7CB1D8F2573004741EC /* Snackbar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 963FBEFC1D669510008F8512 /* Snackbar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7CC1D8F2573004741EC /* SnackbarController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 961EFC571D738FF600E84652 /* SnackbarController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7CD1D8F2582004741EC /* Color.swift in Headers */ = {isa = PBXBuildFile; fileRef = 9661222D1D3EC414008BB4CB /* Color.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -229,7 +226,6 @@
/* Begin PBXFileReference section */
961276621DCD8B1800A7D920 /* CharacterAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CharacterAttribute.swift; sourceTree = "<group>"; };
961730351E0E156400A9A297 /* SpringMotion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpringMotion.swift; sourceTree = "<group>"; };
9617304B1E11931400A9A297 /* MenuItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuItem.swift; sourceTree = "<group>"; };
961DED451DCC40C500F425B6 /* Editor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Editor.swift; sourceTree = "<group>"; };
961DED4A1DCC546100F425B6 /* EditorController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorController.swift; sourceTree = "<group>"; };
961E6BDE1DDA2A95004E6C93 /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
......@@ -266,7 +262,6 @@
967887881C9777CB0037F6C9 /* MaterialViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialViewTests.swift; sourceTree = "<group>"; };
967A48181D0F425A00B8CEB7 /* StatusBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusBarController.swift; sourceTree = "<group>"; };
968C99461D377849000074FF /* Offset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Offset.swift; sourceTree = "<group>"; };
9697F7B61D8F22A4004741EC /* FABMenuItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABMenuItem.swift; sourceTree = "<group>"; };
96A183621E0C6CE200083C30 /* FABMenu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABMenu.swift; sourceTree = "<group>"; };
96A183641E0C6DD400083C30 /* FABMenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABMenuController.swift; sourceTree = "<group>"; };
96A183661E0C6DE100083C30 /* FABToToolbarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABToToolbarController.swift; sourceTree = "<group>"; };
......@@ -548,9 +543,7 @@
isa = PBXGroup;
children = (
96BCB78E1CB40DC500C806FE /* Menu.swift */,
9617304B1E11931400A9A297 /* MenuItem.swift */,
96A183621E0C6CE200083C30 /* FABMenu.swift */,
9697F7B61D8F22A4004741EC /* FABMenuItem.swift */,
96A183641E0C6DD400083C30 /* FABMenuController.swift */,
96A183661E0C6DE100083C30 /* FABToToolbarController.swift */,
);
......@@ -930,7 +923,6 @@
9697F7C11D8F2572004741EC /* Material+Array.swift in Headers */,
9697F7C21D8F2572004741EC /* Material+UIWindow.swift in Headers */,
9697F7C31D8F2572004741EC /* DynamicFontType.swift in Headers */,
9697F7C51D8F2573004741EC /* FABMenuItem.swift in Headers */,
9697F7CB1D8F2573004741EC /* Snackbar.swift in Headers */,
9697F7CC1D8F2573004741EC /* SnackbarController.swift in Headers */,
9617B07D1DFCA8CF00410F8F /* Application.swift in Headers */,
......@@ -1157,7 +1149,6 @@
965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */,
965E80FF1DD4D5C800D61E4B /* BottomNavigationController.swift in Sources */,
965E81001DD4D5C800D61E4B /* Capture.swift in Sources */,
9617304C1E11931400A9A297 /* MenuItem.swift in Sources */,
965E81011DD4D5C800D61E4B /* CapturePreview.swift in Sources */,
965E81021DD4D5C800D61E4B /* CaptureController.swift in Sources */,
965E81031DD4D5C800D61E4B /* CollectionView.swift in Sources */,
......@@ -1170,7 +1161,6 @@
965E810B1DD4D5C800D61E4B /* RobotoFont.swift in Sources */,
965E810C1DD4D5C800D61E4B /* DynamicFontType.swift in Sources */,
965E810D1DD4D5C800D61E4B /* Menu.swift in Sources */,
965E810E1DD4D5C800D61E4B /* FABMenuItem.swift in Sources */,
96A183651E0C6DD400083C30 /* FABMenuController.swift in Sources */,
965E81101DD4D5C800D61E4B /* NavigationBar.swift in Sources */,
965E81111DD4D5C800D61E4B /* NavigationController.swift in Sources */,
......
......@@ -36,7 +36,7 @@ public enum ContentViewAlignment: Int {
case center
}
open class Bar: View {
open class Bar: PulseView {
/// Will layout the view.
open var willLayout: Bool {
return 0 < width && 0 < height && nil != superview
......@@ -275,6 +275,7 @@ open class Bar: View {
open override func prepare() {
super.prepare()
heightPreset = .normal
pulseAnimation = .none
autoresizingMask = .flexibleWidth
interimSpacePreset = .interimSpace3
contentEdgeInsetsPreset = .square1
......
......@@ -38,6 +38,96 @@ public enum FABMenuDirection: Int {
case right
}
open class FABMenuItem: View {
/// A reference to the titleLabel.
open let titleLabel = UILabel()
/// A reference to the button.
open let button = FABButton()
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open override func prepare() {
super.prepare()
backgroundColor = nil
prepareButton()
prepareTitleLabel()
}
/// A reference to the titleLabel text.
open var title: String? {
get {
return titleLabel.text
}
set(value) {
titleLabel.text = value
layoutSubviews()
}
}
open override func layoutSubviews() {
super.layoutSubviews()
guard let t = title, 0 < t.utf16.count else {
titleLabel.removeFromSuperview()
return
}
if nil == titleLabel.superview {
addSubview(titleLabel)
}
}
}
extension FABMenuItem {
/// Shows the titleLabel.
open func showTitleLabel() {
let interimSpace = InterimSpacePresetToValue(preset: .interimSpace6)
titleLabel.sizeToFit()
titleLabel.width += 1.5 * interimSpace
titleLabel.height += interimSpace / 2
titleLabel.y = (height - titleLabel.height) / 2
titleLabel.x = -titleLabel.width - interimSpace
titleLabel.alpha = 0
titleLabel.isHidden = false
UIView.animate(withDuration: 0.25, animations: { [weak self] in
guard let s = self else {
return
}
s.titleLabel.alpha = 1
})
}
/// Hides the titleLabel.
open func hideTitleLabel() {
titleLabel.isHidden = true
}
}
extension FABMenuItem {
/// Prepares the button.
fileprivate func prepareButton() {
layout(button).edges()
}
/// Prepares the titleLabel.
fileprivate func prepareTitleLabel() {
titleLabel.font = RobotoFont.regular(with: 14)
titleLabel.textAlignment = .center
titleLabel.backgroundColor = .white
titleLabel.depthPreset = button.depthPreset
titleLabel.cornerRadiusPreset = .cornerRadius1
}
}
@objc(FABMenuDelegate)
public protocol FABMenuDelegate {
/**
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
open class FABMenuItem: View {
/// A reference to the titleLabel.
open let titleLabel = UILabel()
/// A reference to the button.
open let button = FABButton()
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open override func prepare() {
super.prepare()
backgroundColor = nil
prepareButton()
prepareTitleLabel()
}
/// A reference to the titleLabel text.
open var title: String? {
get {
return titleLabel.text
}
set(value) {
titleLabel.text = value
layoutSubviews()
}
}
open override func layoutSubviews() {
super.layoutSubviews()
guard let t = title, 0 < t.utf16.count else {
titleLabel.removeFromSuperview()
return
}
if nil == titleLabel.superview {
addSubview(titleLabel)
}
}
}
extension FABMenuItem {
/// Shows the titleLabel.
open func showTitleLabel() {
let interimSpace = InterimSpacePresetToValue(preset: .interimSpace6)
titleLabel.sizeToFit()
titleLabel.width += 1.5 * interimSpace
titleLabel.height += interimSpace / 2
titleLabel.y = (height - titleLabel.height) / 2
titleLabel.x = -titleLabel.width - interimSpace
titleLabel.alpha = 0
titleLabel.isHidden = false
UIView.animate(withDuration: 0.25, animations: { [weak self] in
guard let s = self else {
return
}
s.titleLabel.alpha = 1
})
}
/// Hides the titleLabel.
open func hideTitleLabel() {
titleLabel.isHidden = true
}
}
extension FABMenuItem {
/// Prepares the button.
fileprivate func prepareButton() {
layout(button).edges()
}
/// Prepares the titleLabel.
fileprivate func prepareTitleLabel() {
titleLabel.font = RobotoFont.regular(with: 14)
titleLabel.textAlignment = .center
titleLabel.backgroundColor = .white
titleLabel.depthPreset = button.depthPreset
titleLabel.cornerRadiusPreset = .cornerRadius1
}
}
......@@ -38,6 +38,18 @@ public enum MenuDirection: Int {
case right
}
open class MenuItem: Toolbar {
open override func prepare() {
super.prepare()
heightPreset = .normal
pulseAnimation = .pointWithBacking
titleLabel.textAlignment = .left
detailLabel.textAlignment = .left
}
}
open class MenuCard: Card {}
@objc(MenuDelegate)
public protocol MenuDelegate {
/**
......@@ -78,6 +90,9 @@ public protocol MenuDelegate {
*/
@objc
optional func menu(menu: Menu, tappedAt point: CGPoint, isOutside: Bool)
@objc
optional func menu(menu: Menu, didSelect menuItem: MenuItem)
}
......@@ -90,16 +105,28 @@ open class Menu: Button {
}
}
/// A reference to the contentView.
open let contentView = UIView()
/// A reference to the card.
open let card = MenuCard()
/// A reference to the contentView.
open let container = UIView()
/// A preset wrapper around cardEdgeInsets.
open var cardEdgeInsetsPreset = EdgeInsetsPreset.none {
didSet {
cardEdgeInsets = EdgeInsetsPresetToValue(preset: cardEdgeInsetsPreset)
}
}
/// A reference to cardEdgeInsets.
@IBInspectable
open var cardEdgeInsets = EdgeInsets.zero {
didSet {
layoutSubviews()
}
}
/// A reference to the MenuItems.
open var items = [MenuItem]() {
didSet {
reload()
layoutSubviews()
}
}
......@@ -111,38 +138,45 @@ open class Menu: Button {
open override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = container.bounds
reload()
}
open override func prepare() {
super.prepare()
prepareContentView()
prepareContainer()
prepareCard()
prepareHandler()
}
}
extension Menu {
fileprivate func reload() {
open func reload() {
let screen = Screen.bounds
card.width = screen.width - cardEdgeInsets.left - cardEdgeInsets.right
guard let contentView = card.contentView else {
return
}
contentView.grid.begin()
contentView.grid.axis.rows = items.count
contentView.grid.axis.direction = .vertical
var h: CGFloat = 0
for v in items {
h += v.height
}
contentView.height = h
contentView.grid.views = items
contentView.grid.commit()
}
}
extension Menu {
/// Prepares the contentView.
fileprivate func prepareContentView() {
contentView.clipsToBounds = true
contentView.backgroundColor = .white
contentView.cornerRadiusPreset = .cornerRadius1
/// Prepares the card.
fileprivate func prepareCard() {
card.contentView = UIView()
}
/// Prepares the container.
fileprivate func prepareContainer() {
container.depthPreset = .depth1
container.addSubview(contentView)
}
/// Prepares the button.
/// Prepares the handler.
fileprivate func prepareHandler() {
addTarget(self, action: #selector(handleToggleMenu), for: .touchUpInside)
}
......@@ -160,6 +194,19 @@ extension Menu {
return super.hitTest(point, with: event)
}
guard let contentView = card.contentView else {
return nil
}
for v in contentView.subviews {
let p = v.convert(point, from: self)
if v.bounds.contains(p) {
if let item = v as? MenuItem {
delegate?.menu?(menu: self, didSelect: item)
}
}
}
for v in subviews {
let p = v.convert(point, from: self)
if v.bounds.contains(p) {
......@@ -183,7 +230,7 @@ extension Menu {
return
}
guard nil == container.superview else {
guard nil == card.superview else {
return
}
......@@ -191,7 +238,7 @@ extension Menu {
switch direction {
case .up, .down, .left, .right:
layout(container).bottom().width(100).height(200).centerHorizontally()
layout(card).bottom().centerHorizontally()
}
isOpened = true
......@@ -206,7 +253,7 @@ extension Menu {
delegate?.menuWillClose?(menu: self)
container.removeFromSuperview()
card.removeFromSuperview()
isOpened = false
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
open class MenuItem: View {
/// A reference to the titleLabel.
open let titleLabel = UILabel()
/// A reference to the button.
open let button = FABButton()
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open override func prepare() {
super.prepare()
backgroundColor = nil
prepareButton()
prepareTitleLabel()
}
/// A reference to the titleLabel text.
open var title: String? {
get {
return titleLabel.text
}
set(value) {
titleLabel.text = value
layoutSubviews()
}
}
open override func layoutSubviews() {
super.layoutSubviews()
guard let t = title, 0 < t.utf16.count else {
titleLabel.removeFromSuperview()
return
}
if nil == titleLabel.superview {
addSubview(titleLabel)
}
}
}
extension MenuItem {
/// Shows the titleLabel.
open func showTitleLabel() {
let interimSpace = InterimSpacePresetToValue(preset: .interimSpace6)
titleLabel.sizeToFit()
titleLabel.width += 1.5 * interimSpace
titleLabel.height += interimSpace / 2
titleLabel.y = (height - titleLabel.height) / 2
titleLabel.x = -titleLabel.width - interimSpace
titleLabel.alpha = 0
titleLabel.isHidden = false
UIView.animate(withDuration: 0.25, animations: { [weak self] in
guard let s = self else {
return
}
s.titleLabel.alpha = 1
})
}
/// Hides the titleLabel.
open func hideTitleLabel() {
titleLabel.isHidden = true
}
}
extension MenuItem {
/// Prepares the button.
fileprivate func prepareButton() {
layout(button).edges()
}
/// Prepares the titleLabel.
fileprivate func prepareTitleLabel() {
titleLabel.font = RobotoFont.regular(with: 14)
titleLabel.textAlignment = .center
titleLabel.backgroundColor = .white
titleLabel.depthPreset = button.depthPreset
titleLabel.cornerRadiusPreset = .cornerRadius1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment