Seventh Lesson:
1. View
In general, a view is a building block that represents a rectangular area on the screen, defines a coordinate space, draws and adds touch events in it, and so on.
Hierarchical relationship of ① views
A view can have only one parent view, with multiple child views
1 -(void) Addsubview: (UIView *) Aview; // Parent View Add child view 2 -(void) Removefromsuperview; // child views remove themselves from the parent view
②uiwindow
Top-level view of UIView: In general, there is only one UIWindow in the iOS app, which refers to the screen content that is currently displayed.
Initialization of the ③uiview
A. Initializing from storyboard: awakefromnib
B. Code initialization: Alloc initwithframe:
-(void) Setup {...} -(void) awakefromnib {[Self setup];} -(ID) initwithframe: (cgrect) arect{ = [Super Initwithframe:arect]; [Self setup]; return Self ;}
④ classes related to views
A.cgfloat
B.cgpoint: (cgfloat) x, (cgfloat) y
C.cgsize: (cgfloat) width, (cgfloat) height
D.cgrect: (Cgpoint) origin, (cgsize) size
⑤ coordinate system
A. Pixel-to-point concept: Each view has a read-only property contentscalefactor that identifies how many pixels a point contains
B. Coordinate System Properties: (CGRect) bounds, (cgpoint) center, (cgrect) frame
For view B:bounds = ((0,0), (200,250))
frame = ((140,65), (320,320))
Center = (300,225)
Here you understand the concept that a view can be rotated in a parent view.
Creating a ⑥ View
Storyboard:drag
Code:alloc initWithFrame (directly initialized to frame = Cgrectzero by using init)
1 cgrect labelrect = CGRectMake (a); 2 UILabel *label =3 label.text = @ "hello! "; 4 [Self.view Addsubview:label];
⑦ a custom view
By implementing-(void) DrawRect: (CGRect) Arect; method to draw the content, Arect refers to the area that needs to be optimized for drawing, related to the final performance of the view (not required here)
Note: The DrawRect: Method cannot be called actively, and if it needs repainting, it can call-(void) setneedsdisplay; or-(void) Setneedsdisplayinrect: (CGRect) Arect; The system will call DrawRect at the appropriate time:
The implementation process of A.drawrect
Use Coregraphics: * Gets the context of the drawing content
* Create drawing path (Uibezierpath)
* Set drawing properties (Color,font,textures,linewidth,linecaps)
* Strokes (Strok), padding (fill), etc.
Use of B.uibezierpath
Uibezierpath encapsulates contextual content (context: refers to the location of the drawing, the content, and other information)
Uikit calls DrawRect before processing the context content, need to get the current context content when used: cgcontextref context = Uigraphicsgetcurrentcontext ();
Uibezierpath *path = [[Uibezierpath alloc] init];//Create//Draw Path[Path Movetopoint:cgpointmake ( the,Ten)]; [Path Addlinetopoint:cgpointmake ( the, Max)]; [Path Addlinetopoint:cgpointmake (Ten, Max]);//Closed Path[path Closepath];//set strokes and fills[[Uicolor Greencolor] setfill]; [[Uicolor Redcolor] setstroke];//strokes and fills[path fill]; [path stroke];
// Other usage 2.0; // set Drawing path width *roundedrect = [Uibezierpath bezierpathwithroundedrect: (cgrect) Bounds Cornerradius: (cgfloat) radius]; // Draw rounded rectangles // Draw Ellipse Uibezierpath *oval = [Uibezierpath bezierpathwithovalinrect: (cgrect) bounds]; // Crop View [Roundedrect Addclip]; // the clipped view can only be drawn within its path area, beyond which the part will not be drawn
C. Transparency-related
*uicolor: Attribute Alpha (0.0-1.0)
*uiview: (BOOL) opaque (opaque), alpha (0.0-1.0), Hidden (hidden view)
See the difference: http://blog.csdn.net/martin_liang/article/details/40739845
D. Issues with context content changes when the child view and parent view are converted
Press-in (push), remove (pop) status
- (void) Drawgreencircle: (cgcontextref) ctxt{cgcontextsavegstate (ctxt);//Save Current context[[Uicolor Greencolor] setfill]; //Draw My CircleCgcontextrestoregstate (Ctxt);//recovering a saved context}- (void) DrawRect: (cgrect) arect{cgcontextref context=Uigraphicsgetcurrentcontext (); [[Uicolor Redcolor] setfill]; //Do some stuff[self drawgreencircle:context]; //Do more stuff and expect fill color to be red}
E. Drawing text
Using nsattributestring
Nsattributedstring *text = ...; // Create drawing content Cgsize textSize = [Text Size]; // get text size size [Text drawatpoint: (Cgpoint) p]; // draw the text to the specified position (upper-left corner), or use Drawinrect to
F. Drawing pictures
UIImage *image =[UIImage imagenamed:@ "foo.jpg"];//UIImage *image = [[UIImage alloc] Initwithcontentsoffile: (NSString *) FullPath];//UIImage *image = [[UIImage alloc] Initwithdata: (NSData *) imageData];//Drawing with ContextUigraphicsbeginimagecontext (cgsize);//draw with Cgcontext functionsUIImage *myimage =Uigraphicsgetimagefromcurrentcontext (); Uigraphicsendimagecontext ();//Standard Drawing[Image drawatpoint: (Cgpoint) p];//[Image drawinrect: (CGRect) r];//[Image drawaspatterninrect: (cgrect) patrect;
Redraw the view when the g.bounds changes
UIView Properties: @property (nonatomic) Uiviewcontentmode Contentmode;
// position Redraw Uiviewcontentmode{left,right,top,right,bottomleft,bottomright,topleft,topright} // Zoom Redraw // //bounds change when called DrawRect redraw // It is quite often , that's what you want
2. Gesture Recognition
Step: A. Create a gesture recognizer, add to view
B. Invoking methods when implementing gesture triggering
①uigesturerecognizer
Abstract superclass, parent class for all specific gesture classes
② adding gesture control
-(void// Maybe-a setter in a Controller {= pannableview; *pangr = [[Uipangesturerecognizer alloc] Initwithtarget:pannableview action: @selector (pan:)]; // Target is also the view controller, and the pan is the calling method for the trigger, implemented by the target class [Pannableview Addgesturerecognizer:pangr]; // tell gestures to add to view }
Examples of ③pan gestures
-(Cgpoint) Translationinview: (UIView *) Aview; // Touch Moving Distance -(cgpoint) Velocityinview: (UIView *) Aview; // Movement Speed -(void) Settranslation: (cgpoint) Translation InView: (UIView *) Aview;
④ the State property provided by the abstract superclass
//uigesturerecognizerstatebegin continuous gesture start//uigesturerecognizerstatechanged Mobile//uigesturerecognizerstateended//uigesturerecognizerstatecancelled//uigesturerecognizerstatefailed//uigesturerecognizerstaterecognized recognition to gestures//Examples of Use- (void) Pan: (Uipangesturerecognizer *) recognizer{if((recognizer.state = = uigesturerecognizerstatechanged) | |(Recognizer.state==uigesturerecognizerstateended)) {Cgpoint translation=[recognizer Translationinview:self]; //move something in myself (I ' m a UIView) by translation.x and TRANSLATION.Y//For example, if I were a graph and my origin is set by an @property calledOrigin self.origin = Cgpointmake (self.origin.x+translation.x, self.origin.y+TRANSLATION.Y); [Recognizer Settranslation:cgpointzero inview:self];//Resume gesture Move distance, initialize for next gesture recognition call? }}
⑤ Other Gesture Properties
//Uipinchgesturerecognizer pinch gesture@property CGFloat Scale;//Zoom ratio@property (ReadOnly) CGFloat Velocity;//speed (readonly)Uirotationgesturerecognizer rotation gesture @property cgfloat rotation;//Rotate radians@property (ReadOnly) CGFloat Velocity;//speed (readonly)uiswipegesturerecognizer Slide gesture @property uiswipegesturerecognizerdirection direction;//direction (4)@property Nsuinteger numberoftouchesrequired;//number of touch controlsUITapGestureRecognizer Click Gesture @property nsuinteger numberoftapsrequired;//Click Count@property Nsuinteger numberoftouchesrequired;//number of touch controls
3. Other
#pragma mark-example
compiler tags, grouping methods, with the following results
5. Demo
Supercard:https://github.com/nslogmeng/stanford_ios7_study/commit/1505f50229e875776c323fcd08d4b80e04cfcff0
Course Video Address: NetEase Open class: Http://open.163.com/movie/2014/1/2/A/M9H7S9F1H_M9H80ED2A.html
or itunes U search Standford Course
(7/18) Re-learning Standford_ios7 Development _ View, drawing, gesture recognition _ Course notes