iOS Development UI chapter-quartz2d simple use (iii)

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>one, the slider to control the scale of the circle</strong></p></p><p><p>1. implementation process</p></p><p><p>Create a new project, create a new class that inherits from Uiview, and associate with the custom view in Storyboard.</p></p><p><p>Interface construction,</p></p><p><p>Code example:</p></p><p><p>YYVIEWCONTROLLER.M file</p></p><pre><pre>1//2// yyviewcontroller.m 3// 04-scale The Circle 4//5// Created by Apple on 14-6-11.6// Copyright (c) 2014 Year Itcase. All Rights Reserved. 7//8 9 #import "YYViewController.h" #import "YYview.h" @interface yyviewcontroller () @property (weak, Nona Tomic) iboutlet yyview *circleview;14-(ibaction) valueChange: (uislider *) sender;15 @end17 @implementation Yyviewco Ntroller19-(void) viewDidLoad21 { [super viewdidload];23 //do Any additional setup after loading the view, Typically from a nib.24}25-(ibaction) valueChange: (uislider *) Sender { //when The value is changed, pass it to view, change the radius of the circle 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</pre></pre><p><p>YYview.h file</p></p><pre><pre>1//2// YYview.h 3// 04-scale The Circle 4//5// Created by Apple on 14-6-11.6// Copyright (c) 2014 itcase. All Rights Reserved. 7//8 9 #import <uikit/uikit.h>10 @interface yyview:uiview12//provides A property to receive the incoming radius of the external @property (nonatomic,ass Ign) float radius;14 @end</pre></pre><p><p>YYVIEW.M file</p></p><pre><pre>1//2// yyview.m 3// 04-scale The Circle 4//5// Created by Apple on 14-6-11.6// Copyright (c) 2014 itcase. All Rights Reserved. 7//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</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>2. Note the Point:</p></p>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:<pre> 1//2//YYVIEW.M 3//05-brush frame animation 4//5//Created by Apple on 14-6-11. 6//Copyright (c) 2014 Itcase. All Rights Reserved. 7//8 9 #import "YYview.h" 10 11//private extension @interface yyview () @property (nonatomic,assign) float imagey;14 @end16 @i Mplementation YYview17-(id) initwithcoder: (nscoder *) aDecoder20 {21///note You must first initialize the constructor of the parent class, if (self=[super Initwithcoder:adecoder]) {NSLog (@ "initwithcoder:");//nstimer is typically used to periodically update some non-interface data to tell how long to call 26 Use of the timer, the use of the timer will appear the phenomenon of the Stutter//[nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (upd Ateimage) 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 added Load into message loop Cadisplaylink *display= [cadisplaylink displaylinkwithtarget:self selector: @selector (updateimage)];32 [display addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode];33}35 return self;36}37 -(void) UpdatEImage39 {40//call This method to redraw the screen in [self setneedsdisplay];42}43-(void) awakeFromNib44 {NSLog (@ "awakefromnib"); 46} -(void) drawrect: (cgrect) rect49 {50//draw the picture onto view 51 52//each time The method is called to redraw the screen, the Imagey value is +553 self.imagey+=5;54 judging, 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 Uiima GE *image=[uiimage imagenamed:@ "snow"];59 [image drawatpoint:cgpointmake (0, self.imagey)];60-UIImage *image2=[u IImage imagenamed:@ "me"];62 [image2 drawatpoint:cgpointmake (, self.imagey)];63-}65 @end</pre><p><p>Achieve results</p></p><p><p></p></p><p><p>2. Important Notes</p></p><p><p>(1) sequence of calls to the following two methods</p></p><p><p>-(void) awakefromnib</p></p><p><p>-(id) initwithcoder: (nscoder *) adecoder</p></p><p><p>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.</p></p><p><p>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.</p></p><p><p>You can go to the head file to View:</p></p><p><p></p></p><p><p>Run the new program and print to verify the sequence of calls to the above two METHODS.</p></p><p><p></p></p><p><p>(2) Two Timers</p></p><p><p>The first one:</p></p><p><p>[nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updateimage) userinfo:nil repeats:YES];</p></p><p><p>Description: Nstimer is generally used to periodically update some non-interface data, telling how long to call once</p></p><p><p>The second one:</p></p><p><p>Cadisplaylink *display= [cadisplaylink displaylinkwithtarget:self selector: @selector (updateimage)];</p></p><p><p>[display addtorunloop:[nsrunloopmainrunloop] formode:nsdefaultrunloopmode];</p></p><p><p>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</p></p><p><p>iOS Development UI chapter-quartz2d simple use (iii)</p></p></span>

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.