I'm trying to still learn the ropes in coding and I'm having trouble figuring out how to change the color of the brush with the press of a button. I have the default color set to black.
Thanks a lot!
Code:
#import <UIKit/UIKit.h>
@interface Image : UIViewController {
CGPoint lastPoint;
UIImageView *drawImage;
BOOL mouseSwiped;
int mouseMoved;
IBOutlet UIButton *colormenu;
IBOutlet UIButton *closebutton;
IBOutlet UIButton *mainclosebutton;
IBOutlet UIButton *colorbutton;
IBOutlet UIButton *erasebutton;
IBOutlet UIButton *textbutton;
IBOutlet UIButton *redbutton;
IBOutlet UIButton *bluebutton;
IBOutlet UIButton *greenbutton;
IBOutlet UIButton *orangebutton;
IBOutlet UIButton *yellowbutton;
IBOutlet UIButton *purplebutton;
IBOutlet UIButton *greybutton;
IBOutlet UIButton *blackbutton;
IBOutlet UIButton *pinkbutton;
IBOutlet UIButton *brownbutton;
IBOutlet UIButton *topmenu;
IBOutlet UIButton *infobutton;
}
-(IBAction)colormenuhide;
-(IBAction)colormenuappear;
-(IBAction)topmenubarappear;
-(IBAction)topmenubarhide;
-(IBAction)redcolor;
-(IBAction)orangecolor;
-(IBAction)yellowcolor;
-(IBAction)greencolor;
-(IBAction)bluecolor;
-(IBAction)purplecolor;
-(IBAction)blackcolor;
-(IBAction)greycolor;
-(IBAction)pinkcolor;
-(IBAction)browncolor;
@end
Code:
#import "Image.h"
@implementation Image
-(IBAction)topmenubarhide {
topmenu.hidden = YES;
mainclosebutton.hidden = YES;
erasebutton.hidden = YES;
textbutton.hidden = YES;
colorbutton.hidden = YES;
redbutton.hidden = YES;
bluebutton.hidden = YES;
greenbutton.hidden = YES;
orangebutton.hidden = YES;
yellowbutton.hidden = YES;
purplebutton.hidden = YES;
greybutton.hidden = YES;
blackbutton.hidden = YES;
pinkbutton.hidden = YES;
brownbutton.hidden = YES;
closebutton.hidden = YES;
colormenu.hidden = YES;
}
-(IBAction)topmenubarappear {
topmenu.hidden = NO;
mainclosebutton.hidden = NO;
erasebutton.hidden = NO;
textbutton.hidden = NO;
colorbutton.hidden = NO;
redbutton.hidden = YES;
bluebutton.hidden = YES;
greenbutton.hidden = YES;
orangebutton.hidden = YES;
yellowbutton.hidden = YES;
purplebutton.hidden = YES;
greybutton.hidden = YES;
blackbutton.hidden = YES;
pinkbutton.hidden = YES;
brownbutton.hidden = YES;
closebutton.hidden = YES;
colormenu.hidden = YES;
}
-(IBAction)colormenuhide {
redbutton.hidden = YES;
bluebutton.hidden = YES;
greenbutton.hidden = YES;
orangebutton.hidden = YES;
yellowbutton.hidden = YES;
purplebutton.hidden = YES;
greybutton.hidden = YES;
blackbutton.hidden = YES;
pinkbutton.hidden = YES;
brownbutton.hidden = YES;
closebutton.hidden = YES;
colormenu.hidden = YES;
}
-(IBAction)colormenuappear {
redbutton.hidden = NO;
bluebutton.hidden = NO;
greenbutton.hidden = NO;
orangebutton.hidden = NO;
yellowbutton.hidden = NO;
purplebutton.hidden = NO;
greybutton.hidden = NO;
blackbutton.hidden = NO;
pinkbutton.hidden = NO;
brownbutton.hidden = NO;
closebutton.hidden = NO;
colormenu.hidden = NO;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 5) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 5;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 5;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 5) {
drawImage.image = nil;
return;
}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
self.view.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Thanks a lot!