IOS application development-small drawing board demo

Source: Internet
Author: User

There are many other functions that can be added. Remember to add them here.


Code Section

Touchview. h

#import <UIKit/UIKit.h>@interface TouchView : UIView{    NSMutableArray *points;    NSArray *points_all;    CGContextRef context;    UIColor *paint_clr;}@property (strong,nonatomic) NSMutableArray *points;@property (strong,nonatomic) NSArray *points_all;@property (strong,nonatomic) UIColor *paint_clr;@end

Touchview. m

# Import "touchview. H "@ implementation touchview @ synthesize points, points_all, paint_clr;-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {// initialization code paint_clr = [uicolor greencolor];} return self;} // only override drawrect: If you perform custom drawing. // an empty implementation adversely affects performance during animation. -(void) drawrect :( cgrect) Re CT {// drawing code if ((! Self. points) | (self. points. count <2) {return;} context = uigraphicsgetcurrentcontext (); // set the paint brush thickness cgcontextsetlinewidth (context, 5.0f ); // set the paint brush color // [[uicolor bluecolor] set]; // [paint_clr set]; // cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor] cgcolor]); cgcontextsetstrokecolorwithcolor (context, [paint_clr cgcolor]); // draw the previous trajectory for (Int J = 0; j <[self. points_all count]; j ++) {nsmutablearray * points_tmp = [points_all objectatindex: J]; for (INT I = 0; I <[points_tmp count]-1; I ++) {cgpoint point1 = [[points_tmp objectatindex: I] cgpointvalue]; cgpoint point2 = [[points_tmp objectatindex :( I + 1)] cgpointvalue]; cgcontextmovetopoint (context, point1.x, point1.y ); cgcontextaddlinetopoint (context, point2.x, point2.y); cgcontextstrokepath (context) ;}// draw this for (INT I = 0; I <[self. points count]-1; I ++) {cgpoint point1 = [[self. points objectatindex: I] cgpointvalue]; cgpoint point2 = [[self. points objectatindex :( I + 1)] cgpointvalue]; cgcontextmovetopoint (context, point1.x, point1.y); cgcontextaddlinetopoint (context, point2.x, point2.y); cgcontextstrokepath (context );}} // multipoint touch-(bool) ismultipletouchenabled {return no;} // create an array and record the initial ponit-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event {self. points = [nsmutablearray array]; cgpoint Pt = [[touches anyobject] locationinview: Self]; [self. points addobject: [nsvalue valuewithcgpoint: pt];} // record these points during movement // call setneedsdisplay, which triggers call of the drawrect method-(void) touchesmoved :( nsset *) touches withevent :( uievent *) event {cgpoint Pt = [[touches anyobject] locationinview: Self]; [self. points addobject: [nsvalue valuewithcgpoint: pt]; [self setneedsdisplay];}-(void) touchesended :( nsset *) touches withevent :( ui* event *) event {nsmutablearray * points_tmp = [[nsmutablearray alloc] initwitharray: Self. points]; If (self. points_all = nil) {self. points_all = [[nsarray alloc] initwithobjects: points_tmp, nil];} else {self. points_all = [self. points_all arraybyaddingobject: points_tmp] ;}}@ end

Viewcontroller. h

#import <UIKit/UIKit.h>@class TouchView;@interface ViewController : UIViewController{    TouchView *tv;}@end

Viewcontroller. m

#import "ViewController.h"#import "TouchView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    self.view.userInteractionEnabled = YES;      // TouchView *tv = [[TouchView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 260.0f)];    tv = [[TouchView alloc]initWithFrame:self.view.frame];    tv.backgroundColor = [UIColor blackColor];        [self.view addSubview:tv];        UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[@"White Red Blue Green Yellow" componentsSeparatedByString:@" "]];seg.segmentedControlStyle = UISegmentedControlSegmentCenter;    seg.tintColor = [UIColor blackColor];     seg.center = CGPointMake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));     [self.view addSubview:seg];        [seg addTarget:self action:@selector(colorChange:) forControlEvents:UIControlEventValueChanged];}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}- (void) colorChange: (UISegmentedControl *) seg{switch ([seg selectedSegmentIndex]){case 0:             tv.paint_clr = [UIColor whiteColor];break;case 1:            tv.paint_clr = [UIColor redColor];break;case 2:tv.paint_clr = [UIColor blueColor];break;case 3:tv.paint_clr = [UIColor greenColor];break;case 4:tv.paint_clr = [UIColor yellowColor];break;default:break;}}@end











Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.