/
Counter.swift
188 lines (108 loc) · 4.45 KB
/
Counter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Counter.swift
// IncCounter
//
// Created by Ragaie alfy on 11/15/17.
// Copyright © 2017 Ragaie alfy. All rights reserved.
//
import UIKit
@IBDesignable open class Counter: UIView {
@IBOutlet weak var increment: UIButton!
@IBOutlet weak var decrement: UIButton!
@IBOutlet weak public var valueLabel: UILabel!
public var ID : String!
public var counterValue : Int! = 0
public var delegate : CounterDelegate!
// private var rateImage : UIImage! //= UIImage.init(named: "star3")
// private var halfrateImage : UIImage! //= UIImage.init(named: "star2")
//
//
@IBInspectable public var cornerRadius: CGFloat = 5 {
didSet {
layer.cornerRadius = cornerRadius
clipsToBounds = true
}
}
@IBInspectable public var borderWidth: CGFloat = 1.0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable public var borderColor: UIColor = UIColor.blue {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable public var fontColor: UIColor = UIColor.black {
didSet {
valueLabel.textColor = fontColor
}
}
@IBInspectable public var fontSize: CGFloat = 20.0{
didSet {
valueLabel.font = UIFont.systemFont(ofSize: fontSize)
}
}
@IBInspectable public var satrtValue: Int = 0{
didSet {
valueLabel.text = String(satrtValue)
counterValue = satrtValue
}
}
@IBInspectable public var imagedec : UIImage? = UIImage.init(named: "image-") {
didSet {
// arrowImage.image = Arrow
decrement.setBackgroundImage(imagedec, for: .normal)
}
}
@IBInspectable public var imagein : UIImage? = UIImage.init(named: "image+") {
didSet {
increment.setBackgroundImage(imagein, for: .normal)
// arrowImage.image = Arrow
}
}
//MARK: Initializers
override public init(frame : CGRect) {
super.init(frame : frame)
initSubviews()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSubviews()
initActionAndDelegete()
}
func initSubviews() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "Counter", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
// to make view fit view in design you welcome.
view.frame = self.bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
// nib.contentView.frame = bounds
addSubview(view)
// custom initialization logic
// initalize images
imagedec = UIImage(named: "Image-" , in: bundle,compatibleWith: nil)!
imagein = UIImage(named: "Image+", in: bundle,compatibleWith: nil)!
}
func initActionAndDelegete() {
increment.addTarget(self, action: #selector(Counter.increseValue(_:)), for: .touchUpInside)
decrement.addTarget(self, action: #selector(Counter.decreschangeValue(_:)), for: .touchUpInside)
}
@objc func increseValue(_ sender : UIButton){
counterValue = counterValue + 1
valueLabel.text = String(counterValue)
if delegate != nil {
delegate.counter(self, changedValue: counterValue,buttonPress: "+")
}
}
@objc func decreschangeValue(_ sender : UIButton){
if counterValue > satrtValue {
counterValue = counterValue - 1
valueLabel.text = String(counterValue)
}
if delegate != nil {
delegate.counter(self, changedValue: counterValue,buttonPress: "-")
}
}
}