Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

EducationApp

macrumors newbie
Original poster
Aug 17, 2011
18
0
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.
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!
 
You need to add a method that looks like this:

Code:
- (IBAction)changeColor:(id)sender
{
/* Insert the code here that will, based on
who the sender is, change the current color. */
}

Save the .h file after you've added the method, then go to your .xib file and assign the method to touchUpInside on all of your color choosing UIButtons.
 
The code might look something like this:

Code:
- (IBAction)changeColor:(id)sender
{
    switch (sender.tag)
    {
        case 1: //The code for setting the color to red
            break;
        case 2: //The code for setting the color to orange
            break;
        //etc...
        default: //Include a behavior for if it's none of them... logging that an error occurred might be a good idea.
    }
}

And then in your .xib file, for each color choosing button:
1.) Assign a tag for it. A tag is an integer that will allow it to be uniquely identified. You need to do this if you want to use the code as I wrote it above, because my code relies on the tag of the button that was tapped.

2.) Command drag from the button to the File's Owner, and choose for the changeColor: method to be called on touchUpInside.

Hope that helps. (You already have so much code written using CGGraphicsContext, I imagine you can handle the actual code for changing to a specific color yourself. I don't actually know much about it yet, so I'd need to read up on it before I could type the actual code for it.)
 
Changing Colors

Hi,
I am also trying to change the color by using buttons.
How did you solve it in the end?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.