"Learning the path to iOS: UI series" Drawing (DrawRect)

Source: Internet
Author: User


The implementation code is as follows:
① defining 3 properties in a custom view

//记录线条的路径@property (nonatomicNSMutableArray *paths;//设置绘图过程中线条的颜色@property (nonatomicUIColor *pathColor;@property (nonatomicassignCGFloat pathWidth;//撤销功能- (void)undo;//清除功能- (void)clear;

② implementation, you need to process the operation during the move of your finger, that is, you need to operate in the following two methods.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

Method is implemented in:

//Achieve the appropriate time- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {//Get Finger objectUitouch *touch = [touches anyobject];//Get finger current touch point    CgpointStartPoint = [Touch Locationinview:touch. View];//Create Path ObjectUibezierpath *path = [Uibezierpath Bezierpath];//Set the starting point of the path[Path Movetopoint:startpoint];//Configure path Properties    //Line widthPath. LineWidth= Self. Pathwidth;//Line Union stylePath. Linejoinstyle= Kcglinejoinround;//Line stylePath. Linecapstyle= Kcglinecapround;//Add a path to an array of paths[ Self. PathsAddobject:path];} - (void) touchesmoved: (Nsset *) touches withevent: (Uievent *) Event {//Get Finger objectUitouch *touch = [touches anyobject];//Get the position of the finger current touch    CgpointCurrentpoint = [Touch Locationinview:touch. View];//Get current pathUibezierpath *path = [ Self. PathsLastobject];//Set path end point[Path Addlinetopoint:currentpoint];//This method does not need to be implemented, but it automatically calls the DrawRect method[ SelfSetneedsdisplay];}

③ implementation view in the self-contained method (DrawRect), the code is as follows:

- (void)drawRect:(CGRect)rect {     [self.pathColor setStroke];    //遍历所有路径    forself.paths) {    path.usesEvenOddFillRuleYES;        [self.pathColor setFill];        //设置笔画的颜色        [self.pathColor setStroke];        //和所有的路径相连       [path stroke];    }}

④ Properties for lazy loading that eliminates and revokes operations

- (Nsmutablearray*) Paths {if(!_paths) { Self. Paths= [NsmutablearrayArraywithcapacity:1]; }return[[_paths retain] autorelease];} - (CGFloat) Pathwidth {if(!_pathwidth) {_pathwidth =1; }return_pathwidth;}//Undo function- (void) Undo {//Delete last piece of data, path[ Self. PathsRemovelastobject]; [ SelfSetneedsdisplay];}//Clear function- (void) Clear {//Delete all information[ Self. PathsRemoveallobjects]; [ SelfSetneedsdisplay];}

④ enables you to save graffiti pictures to your system albums.
1) Get graffiti pictures (uiimage), view is the current doodle

  + (UIImage *)saveImageFromDrawView:(UIView *)view {    //获取图形上下文(上下文在这两个Graphics之间有效或者在DracRect中有效)    UIGraphicsBeginImageContext(view.frame.size);    //2.将要保存的view的layer绘制到bitmap图形上下文中,完成拍照的功能    [view.layer renderInContext:UIGraphicsGetCurrentContext()];    //从当前上下文获取图片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    //结束操作    UIGraphicsEndImageContext();    return image;

2) Save the picture (written in the trigger event of the button)

   //获取要保存的图片    UIImage *image = [UIImage saveImageFromDrawView:self.nameView];    //保存到相册    selfnilnil);

⑤ sliding the bottom of the slider implementation to change the line color, that is, the width of the line.

//设置颜色- (IBAction)colorAction:(UISlider *)sender {     //CGFloat value = sender.value;    self.nameView.pathColor = [UIColor colorWithHue:sender.value saturation:sender.value brightness:sender.value alpha:sender.value];}//设置线条的宽度- (IBAction)boldAction:(UISlider *)sender {    self.nameView.pathWidth = sender.value;}

"Learning the path to iOS: UI series" Drawing (DrawRect)

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.