(7/18) re-learn Standford_iOS7 development _ views, plotting, gesture recognition _ course notes, standford_ios7 gestures

Source: Internet
Author: User

(7/18) re-learn Standford_iOS7 development _ views, plotting, gesture recognition _ course notes, standford_ios7 gestures

Lesson 7:

1. View

Generally, a view is a construction block that represents a rectangular area on the screen, defines a coordinate space, and draws and adds touch events.

① Hierarchical relationship of views

A view can have only one parent view and multiple child views.

1-(void) addSubview :( UIView *) aView; // Add the child View to the parent view; 2-(void) removeFromSuperview; // remove the child view from the parent View

② UIWindow

The top-level view of the UIView: Generally, there is only one UIWindow In the iOS application, which indicates the screen content currently displayed.

③ Initialize UIView

A. initialize from storyboard: awakeFromNib

B. Code initialization: alloc initWithFrame:

- (void)setup { ... }- (void)awakeFromNib { [self setup]; }- (id)initWithFrame:(CGRect)aRect{    self = [super initWithFrame:aRect];    [self setup];    return self;}

④ View-related classes

A. CGFloat

B. CGPoint :( CGFloat) x, (CGFloat) y

C. CGSize :( CGFloat) width, (CGFloat) height

D. CGRect :( CGPoint) origin, (CGSize) size

⑤ Coordinate System

A. Concept of pixels and points: each View has a read-only attribute contentScaleFactor to identify how many pixels a point contains.

B. Coordinate System attributes: (CGRect) bounds, (CGPoint) center, (CGRect) frame


For View B: bounds = (0, 0), (200,250 ))

Frame = (320,320 ))

Center = (300,225)

Here we understand the concept that a view can rotate in the parent view.

6. Create a view

Storyboard: drag

Code: alloc initWithFrame (use init to initialize frame = CGRectZero by default)

1 CGRect labelRect = CGRectMake(20, 20, 50, 30);2 UILabel *label = [[UILabel alloc] initWithFrame:labelRect]; 3 label.text = @”Hello!”;4 [self.view addSubview:label];

7. Custom View

Through implementation-(void) drawRect :( CGRect) aRect; Method to draw content, aRect refers to the area to be optimized and is related to the final performance of the view (not required here)

Note: drawRect: The method cannot be called actively. If you need to re-paint, you can call-(void) setNeedsDisplay; or-(void) setNeedsDisplayInRect :( CGRect) aRect ;, the system will call drawRect at the appropriate time:

A. Implementation Process of drawRect

Use CoreGraphics: * to obtain the context of the drawn content

* Create a drawing path (UIBezierPath)

* Set the painting attributes (color, font, textures, lineWidth, linecaps)

* Stroke, fill, and so on

B. Use of UIBezierPath

UIBezierPath encapsulates the context content (Context: refers to the location, content, and other information of the drawing)

UIKit processes the context content before calling DrawRect. To obtain the current context, use CGContextRef context = UIGraphicsGetCurrentContext ();

UIBezierPath * path = [[UIBezierPath alloc] init]; // create // draw path [path moveToPoint: CGPointMake (75, 10)]; [path addLineToPoint: CGPointMake (160,150)]; [path addLineToPoint: CGPointMake (10,150]); // close path [path closePath]; // set stroke and fill [[UIColor greenColor] setFill]; [[UIColor redColor] setStroke]; // stroke and fill in [path fill]; [path stroke];
// Other usage path. lineWidth = 2.0; // set the drawing path width UIBezierPath * roundedRect = [UIBezierPath bezierPathWithRoundedRect :( CGRect) bounds cornerRadius :( CGFloat) radius]; // draw the rounded rectangle // draw the oval UIBezierPath * oval = [UIBezierPath bezierPathWithOvalInRect :( CGRect) bounds]; // crop the view [roundedRect addClip]; // The cropped view can only be drawn in its path area.

C. Transparency

* UIColor: attribute alpha (0.0-1.0)

* UIView: (BOOL) opaque (opaque), alpha (0.0-1.0), and hidden (hidden view)

