3
3
// SwiftMessages
4
4
//
5
5
// Created by Timothy Moose on 7/30/16.
6
- // Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
6
+ // Copyright © 2016 SwiftKick Mobile LLC.LLC. All rights reserved.
7
7
//
8
8
9
9
import UIKit
10
10
11
11
public class MessageView : DropShadowView , Identifiable , MarginAdjustable {
12
-
12
+
13
13
/*
14
- MARK: - IB outlets
14
+ MARK: - Creating message views
15
15
*/
16
16
17
- @IBOutlet public var titleLabel : UILabel ?
18
- @IBOutlet public var bodyLabel : UILabel ?
19
- @IBOutlet public var iconContainer : UIView ?
20
- @IBOutlet public var iconImageView : UIImageView ?
21
- @IBOutlet public var iconLabel : UILabel ?
22
- @IBOutlet public var contentView : UIView ?
23
- @IBOutlet public var backgroundView : UIView !
17
+ public enum Layout : String {
18
+ case MessageView = " MessageView "
19
+ case CardView = " CardView "
20
+ case StatusLine = " StatusLine "
21
+ case MessageViewIOS8 = " MessageViewIOS8 "
22
+ }
24
23
25
- @IBOutlet public var button : UIButton ? {
24
+ public static func viewFromNib< T: MessageView > ( layout layout: Layout , filesOwner: AnyObject = NSNull . init ( ) ) -> T {
25
+ return try ! SwiftMessages . viewFromNib ( named: layout. rawValue)
26
+ }
27
+
28
+ public static func viewFromNib< T: MessageView > ( layout layout: Layout , bundle: NSBundle , filesOwner: AnyObject = NSNull . init ( ) ) -> T {
29
+ return try ! SwiftMessages . viewFromNib ( named: layout. rawValue, bundle: bundle, filesOwner: filesOwner)
30
+ }
31
+
32
+ /*
33
+ MARK: - Tap handler
34
+ */
35
+
36
+ public var tapHandler : ( ( view: MessageView ) -> Void ) ? {
26
37
didSet {
27
- if let old = oldValue {
28
- old. removeTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
29
- }
30
- if let button = button {
31
- button. addTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
32
- }
38
+ installTapRecognizer ( )
39
+ }
40
+ }
41
+
42
+ private lazy var tapRecognizer : UITapGestureRecognizer = {
43
+ let tapRecognizer = UITapGestureRecognizer ( target: self , action: #selector( MessageView . tapped) )
44
+ return tapRecognizer
45
+ } ( )
46
+
47
+ func tapped( ) {
48
+ tapHandler ? ( view: self )
49
+ }
50
+
51
+ private func installTapRecognizer( ) {
52
+ guard let contentView = contentView else { return }
53
+ contentView. removeGestureRecognizer ( tapRecognizer)
54
+ if tapHandler != nil {
55
+ // Only install the tap recognizer if there is a tap handler,
56
+ // which makes it slightly nicer if one wants to install
57
+ // a custom gesture recognizer.
58
+ contentView. addGestureRecognizer ( tapRecognizer)
33
59
}
34
60
}
35
61
@@ -44,22 +70,35 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
44
70
}
45
71
46
72
/*
47
- MARK: - Creating message views
73
+ MARK: - IB outlets
48
74
*/
49
75
50
- public enum Layout : String {
51
- case MessageView = " MessageView "
52
- case CardView = " CardView "
53
- case StatusLine = " StatusLine "
54
- case MessageViewIOS8 = " MessageViewIOS8 "
55
- }
56
-
57
- public static func viewFromNib< T: MessageView > ( layout layout: Layout ) -> T {
58
- return try ! UIView . viewFromNib ( named: layout. rawValue)
76
+ @IBOutlet public var titleLabel : UILabel ?
77
+ @IBOutlet public var bodyLabel : UILabel ?
78
+ @IBOutlet public var iconContainer : UIView ?
79
+ @IBOutlet public var iconImageView : UIImageView ?
80
+ @IBOutlet public var iconLabel : UILabel ?
81
+
82
+ @IBOutlet public var contentView : UIView ! {
83
+ didSet {
84
+ if let old = oldValue {
85
+ old. removeGestureRecognizer ( tapRecognizer)
86
+ }
87
+ installTapRecognizer ( )
88
+ }
59
89
}
60
-
61
- public static func viewFromNib< T: MessageView > ( layout layout: Layout , bundle: NSBundle ) -> T {
62
- return try ! UIView . viewFromNib ( named: layout. rawValue, bundle: bundle)
90
+
91
+ @IBOutlet public var backgroundView : UIView !
92
+
93
+ @IBOutlet public var button : UIButton ? {
94
+ didSet {
95
+ if let old = oldValue {
96
+ old. removeTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
97
+ }
98
+ if let button = button {
99
+ button. addTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
100
+ }
101
+ }
63
102
}
64
103
65
104
/*
@@ -98,7 +137,7 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
98
137
}
99
138
100
139
/*
101
- MARK: - Configuring the theme
140
+ MARK: - Theming the view
102
141
*/
103
142
104
143
extension MessageView {
0 commit comments