/
paint_view.rb
57 lines (52 loc) · 1.41 KB
/
paint_view.rb
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
class PaintView < UIView
def initWithFrame(rect)
if super
path = NSBundle.mainBundle.pathForResource('erase', ofType:'caf')
url = NSURL.fileURLWithPath(path)
error_ptr = Pointer.new(:id)
@eraseSound = AVAudioPlayer.alloc.initWithContentsOfURL(url,
error:error_ptr)
unless @eraseSound
raise "Can't open sound file: #{error_ptr[0].description}"
end
@paths = []
end
self
end
def drawRect(rect)
UIColor.blackColor.set
UIBezierPath.bezierPathWithRect(rect).fill
@paths.each do |path, color|
color.set
path.stroke
end
end
def touchesBegan(touches, withEvent:event)
bp = UIBezierPath.alloc.init
bp.lineWidth = 3.0
random_color = begin
red, green, blue = rand(100)/100.0, rand(100)/100.0, rand(100)/100.0
UIColor.alloc.initWithRed(red/100.0, green:green, blue:blue, alpha:1.0)
end
@paths << [bp, random_color]
end
def touchesMoved(touches, withEvent:event)
touch = event.touchesForView(self).anyObject
point = touch.locationInView(self)
if @previousPoint and !@paths.empty?
bp = @paths.last.first
bp.moveToPoint(@previousPoint)
bp.addLineToPoint(point)
end
@previousPoint = point
setNeedsDisplay
end
def touchesEnded(touches, withEvent:event)
@previousPoint = nil
end
def eraseContent
@paths.clear
@eraseSound.play
setNeedsDisplay
end
end