Skip to content

Instantly share code, notes, and snippets.

@imrekel
Created September 21, 2012 08:57
Show Gist options
  • Save imrekel/3760460 to your computer and use it in GitHub Desktop.
Save imrekel/3760460 to your computer and use it in GitHub Desktop.
bme-ios - iPaint
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
for (int i=0; i<kColorCount; i++)
{
UIColor* color = [UIColor colorWithHue:i*(1.0/kColorCount)
saturation:1.0 brightness:1.0 alpha:1.0];
[color setFill];
CGFloat colorWidth = self.bounds.size.width/kColorCount;
CGContextFillRect(context, CGRectMake(colorWidth*i, 0,
colorWidth, self.bounds.size.height));
/*
if (i == _selectedColorIndex)
{
[[UIColor blackColor] setStroke];
CGContextSetLineWidth(context, 2.0);
CGContextStrokeRect(context, CGRectMake(colorWidth*i, 1, colorWidth-1, self.bounds.size.height-1));
}
*/
}
}
- (void)handleTap:(UITapGestureRecognizer*)gestureRecognizer
{
CGPoint tapPoint = [gestureRecognizer locationInView:self];
CGFloat colorWidth = self.bounds.size.width/kColorCount;
_selectedColorIndex = tapPoint.x / colorWidth;
_selectedColor = [UIColor colorWithHue:_selectedColorIndex*(1.0/kColorCount)
saturation:1.0 brightness:1.0 alpha:1.0];
[self setNeedsDisplay];
}
- (id)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (self) {
self.selectedColor = [UIColor colorWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];
_selectedColorIndex = 0;
}
return self;
}
- (void)drawCircleWithCenter: (CGPoint)center radius: (CGFloat)radius color:(UIColor*)color
{
// Új grafikus kontextus létrehozása, képre való rajzoláshoz
UIGraphicsBeginImageContextWithOptions(self.canvas.bounds.size, NO, [[UIScreen mainScreen] scale]);
// Ha már van valami a képen, akkor azt kirajzoljuk az új grafikus kontextusra
// UIImage drawAtPoint automatikusan az aktuális grafikus kontextust használja
if (self.canvas.image)
[self.canvas.image drawAtPoint:CGPointZero];
// Grafikus kontextus elkérése és rajzolás
CGContextRef context = UIGraphicsGetCurrentContext();
[color setStroke];
[color setFill];
CGContextFillEllipseInRect(context, CGRectMake(center.x-radius, center.y-radius, radius*2, radius*2));
// A grafikus kontextus tartalmának lekérése egy UIImage-ben, majd
// ennek beállítása a UIImageView-hez
self.canvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (void)drawSquareWithCenter: (CGPoint)center width: (CGFloat)width color:(UIColor*)color
{
// Új grafikus kontextus létrehozása, képre való rajzoláshoz
UIGraphicsBeginImageContextWithOptions(self.canvas.bounds.size, NO, [[UIScreen mainScreen] scale]);
// Ha már van valami a képen, akkor azt kirajzoljuk az új grafikus kontextusra
// UIImage drawAtPoint automatikusan az aktuális grafikus kontextust használja
if (self.canvas.image)
[self.canvas.image drawAtPoint:CGPointZero];
// Grafikus kontextus elkérése és rajzolás
CGContextRef context = UIGraphicsGetCurrentContext();
[color setStroke];
[color setFill];
CGContextFillRect(context, CGRectMake(center.x-width/2, center.y-width/2, width, width));
// A grafikus kontextus tartalmának lekérése egy UIImage-ben, majd
// ennek beállítása a UIImageView-hez
self.canvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment