Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

draw to the screen

  • Loading branch information...
commit 2e44e475ccab551ddffd638335d4b6c7bb3810ab 1 parent 07b9aa3
@tbugai tbugai authored
View
6 app/app_delegate.rb
@@ -1,5 +1,11 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
+
+ @window.rootViewController = MainViewController.alloc.init
+ @window.rootViewController.wantsFullScreenLayout = true
+
+ @window.makeKeyAndVisible
true
end
end
View
8 app/main_view_controller.rb
@@ -0,0 +1,8 @@
+class MainViewController < UIViewController
+ def viewDidLoad
+ super
+
+ paintView = PaintView.alloc.initWithFrame(self.view.bounds)
+ view.addSubview(paintView)
+ end
+end
View
47 app/paint_view.rb
@@ -0,0 +1,47 @@
+class PaintView < UIView
+
+ def initWithFrame(frame)
+ if super
+ @hue = 0.0;
+ self.init_cache_context(frame.size)
+ end
+ self
+ end
+
+ def init_cache_context(size)
+ bitmap_bytes_per_row = size.width * 4
+ bitmap_byte_count = bitmap_bytes_per_row * size.height
+
+ cache_bitmap = Pointer.new(:char, bitmap_byte_count)
+ @cached_context = CGBitmapContextCreate(cache_bitmap, size.width, size.height, 8, bitmap_bytes_per_row, CGColorSpaceCreateDeviceRGB(), KCGImageAlphaNoneSkipFirst)
+ true
+ end
+
+ def touchesMoved(touches, withEvent:event)
+ @touch = touches.anyObject
+ self.draw_to_cache(@touch)
+ end
+
+ def draw_to_cache(touch)
+ color = UIColor.colorWithHue(@hue, saturation:0.7, brightness:1.0, alpha:1.0)
+
+ CGContextSetStrokeColorWithColor(@cached_context, color.CGColor)
+ CGContextSetLineCap(@cached_context, KCGLineCapRound)
+ CGContextSetLineWidth(@cached_context, 15)
+
+ last_point = @touch.previousLocationInView(self)
+ new_point = @touch.locationInView(self)
+
+ CGContextMoveToPoint(@cached_context, last_point.x, last_point.y)
+ CGContextAddLineToPoint(@cached_context, new_point.x, new_point.y)
+ CGContextStrokePath(@cached_context)
+ self.setNeedsDisplay()
+ end
+
+ def drawRect(rect)
+ context = UIGraphicsGetCurrentContext()
+ cache_image = CGBitmapContextCreateImage(@cached_context)
+ CGContextDrawImage(context, self.bounds, cache_image)
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.