Class: EasyButton

Inherits:
UIButton
  • Object
show all
Defined in:
lib/easy-button/easy-button.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backgroundColor=(value) ⇒ Object (writeonly)

Sets the attribute backgroundColor

Parameters:

  • value

    the value to set the attribute backgroundColor to.



2
3
4
# File 'lib/easy-button/easy-button.rb', line 2

def backgroundColor=(value)
  @backgroundColor = value
end

#borderRadiusObject

Returns the value of attribute borderRadius.



3
4
5
# File 'lib/easy-button/easy-button.rb', line 3

def borderRadius
  @borderRadius
end

#fontObject

Returns the value of attribute font.



3
4
5
# File 'lib/easy-button/easy-button.rb', line 3

def font
  @font
end

#textColorObject

Returns the value of attribute textColor.



3
4
5
# File 'lib/easy-button/easy-button.rb', line 3

def textColor
  @textColor
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/easy-button/easy-button.rb', line 3

def title
  @title
end

#titleLabelObject (readonly)

Returns the value of attribute titleLabel.



4
5
6
# File 'lib/easy-button/easy-button.rb', line 4

def titleLabel
  @titleLabel
end

Instance Method Details

#buttonSetupObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/easy-button/easy-button.rb', line 20

def buttonSetup
  # Custom Title Label
  self.setTitleColor(UIColor.clearColor, forState:UIControlStateNormal)
  @titleLabel = UILabel.alloc.initWithFrame(self.bounds)
  @titleLabel.backgroundColor = UIColor.clearColor
  @titleLabel.shadowColor = UIColor.colorWithWhite(0, alpha:0.5);
  @titleLabel.shadowOffset = [0, -1]
  @titleLabel.textAlignment = UITextAlignmentCenter
  self.addSubview(@titleLabel)
  
  self.opaque = false
  self.backgroundColor = UIColor.clearColor
  self.backgroundColor = "#ff0000"
  self.borderRadius = 10
  self.font = UIFont.boldSystemFontOfSize(18)
  self.textColor = '#fff'
end

#createRoundedRectForRect(rect, radius) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/easy-button/easy-button.rb', line 172

def createRoundedRectForRect(rect, radius)
  path = CGPathCreateMutable()
  CGPathMoveToPoint(path, nil, CGRectGetMidX(rect), CGRectGetMinY(rect))
  CGPathAddArcToPoint(path, nil, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius)
  CGPathAddArcToPoint(path, nil, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMaxY(rect), radius)
  CGPathAddArcToPoint(path, nil, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMinY(rect), radius)
  CGPathAddArcToPoint(path, nil, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMinY(rect), radius)
  CGPathCloseSubpath(path)    
  path
end

#drawLinearGradient(context, rect, start_color, end_color) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/easy-button/easy-button.rb', line 153

def drawLinearGradient(context, rect, start_color, end_color)
  color_space = CGColorSpaceCreateDeviceRGB()
  locations = Pointer.new(:float, 2)
  locations[1]  = 1.0
  
  colors = [start_color, end_color]
  
  gradient = CGGradientCreateWithColors(color_space, colors, locations)
  
  start_point = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect))
  end_point = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect))
  
  CGContextSaveGState(context)
  CGContextAddRect(context, rect)
  CGContextClip(context)
  CGContextDrawLinearGradient(context, gradient, start_point, end_point, 0)
  CGContextRestoreGState(context)    
end

#drawRect(rect) ⇒ Object



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
# File 'lib/easy-button/easy-button.rb', line 86

