Skip to content

Commit de4220f

Browse files
committedDec 31, 2017
Ignore taps outside of backgroundView
1 parent 73f1267 commit de4220f

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed
 

‎SwiftMessages/BaseView.swift

+33-22
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import UIKit
1515
and a configurable tap handler. Message views do not need to inherit from `BaseVew`.
1616
*/
1717
open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
18-
18+
1919
/*
2020
MARK: - IB outlets
2121
*/
22-
22+
2323
/**
2424
Fulfills the `BackgroundViewable` protocol and is the target for
2525
the optional `tapHandler` block. Defaults to `self`.
@@ -32,24 +32,24 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
3232
installTapRecognizer()
3333
}
3434
}
35-
35+
3636
// The `contentView` property was removed because it no longer had any functionality
3737
// in the framework. This is a minor backwards incompatible change. If you've copied
3838
// one of the included nib files from a previous release, you may get a key-value
39-
// coding runtime error related to contentView, in which case you can subclass the
39+
// coding runtime error related to contentView, in which case you can subclass the
4040
// view and add a `contentView` property or you can remove the outlet connection in
4141
// Interface Builder.
4242
// @IBOutlet public var contentView: UIView!
4343

4444
/*
4545
MARK: - Initialization
4646
*/
47-
47+
4848
public required init?(coder aDecoder: NSCoder) {
4949
super.init(coder: aDecoder)
5050
backgroundView = self
5151
}
52-
52+
5353
public override init(frame: CGRect) {
5454
super.init(frame: frame)
5555
backgroundView = self
@@ -83,16 +83,17 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
8383
let bottom = NSLayoutConstraint(item: backgroundView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottomMargin, multiplier: 1.0, constant: 0)
8484
let right = NSLayoutConstraint(item: backgroundView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .rightMargin, multiplier: 1.0, constant: 0)
8585
addConstraints([top, left, bottom, right])
86+
installTapRecognizer()
8687
}
8788

8889
/**
8990
A convenience function for installing a content view as a subview of `backgroundView`
9091
and pinning the edges to `backgroundView` with the specified `insets`.
91-
92+
9293
- Parameter contentView: The view to be installed into the background view
93-
and assigned to the `contentView` property.
94+
and assigned to the `contentView` property.
9495
- Parameter insets: The amount to inset the content view from the background view.
95-
Default is zero inset.
96+
Default is zero inset.
9697
*/
9798
open func installContentView(_ contentView: UIView, insets: UIEdgeInsets = UIEdgeInsets.zero) {
9899
contentView.translatesAutoresizingMaskIntoConstraints = false
@@ -103,11 +104,11 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
103104
let right = NSLayoutConstraint(item: contentView, attribute: .right, relatedBy: .equal, toItem: backgroundView, attribute: .right, multiplier: 1.0, constant: -insets.right)
104105
backgroundView.addConstraints([top, left, bottom, right])
105106
}
106-
107+
107108
/*
108109
MARK: - Tap handler
109110
*/
110-
111+
111112
/**
112113
An optional tap handler that will be called when the `backgroundView` is tapped.
113114
*/
@@ -116,18 +117,19 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
116117
installTapRecognizer()
117118
}
118119
}
119-
120+
120121
fileprivate lazy var tapRecognizer: UITapGestureRecognizer = {
121122
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(MessageView.tapped))
122123
return tapRecognizer
123124
}()
124-
125+
125126
@objc func tapped() {
126127
tapHandler?(self)
127128
}
128-
129+
129130
fileprivate func installTapRecognizer() {
130131
guard let backgroundView = backgroundView else { return }
132+
removeGestureRecognizer(tapRecognizer)
131133
backgroundView.removeGestureRecognizer(tapRecognizer)
132134
if tapHandler != nil {
133135
// Only install the tap recognizer if there is a tap handler,
@@ -137,22 +139,30 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
137139
}
138140
}
139141

142+
open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
143+
if backgroundView != self {
144+
let backgroundViewPoint = convert(point, to: backgroundView)
145+
return backgroundView.point(inside: backgroundViewPoint, with: event)
146+
}
147+
return super.point(inside: point, with: event)
148+
}
149+
140150
/*
141151
MARK: - MarginAdjustable
142-
152+
143153
These properties fulfill the `MarginAdjustable` protocol and are exposed
144154
as `@IBInspectables` so that they can be adjusted directly in nib files
145155
(see MessageView.nib).
146156
*/
147-
148-
@IBInspectable open var bounceAnimationOffset: CGFloat = 5.0
149157

158+
@IBInspectable open var bounceAnimationOffset: CGFloat = 5.0
159+
150160
/**
151161
For iOS 10 and lower, an optional absolute value for the top margin for cases
152162
where the view appears behind the status bar.
153163
*/
154164
@IBInspectable open var statusBarOffset: CGFloat = 20.0
155-
165+
156166
/**
157167
For iOS 11 and greater, an optional top margin adjustment for cases where the
158168
view appears behind known safe zone elements, such as the status bar and the
@@ -173,7 +183,7 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
173183
/*
174184
MARK: - Setting preferred height
175185
*/
176-
186+
177187
/**
178188
An optional value that sets the message view's intrinsic content height.
179189
This can be used as a way to specify a fixed height for the message view.
@@ -185,7 +195,7 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
185195
setNeedsLayout()
186196
}
187197
}
188-
198+
189199
open override var intrinsicContentSize: CGSize {
190200
if let preferredHeight = preferredHeight {
191201
return CGSize(width: UIViewNoIntrinsicMetric, height: preferredHeight)
@@ -199,7 +209,7 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable {
199209
*/
200210

201211
extension BaseView {
202-
212+
203213
/// A convenience function to configure a default drop shadow effect.
204214
open func configureDropShadow() {
205215
let layer = backgroundView.layer
@@ -214,9 +224,10 @@ extension BaseView {
214224
private func updateShadowPath() {
215225
layer.shadowPath = UIBezierPath(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath
216226
}
217-
227+
218228
open override func layoutSubviews() {
219229
super.layoutSubviews()
220230
updateShadowPath()
221231
}
222232
}
233+

0 commit comments

Comments
 (0)
Please sign in to comment.