mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-19 13:09:23 +08:00
82 lines
2.0 KiB
Ruby
82 lines
2.0 KiB
Ruby
class PaintView < UIView
|
|
def initWithFrame(ect)
|
|
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
|
|
|
|
class PaintViewController < UIViewController
|
|
def loadView
|
|
self.view = PaintView.alloc.init
|
|
end
|
|
|
|
def canBecomeFirstResponder
|
|
true
|
|
end
|
|
|
|
def motionEnded(motion, withEvent:event)
|
|
self.view.eraseContent if motion == UIEventSubtypeMotionShake
|
|
end
|
|
end
|
|
|
|
class AppDelegate
|
|
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
|
window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
|
window.rootViewController = PaintViewController.alloc.init
|
|
window.rootViewController.wantsFullScreenLayout = true
|
|
window.makeKeyAndVisible
|
|
return true
|
|
end
|
|
end
|