def drawRect(rect)
  super
  context = UIGraphicsGetCurrentContext()
  
  if self.state == UIControlStateHighlighted && @backGroundColorRed && @backGroundColorGreen && @backGroundColorBlue
    backgroundColorTop = UIColor.colorWithRed(@backGroundColorRed * 0.8, green:@backGroundColorGreen * 0.8, blue:@backGroundColorBlue * 0.8, alpha:1).CGColor
  else
    backgroundColorTop = @backgroundColorTop.CGColor
  end
  backgroundColorBottom = @backgroundColorBottom.CGColor
  
  outerMargin = 5
  outerRect = CGRectInset(self.bounds, outerMargin, outerMargin)
  outerPath = createRoundedRectForRect(outerRect, @borderRadius)
  
  highlightRect = CGRectInset(outerRect, 2, 3)
  highlightPath = createRoundedRectForRect(highlightRect, @borderRadius - 1)
  
  # Draw Shadow When Not Pressed
  unless self.state == UIControlStateHighlighted
    CGContextSaveGState(context)
    CGContextSetFillColorWithColor(context, backgroundColorTop)
    CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, UIColor.colorWithWhite(0, alpha:0.5).CGColor)
    CGContextAddPath(context, outerPath)
    CGContextFillPath(context)
    CGContextRestoreGState(context)
  end
  
  # Draw Button Gradient
  CGContextSaveGState(context)
  CGContextAddPath(context, outerPath)
  CGContextClip(context)
  drawLinearGradient(context, outerRect, backgroundColorTop, backgroundColorBottom)
  CGContextRestoreGState(context)
  
  # Draw Highlight or Inner Shadow
  CGContextSaveGState(context)
  if self.state == UIControlStateHighlighted
    CGContextSetLineWidth(context, 4)
    CGContextAddPath(context, outerPath)
    CGContextAddPath(context, highlightPath)
    CGContextEOClip(context)
    drawLinearGradient(context, outerRect, UIColor.colorWithWhite(0, alpha:0.12).CGColor, UIColor.colorWithWhite(0, alpha:0.03).CGColor)
  else
    CGContextSetLineWidth(context, 4)
    CGContextAddPath(context, outerPath)
    CGContextAddPath(context, highlightPath)
    CGContextEOClip(context)
    drawLinearGradient(context, outerRect, UIColor.colorWithWhite(1, alpha:0.2).CGColor, UIColor.colorWithWhite(1, alpha:0.02).CGColor)
  end
  CGContextRestoreGState(context)
  
  # Draw Button Border
  CGContextSaveGState(context)
  CGContextSetLineWidth(context, 1)
  CGContextSetStrokeColorWithColor(context, UIColor.colorWithWhite(0, alpha:0.3).CGColor)
  CGContextAddPath(context, outerPath)
  CGContextStrokePath(context)
  CGContextRestoreGState(context)
  
  # Move Title Label Down When Pressed
  @titleLabel.transform = CGAffineTransformIdentity
  if self.state == UIControlStateHighlighted
    @titleLabel.transform = CGAffineTransformMakeTranslation(0, 1)
  end
end

#hesitateUpdateObject



195
196
197
# File 'lib/easy-button/easy-button.rb', line 195

def hesitateUpdate
  self.setNeedsDisplay
end

#initWithCoder(a_decoder) ⇒ Object



13
14
15
16
17
18
# File 'lib/easy-button/easy-button.rb', line 13

def initWithCoder(a_decoder)
  if super
    buttonSetup
  end
  self
end

#initWithFrame(frame) ⇒ Object



6
7
8
9
10
11
# File 'lib/easy-button/easy-button.rb', line 6

def initWithFrame(frame)
  if super
    buttonSetup
  end
  self
end

#rgbFromHex(hex) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/easy-button/easy-button.rb', line 183

def rgbFromHex(hex)
  hex = hex.gsub(%r{[#;]}, '')
  case hex.size
  when 3
    hex.scan(%r{[0-9A-Fa-f]}).map { |el| (1.0 * (el * 2).to_i(16)) / 255 }
  when 6
    hex.scan(%r<[0-9A-Fa-f]{2}>).map { |el| (1.0 * el.to_i(16)) / 255 }
  else
    raise ArgumentError, 'Argument is not a valid hex code.'
  end
end

#touchesBegan(touches, withEvent: the_event) ⇒ Object



199
200
201
202
# File 'lib/easy-button/easy-button.rb', line 199

def touchesBegan(touches, withEvent:the_event)
  super
  self.setNeedsDisplay
end

#touchesCancelled(touches, withEvent: the_event) ⇒ Object



209
210
211
212
213
# File 'lib/easy-button/easy-button.rb', line 209

def touchesCancelled(touches, withEvent:the_event)
  super
  self.setNeedsDisplay
  self.performSelector(:hesitateUpdate, withObject:nil, afterDelay:0.1)
end

#touchesEnded(touches, withEvent: the_event) ⇒ Object



215
216
217
218
219
# File 'lib/easy-button/easy-button.rb', line 215

def touchesEnded(touches, withEvent:the_event)
  super
  self.setNeedsDisplay
  self.performSelector(:hesitateUpdate, withObject:nil, afterDelay:0.1)
end

#touchesMoved(touches, withEvent: the_event) ⇒ Object



204
205
206
207
# File 'lib/easy-button/easy-button.rb', line 204

def touchesMoved(touches, withEvent:the_event)
  super
  self.setNeedsDisplay
end