@@ -14,12 +14,12 @@ protocol MergeButtonDelegate: class {
1414 func didSelectOptions( button: MergeButton )
1515}
1616
17- final class MergeButton : UIView {
17+ final class MergeButton : UIControl {
1818
1919 weak var delegate : MergeButtonDelegate ?
2020
2121 // public to use as source view for popover
22- let optionIconView = UIImageView ( )
22+ let optionButton = UIButton ( )
2323
2424 private let mergeLabel = UILabel ( )
2525 private let optionBorder = UIView ( )
@@ -29,20 +29,30 @@ final class MergeButton: UIView {
2929 override init ( frame: CGRect ) {
3030 super. init ( frame: frame)
3131
32+ addTouchEffect ( )
33+
3234 addSubview ( mergeLabel)
33- addSubview ( optionIconView )
35+ addSubview ( optionButton )
3436 addSubview ( optionBorder)
3537 addSubview ( activityView)
3638
39+ addTarget ( self , action: #selector( onMainTouch) , for: . touchUpInside)
3740 layer. cornerRadius = Styles . Sizes. avatarCornerRadius
3841
3942 let image = UIImage ( named: " chevron-down " ) . withRenderingMode ( . alwaysTemplate)
40- let optionButtonWidth = ( image. size. width ?? 0 ) + ( 2 * Styles. Sizes. gutter)
41- optionIconView. contentMode = . center
42- optionIconView. image = image
43- optionIconView. isAccessibilityElement = true
44- optionIconView. accessibilityTraits = UIAccessibilityTraitButton
45- optionIconView. snp. makeConstraints { make in
43+ let optionButtonWidth = image. size. width + 2 * Styles. Sizes. gutter
44+ // more exagerated than the default given the small button size
45+ optionButton. addTouchEffect ( UIControlEffect (
46+ alpha: 0.5 ,
47+ transform: CGAffineTransform ( scaleX: 0.85 , y: 0.85 )
48+ ) )
49+ optionButton. adjustsImageWhenHighlighted = false
50+ optionButton. imageView? . contentMode = . center
51+ optionButton. setImage ( image, for: . normal)
52+ optionButton. isAccessibilityElement = true
53+ optionButton. accessibilityTraits = UIAccessibilityTraitButton
54+ optionButton. addTarget ( self , action: #selector( onOptionsTouch) , for: . touchUpInside)
55+ optionButton. snp. makeConstraints { make in
4656 make. top. right. bottom. equalToSuperview ( )
4757 make. width. equalTo ( optionButtonWidth)
4858 }
@@ -58,16 +68,14 @@ final class MergeButton: UIView {
5868 make. top. equalToSuperview ( ) . offset ( Styles . Sizes. rowSpacing/ 2 )
5969 make. bottom. equalToSuperview ( ) . offset ( - Styles. Sizes. rowSpacing/ 2 )
6070 make. width. equalTo ( 1 / UIScreen. main. scale)
61- make. right. equalTo ( optionIconView . snp. left)
71+ make. right. equalTo ( optionButton . snp. left)
6272 }
6373
6474 activityView. hidesWhenStopped = true
6575 activityView. snp. makeConstraints { make in
6676 make. centerY. equalToSuperview ( )
6777 make. right. equalTo ( mergeLabel. snp. left) . offset ( - Styles. Sizes. columnSpacing)
6878 }
69-
70- addGestureRecognizer ( UITapGestureRecognizer ( target: self , action: #selector( onTap ( recognizer: ) ) ) )
7179 }
7280
7381 required init ? ( coder aDecoder: NSCoder ) {
@@ -99,7 +107,7 @@ final class MergeButton: UIView {
99107 ]
100108 layer. addSublayer ( gradientLayer)
101109
102- [ mergeLabel, optionIconView , optionBorder, activityView] . forEach {
110+ [ mergeLabel, optionButton , optionBorder, activityView] . forEach {
103111 bringSubview ( toFront: $0)
104112 }
105113 } else {
@@ -108,7 +116,7 @@ final class MergeButton: UIView {
108116
109117 let titleColor = enabled ? . white : Styles . Colors. Gray. dark. color
110118 mergeLabel. textColor = titleColor
111- optionIconView . tintColor = titleColor
119+ optionButton . imageView ? . tintColor = titleColor
112120 optionBorder. backgroundColor = titleColor
113121
114122 mergeLabel. text = title
@@ -120,29 +128,11 @@ final class MergeButton: UIView {
120128 }
121129
122130 let mergeButtonElement = mergeElement ( withAccessibilityLabel: title)
123- accessibilityElements = [ mergeButtonElement, optionIconView]
124- optionIconView. accessibilityLabel = NSLocalizedString ( " More merging options " , comment: " More options button for merging " )
125- }
126-
127- // MARK: Overrides
128-
129- override func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
130- super. touchesBegan ( touches, with: event)
131- // don't highlight when touching inside the chevron button
132- if let touch = touches. first, optionIconView. frame. contains ( touch. location ( in: self ) ) {
133- return
134- }
135- highlight ( true )
136- }
137-
138- override func touchesCancelled( _ touches: Set < UITouch > , with event: UIEvent ? ) {
139- super. touchesCancelled ( touches, with: event)
140- highlight ( false )
141- }
142-
143- override func touchesEnded( _ touches: Set < UITouch > , with event: UIEvent ? ) {
144- super. touchesEnded ( touches, with: event)
145- highlight ( false )
131+ accessibilityElements = [ mergeButtonElement, optionButton]
132+ optionButton. accessibilityLabel = NSLocalizedString (
133+ " More merging options " ,
134+ comment: " More options button for merging "
135+ )
146136 }
147137
148138 // MARK: Private API
@@ -153,27 +143,20 @@ final class MergeButton: UIView {
153143 element. accessibilityFrameInContainerSpace = CGRect (
154144 origin: bounds. origin,
155145 size: CGSize (
156- width: bounds. size. width - optionIconView . bounds. size. width,
146+ width: bounds. size. width - optionButton . bounds. size. width,
157147 height: bounds. size. height
158148 )
159149 )
160150 element. accessibilityTraits |= UIAccessibilityTraitButton
161151 return element
162152 }
163153
164- func highlight( _ highlight: Bool ) {
165- guard isUserInteractionEnabled else { return }
166- alpha = highlight ? 0.5 : 1
154+ @objc func onMainTouch( ) {
155+ delegate? . didSelect ( button: self )
167156 }
168157
169- @objc func onTap( recognizer: UITapGestureRecognizer ) {
170- guard recognizer. state == . ended else { return }
171-
172- if optionIconView. frame. contains ( recognizer. location ( in: self ) ) {
173- delegate? . didSelectOptions ( button: self )
174- } else {
175- delegate? . didSelect ( button: self )
176- }
158+ @objc func onOptionsTouch( ) {
159+ delegate? . didSelectOptions ( button: self )
177160 }
178161
179162}
0 commit comments