Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:32:00+00:00 2026-06-09T05:32:00+00:00

I am new to iOS and am doing this as a learning experience. I

  • 0

I am new to iOS and am doing this as a learning experience. I got most of this code from a tutorial and its working but I wanted to add more stroke colors. My code begins with a default stroke in UIColor greenColor, but I have two buttons labeled “Red” and “Blue” that should change the stroke color to Red or Blue when pressed. I want to press Red, then start drawing and have that stroke come out red, etc. like in MS Paint. I’ve confirmed my UIButtons are linked up correctly. Any help is appreciated, thanks!

DrawView.h:

#import <UIKit/UIKit.h>
@interface DrawView : UIImageView
- (void)changeOption:(NSString *)withOption;
@end

DrawView.m:

#import "DrawView.h"
@interface DrawView()
@property (nonatomic) BOOL fingerMoved;
@property (nonatomic) CGPoint lastPoint;
@end

@implementation DrawView

CGFloat red = 0.0, green = 0.0, blue = 0.0, width = 5.0;
@synthesize fingerMoved = _fingerMoved;
@synthesize lastPoint = _lastPoint;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)changeOption:(NSString *)withOption
{
    if ([withOption isEqualToString:@"Black"]) {
        red = 0.0; green = 0.0; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Red"]) {
        red = 1.0; green = 0.0; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Blue"]) {
        red = 0.0; green = 0.0; blue = 1.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Green"]) {
        red = 0.0; green = 1.0; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Brown"]) {
        red = 0.4; green = 0.0; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Orange"]) {
        red = 1.0; green = 0.6; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Yellow"]) {
        red = 1.0; green = 1.0; blue = 0.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Purple"]) {
        red = 0.5; green = 0.0; blue = 1.0; width = 5.0;
    }else if ([withOption isEqualToString:@"Eraser"]) {
        red = 1.0; green = 1.0; blue = 1.0; width = 13.0;
    }else if ([withOption isEqualToString:@"Clear"]) {
        self.image = nil;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.fingerMoved = NO;
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
        self.image = nil;
        return;
    }

    self.lastPoint = [touch locationInView:self];
    self.lastPoint = CGPointMake(self.lastPoint.x, self.lastPoint.y - 20);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.fingerMoved = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 20;

    UIGraphicsBeginImageContext(self.frame.size);
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
        self.image = nil;
        return;
    }

    if (!self.fingerMoved) {
        UIGraphicsBeginImageContext(self.frame.size);
        [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:32:02+00:00Added an answer on June 9, 2026 at 5:32 am

    Silly mistake in some other code. Fixed now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to IOS programming but doing a bit for practice. I've created this
I am completely new to iOS development so I may be doing this wrong
I'm sure I'm dense from being new to iOS programming, but, I am having
I have a question. I am new to this ios programming. I am doing
I am new iOS development but I know android at eclipse. For example I
I'm new in iOS development, and met this library linking problem in last few
I am new to ios development and doing development in ios4.0.1 and xcode 3.2.3.
So I'm new to iOS development and am doing all I can to learn
I'm new to iOS, so apologies if this is brain dead simple... I've been
The old way that I was doing this in iOS 4 was by declaring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.