Work required, recently working on an iOS chart. Find a lot of third-party library can not achieve effect, so decided to write a control.
#0 goals
Hopefully you can write a common chart control (for this project only), although the development is more difficult, but you can learn a lot of knowledge. And the controls are simple to use, can be adapted to size, and support screen rotation.
#1 Prep Work
- Online various search information
- Researched the system's own control, all based on UIView
- Using storyboard in the development process, a view is added to the page to control the size, the custom control is placed in this view and filled so that the program can adapt to the screen size.
#2 Start customizing
Create a custom control that inherits from UIView.
lgchartview.h// feikangapp//// Created by Luna Gao on 15/10/29.// copyright©2015 year all Rights reserved.//#import <UIKit/UIKit.h> @interface Lgchartview:uiview-(Instancetype) Initwithparentview: ( uiview*) view; @end
lgchartview.m//feikangapp////Created by Luna Gao on 15/10/29.//copyright©2015 year All rights reserved.//#impor T "LGChartView.h" @implementation Lgchartviewuiview *parentview;-(instancetype) Initwithparentview: (uiview*) View { Parentview = view; return [self init];} -(Instancetype) init{return [self Initwithframe:cgrectzero];} -(Instancetype) initWithFrame: (cgrect) frame{frame = CGRectMake (0, 0, ParentView.frame.size.width, ParentView.frame.size.height); self = [super Initwithframe:frame]; if (self) {self.backgroundcolor = [uicolor Clearcolor]; Self.autoresizessubviews = YES; Self.autoresizingmask = Uiviewautoresizingflexibleheight | Uiviewautoresizingflexiblewidth; } return self;} -(void) layoutsubviews {[Self setneedsdisplay];} -(void) DrawRect: (cgrect) Rect {cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextsetrgbstrokecolor (context, 0.0f, 0.0f, 0.0f, 1);//Line Color Cgcontextmovetopoint (context, Cgrectgetminx (Self.frame), Cgrectgetmaxy (self.frame)-16); Cgcontextaddlinetopoint (Context, Cgrectgetmaxx (Self.frame), Cgrectgetmaxy (self.frame)-16); Cgcontextstrokepath (context); Cgcontextsetlinewidth (context, 1.0);} @end
Where the Initwithparentview method passes in the parent view and determines the size of our custom control by the size of the parent view. I feel a little bit strange here, although I think of the solution ...
which
Self.autoresizessubviews = Yes;self.autoresizingmask = Uiviewautoresizingflexibleheight | Uiviewautoresizingflexiblewidth;
These two sentences are particularly important, and these two lines of code guarantee that when the vertical screen of a page is toggled, the control will change with the size of the parent view on the screen and our custom controls.
The following method
-(void) layoutsubviews { [self setneedsdisplay];}
Ensures that the custom control can follow the refresh when the screen is vertically toggled.
At the end of the DrawRect method, I gave all the functions of drawing graphics to it.
#3 Reference in controller
This is a very simple step.
Putting a UIView in storyboard, this uiview determines the position and size of the custom control in the interface and gets it in its associated controller. At this point, Storyboard's work is completed.
tempdetailchartviewcontroller.h// feikangapp//// Created by Luna Gao on 15/10/29.// Copyright© 2015 All rights reserved.//#import <UIKit/UIKit.h> #import "LGChartView.h" @interface Tempdetailchartviewcontroller:uiviewcontroller@property (Weak, nonatomic) Iboutlet UIView *chartview; @end
Next, we'll refer to the custom control we wrote in this controller, and give the parent class of the custom control to the UIView that we just referenced.
-(void) viewdidload { [super viewdidload]; Lgchartview *chartcontrol = [[Lgchartview alloc] initWithParentView:self.chartView]; [Self.chartview Addsubview:chartcontrol];}
The rest, you don't have to do anything ...
Directly run the project to see the effect can be. At this point, the demo of the custom control is complete, and the rest of the work is to gradually refine the custom control, include the delegate, and so on.
Some of the errors and pits #4 encountered
1. When the custom control appears to be stretched, blurred, and so on, check whether the frame of the custom control is modified in DrawRect this method . The size of a size, of course, may also be caused by one of the following.
2. Do not invoke the [self Setneedsdisplay] method in the DrawRect in the custom control , there have been numerous errors and problems with the odd flower, just because of the call of this thing ... Important thing to say 3
3. Do not invoke the [self Setneedsdisplay] method in the DrawRect in the custom control , there have been numerous errors and problems with the odd flower, just because of the call of this thing ... Important thing to say 3
4. Do not invoke the [self Setneedsdisplay] method in the DrawRect in the custom control , there have been numerous errors and problems with the odd flower, just because of the call of this thing ... Important thing to say 3
#5 Other
If there is a problem, or incorrect place, or there is a better way to achieve, please don't be stingy, leave a message below ... ...
IOS Custom Control Development