Difference please see: http://blog.csdn.net/martin_liang/article/details/40739845

D. Context content changes during conversion between child views and parent views

Push and pop status

-(Void) drawGreenCircle :( CGContextRef) ctxt {CGContextSaveGState (ctxt); // Save the current context [[UIColor greenColor] setFill]; // draw my circle CGContextRestoreGState (ctxt ); // restore the saved context}-(void) drawRect :( CGRect) aRect {CGContextRef context = UIGraphicsGetCurrentContext (); [[UIColor redColor] setFill]; // do some stuff [self drawGreenCircle: context]; // do more stuff and keep CT fill color to be red}

E. Draw text

Use NSAttributeString

NSAttributedString * text = ...; // create the drawing content CGSize textSize = [text size]; // obtain the text size [text drawAtPoint :( CGPoint) p]; // draw the text to the specified position (upper left corner ), or use drawInRect.

F. Draw an image

UIImage * image = [UIImage imageNamed: @“foo.jpg "]; // UIImage * image = [[UIImage alloc] initWithContentsOfFile :( NSString *) fullPath]; // UIImage * image = [[UIImage alloc] initWithData :( NSData *) imageData]; // use context to draw UIGraphicsBeginImageContext (CGSize ); // draw with CGContext functionsUIImage * myImage = drawing (); UIGraphicsEndImageContext (); // standard drawing [image drawAtPoint :( CGPoint) p]; // [image drawInRect :( CGRect) r]; // [image drawAsPatternInRect :( CGRect) patRect;

G. redraw of the view chart when the bounds changes

UIView attributes: @ property (nonatomic) UIViewContentMode contentMode;

// Position re-painting UIViewContentMode {Left, Right, Top, Right, BottomLeft, BottomRight, TopLeft, TopRight} // zoom and re-paint UIViewContentModeScale {ToFill, AspectFill, aspectFit} // bit stretching/shrinking // when bounds changes, call drawRect to re-paint UIViewContentModeRedraw // it is quite often that this is what you want

2. Gesture Recognition

Step: a. Create a gesture reader and add it to the view

B. Call methods for gesture triggering

① UIGestureRecognizer

Abstract superclass, the parent class of all specific gesture classes

② Add gesture control

-(Void) setPannableView :( UIView *) pannableView // maybe this is a setter in a Controller {_ pannableView = pannableView; optional * pangr = [paialloc] initWithTarget: pannableView action: @ selector (pan :)]; // target is also a view controller. pan is the call Method for triggering. It is implemented by the target class [pannableView addGestureRecognizer: pangr]; // Add gestures to the view}

③ Example of pan gesture

-(CGPoint) translationInView :( UIView *) aView; // The distance from the touch movement-(CGPoint) velocityInView :( UIView *) aView; // The moving speed-(void) setTranslation :( CGPoint) translation inView :( UIView *) aView;

④ State attribute provided by abstract superclass

// Begin continuous gesture start // UIGestureRecognizerStateChanged move // done // The Consumer recognizes the gesture // use example-(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 was set by an @ property called origin self. origin = CGPointMake (self. origin. x + translation. x, self. origin. y + translation. y); [recognizer setTranslation: CGPointZero inView: self]; // restore the gesture movement distance and initialize the callback for the next Gesture Recognition call }}

⑤ Other gesture attributes

// UIPinchGestureRecognizer kneading gesture @ property CGFloat scale; // scaling ratio @ property (readonly) CGFloat velocity; // speed (readonly) UIRotationGestureRecognizer rotation gesture @ property CGFloat rotation; // rotate radians @ property (readonly) CGFloat velocity; // speed (readonly) UISwipeGestureRecognizer sliding gesture @ property direction ction; // direction (4) @ property NSUInteger numberOfTouchesRequired; // touch count UITapGestureRecognizer click gesture @ property NSUInteger numberOfTapsRequired; // Number of clicks @ property NSUInteger numberOfTouchesRequired; // touch count

3. Others

# Pragma mark-example

The compiler flag groups the methods. The result is as follows:

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 use iTunes U to search for standford courses

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.