quartz2d Simple Use (iii)
One, the slider to control the scale of the circle
1. Implementation process
Create a new project, create a new class that inherits from UIView, and associate with the custom view in storyboard.
Interface Construction,
code example:
YYVIEWCONTROLLER.M file
8 9 #import "YYViewController.h" #import "YYview.h" @interface Yyviewcontroller () @property (weak, Nonatomi c) Iboutlet Yyview *circleview;14-(ibaction) ValueChange: (UISlider *) sender;15 @end17 @implementation Yyviewcontro Ller19-(void) viewDidLoad21 { [super viewdidload];23 //Do no additional setup after loading the view, Typi Cally from a nib.24}25-(ibaction) ValueChange: (UISlider *) Sender { //When the value changes, pass the values to view, change the radius of the circle by NSLog ( @ "%f", sender.value); Pass the value of sender to a custom view, set the radius of the circle to self.circleview.radius=sender.value;32}33 @end
YYview.h file
8 9 #import <uikit/uikit.h>10 @interface YYVIEW:UIVIEW12//provides a property to receive the incoming radius of the outside @property (nonatomic,assign ) Float radius;14 @end
YYVIEW.M file
8 9 #import "YYview.h" @implementation YYview12//Custom View the circle does not display 13//Override set method, assign a value to the radius (void) Setradius: (float) RADIUS15 { _radius=radius;17 //Notification custom view redraw graphic [self setneedsdisplay];19}20 21// If the view is created from Xib or storyboard, the Awakefromnib method is called first-(void) awakeFromNib23 {[/ /] Set an initial value for the radius of the circle here Self.radius = 20;26}27-(void) DrawRect: (CGRect) rect29 {//1 . Get the graphics context to Cgcontextref ctx= Uigraphicsgetcurrentcontext (); //2. Drawing //painting a circle in a custom view Cgcontextaddarc (CTX, 100, 100, Self.radius, 0, 2*M_PI, 0);/ /Set the fill color of the Circle [[Uicolor graycolor]set];37 //3. Render Cgcontextstrokepath (CTX), Cgcontextfillpath (CTX),}42 @end
Effect:
2. Note the point:
DrawRect: Methods cannot be called manually by ourselves, only by the system. DrawRect: Called when the first display or a redraw event occurs. Setneedsdisplay method: Redraw, call this method will notify the custom view redraw screen, call DrawRect:. Tip: When a view is created from Xib or storyboard, the Awakefromnib method is called. 3. Supplements can be monitored by the Value property of the slider and, of course, the value range (set to 0~100) can be specified. Second, the brush frame effect Description: The snowflake-like picture drawn to the view, to achieve the picture in the view of the effect of falling. 1. Implementation code:
8 9 #import "YYview.h" 10 11//Private extension @interface Yyview () @property (nonatomic,assign) float imagey;14 @end16 @imple Mentation YYVIEW17-(ID) Initwithcoder: (Nscoder *) ADECODER20 {21///Please note that it is important to initialize the constructor method of the parent class first if (Self=[super init Withcoder:adecoder]) {NSLog (@ "Initwithcoder:");//nstimer is typically used to periodically update some non-interface data to tell how long to call 26 Using the timer, the use of the timer will appear the phenomenon of stuttering//[Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (Updatei Mage) Userinfo:nil repeats:yes];28//Cadisplaylink brush frame, default refresh 60 times per second 30//After the timer is created, it will not be executed by default, it needs to be loaded into the message Loop Cadisplaylink *display= [Cadisplaylink displaylinkwithtarget:self selector: @selector (updateimage)];32 [Display Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];33}35 return self;36}37 38 -(void) UpdateImage39 {40//Call this method to redraw the screen in [Self setneedsdisplay];42}43-(void) awakeFromNib44 {NSLog (@ "Awakefr Omnib ");}47-(Void) DrawRect: (CGRect) rect49 {50//Draw the picture to view 51 52//each time the method is called to redraw the screen, the value of Imagey +553 self.imagey+=5;54//judgment, When the snowflake is out of the screen, let the picture start from the beginning (self.imagey>rect.size.height) {self.imagey=0;57}58 UIImage *image=[u IImage imagenamed:@ "Snow"];59 [Image drawatpoint:cgpointmake (0, Self.imagey)];60 UIImage *image2=[uiimage image named:@ "Me"];62 [Image2 drawatpoint:cgpointmake (Self.imagey)];63-}65 @end
Achieve results
2. Important Notes
(1) Sequence of calls to the following two methods
-(void) awakefromnib
-(ID) Initwithcoder: (Nscoder *) Adecoder
Tip: If view is created from Xib or storyboard, you can call the Awakefromnib method to archive. Creating a view from a file will actually call the Initwithcoder method first. Xib and storyboard are also documents.
Above two methods,-(ID) Initwithcoder: (Nscoder *) Adecoder will be called first. Implementing this method requires implementing the Nscoding protocol, which is already implemented by default because of the UIView created.
You can go to the head file to view:
Run the new program and print to verify the sequence of calls to the above two methods.
(2) Two timers
The first one:
[Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updateimage) Userinfo:nil Repeats:YES];
Description: Nstimer is generally used to periodically update some non-interface data, telling how long to call once
The second one:
Cadisplaylink *display= [Cadisplaylink displaylinkwithtarget:self selector: @selector (updateimage)];
[Display Addtorunloop:[nsrunloopmainrunloop] formode:nsdefaultrunloopmode];
Description: Cadisplaylink brush frame, default refresh 60 times per second. After the timer is created, it is not executed by default and needs to be loaded into the message loop
quartz2d Simple Use (iii)