@ -71,10 +71,6 @@ class MenuActionsViewController: UIViewController, MenuActionSheetDelegate {
let tapGesture = UITapGestureRecognizer ( target : self , action : #selector ( didTapBackground ) )
let tapGesture = UITapGestureRecognizer ( target : self , action : #selector ( didTapBackground ) )
self . view . addGestureRecognizer ( tapGesture )
self . view . addGestureRecognizer ( tapGesture )
let swipeGesture = UISwipeGestureRecognizer ( target : self , action : #selector ( didSwipeBackground ) )
swipeGesture . direction = . down
self . view . addGestureRecognizer ( swipeGesture )
}
}
override func viewDidAppear ( _ animated : Bool ) {
override func viewDidAppear ( _ animated : Bool ) {
@ -249,11 +245,6 @@ class MenuActionsViewController: UIViewController, MenuActionSheetDelegate {
animateDismiss ( action : nil )
animateDismiss ( action : nil )
}
}
@objc
func didSwipeBackground ( gesture : UISwipeGestureRecognizer ) {
animateDismiss ( action : nil )
}
// MARK: M e n u A c t i o n S h e e t D e l e g a t e
// MARK: M e n u A c t i o n S h e e t D e l e g a t e
func actionSheet ( _ actionSheet : MenuActionSheetView , didSelectAction action : MenuAction ) {
func actionSheet ( _ actionSheet : MenuActionSheetView , didSelectAction action : MenuAction ) {
@ -269,6 +260,7 @@ class MenuActionSheetView: UIView, MenuActionViewDelegate {
private let actionStackView : UIStackView
private let actionStackView : UIStackView
private var actions : [ MenuAction ]
private var actions : [ MenuAction ]
private var actionViews : [ MenuActionView ]
weak var delegate : MenuActionSheetDelegate ?
weak var delegate : MenuActionSheetDelegate ?
override var bounds : CGRect {
override var bounds : CGRect {
@ -288,29 +280,57 @@ class MenuActionSheetView: UIView, MenuActionViewDelegate {
actionStackView . spacing = CGHairlineWidth ( )
actionStackView . spacing = CGHairlineWidth ( )
actions = [ ]
actions = [ ]
actionViews = [ ]
super . init ( frame : frame )
super . init ( frame : frame )
backgroundColor = UIColor . ows_light10
backgroundColor = UIColor . ows_light10
addSubview ( actionStackView )
addSubview ( actionStackView )
actionStackView . ows_ autoPinToSuperviewEdges( )
actionStackView . autoPinEdges ToSuperviewEdges( )
self . clipsToBounds = true
self . clipsToBounds = true
// P r e v e n t p a n n i n g f r o m p e r c o l a t i n g t o t h e s u p e r v i e w , w h i c h w o u l d
let touchGesture = UILongPressGestureRecognizer ( target : self , action : #selector ( didTouch ( gesture : ) ) )
// c a u s e u s t o d i s m i s s
touchGesture . minimumPressDuration = 0.0
let panGestureSink = UIPanGestureRecognizer ( target : nil , action : nil )
touchGesture . allowableMovement = CGFloat . greatestFiniteMagnitude
self . addGestureRecognizer ( panGestureSink )
self . addGestureRecognizer ( touchGesture )
}
}
required init ? ( coder aDecoder : NSCoder ) {
required init ? ( coder aDecoder : NSCoder ) {
fatalError ( " not implemented " )
fatalError ( " not implemented " )
}
}
@objc
public func didTouch ( gesture : UIGestureRecognizer ) {
switch gesture . state {
case . possible :
break
case . began :
let location = gesture . location ( in : self )
highlightActionView ( location : location , fromView : self )
case . changed :
let location = gesture . location ( in : self )
highlightActionView ( location : location , fromView : self )
case . ended :
Logger . debug ( " \( logTag ) in \( #function ) ended " )
let location = gesture . location ( in : self )
selectActionView ( location : location , fromView : self )
case . cancelled :
Logger . debug ( " \( logTag ) in \( #function ) canceled " )
unhighlightAllActionViews ( )
case . failed :
Logger . debug ( " \( logTag ) in \( #function ) failed " )
unhighlightAllActionViews ( )
}
}
public func addAction ( _ action : MenuAction ) {
public func addAction ( _ action : MenuAction ) {
actions . append ( action )
let actionView = MenuActionView ( action : action )
let actionView = MenuActionView ( action : action )
actionView . delegate = self
actionView . delegate = self
actions . append ( action )
actionViews . append ( actionView )
self . actionStackView . addArrangedSubview ( actionView )
self . actionStackView . addArrangedSubview ( actionView )
}
}
@ -329,6 +349,41 @@ class MenuActionSheetView: UIView, MenuActionViewDelegate {
mask . path = path . cgPath
mask . path = path . cgPath
self . layer . mask = mask
self . layer . mask = mask
}
}
private func unhighlightAllActionViews ( ) {
for actionView in actionViews {
actionView . isHighlighted = false
}
}
private func actionView ( touchedBy touchPoint : CGPoint , fromView : UIView ) -> MenuActionView ? {
for actionView in actionViews {
let convertedPoint = actionView . convert ( touchPoint , from : fromView )
if actionView . point ( inside : convertedPoint , with : nil ) {
return actionView
}
}
return nil
}
private func highlightActionView ( location : CGPoint , fromView : UIView ) {
guard let touchedView = actionView ( touchedBy : location , fromView : fromView ) else {
unhighlightAllActionViews ( )
return
}
touchedView . isHighlighted = true
self . actionViews . filter { $0 != touchedView } . forEach { $0 . isHighlighted = false }
}
private func selectActionView ( location : CGPoint , fromView : UIView ) {
guard let selectedView : MenuActionView = actionView ( touchedBy : location , fromView : fromView ) else {
unhighlightAllActionViews ( )
return
}
selectedView . isHighlighted = true
self . actionViews . filter { $0 != selectedView } . forEach { $0 . isHighlighted = false }
delegate ? . actionSheet ( self , didSelectAction : selectedView . action )
}
}
}
protocol MenuActionViewDelegate : class {
protocol MenuActionViewDelegate : class {
@ -337,7 +392,7 @@ protocol MenuActionViewDelegate: class {
class MenuActionView : UIButton {
class MenuActionView : UIButton {
public weak var delegate : MenuActionViewDelegate ?
public weak var delegate : MenuActionViewDelegate ?
p rivate let action : MenuAction
p ublic let action : MenuAction
required init ( action : MenuAction ) {
required init ( action : MenuAction ) {
self . action = action
self . action = action
@ -378,14 +433,15 @@ class MenuActionView: UIButton {
contentRow . isUserInteractionEnabled = false
contentRow . isUserInteractionEnabled = false
self . addSubview ( contentRow )
self . addSubview ( contentRow )
contentRow . ows_ autoPinToSuperviewMargins( )
contentRow . autoPinEdges ToSuperviewMargins( )
contentRow . autoSetDimension ( . height , toSize : 56 , relation : . greaterThanOrEqual )
contentRow . autoSetDimension ( . height , toSize : 56 , relation : . greaterThanOrEqual )
self . addTarget( self , action : #selector ( didPress ( sender : ) ) , for : . touchUpInside )
self . isUserInteractionEnabled = false
}
}
override var isHighlighted : Bool {
override var isHighlighted : Bool {
didSet {
didSet {
Logger . debug ( " \( logTag ) in \( #function ) \( oldValue ) -> \( isHighlighted ) " )
self . backgroundColor = isHighlighted ? UIColor . ows_light10 : UIColor . white
self . backgroundColor = isHighlighted ? UIColor . ows_light10 : UIColor . white
}
}
}
}