Customized view of iOS development

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>iOS Development</strong> Custom <strong>View</strong> is what this article will introduce, the iOS SDK <strong>view</strong> is uiview, we can easily customize a <strong>view</strong>. Create a window-based application program in which you add a Hypnosister class that chooses to inherit Uiobject. Modify this class so that he inherits: UIView</p></p> <ol class="dp-xml"> <ol class="dp-xml"> <li class="alt">@interface Hypnosisview:uiview</li> </ol> </ol><p><p>The key to customizing <strong>view</strong> is to define the Drawrect: method, because it is primarily overloaded to change the look of the <strong>view</strong> . For example, you can use the following code to draw a <strong>view</strong>of the effect of a lot of loops.</p></p> <ol class="dp-xml"> <li class="alt"><li class="alt">View Code</li></li> <li><li>-(void) drawrect: (cgrect) Rect</li></li> <li class="alt"><li class="alt">{</li></li> <li><li>What rectangle AM I filling?      CGRect <span class="attribute">bounds = [self bounds]; </span></li></li> <li class="alt"><li class="alt">Where Is it center? Cgpoint center;</li></li> <li><li><span class="attribute">Center.x = <span class="attribute-value">bounds.origin.x + bounds.size.width/2.0; </span></span></li></li> <li class="alt"><li class="alt"><span class="attribute">Center.y = <span class="attribute-value">BOUNDS.ORIGIN.Y + bounds.size.height/2.0; </span></span></li></li> <li><li>From the center what far off to a corner?      float <span class="attribute">Maxradius = <span class="attribute-value">hypot (bounds.size.width, bounds.size.height)/2.0; </span></span></li></li> <li class="alt"><li class="alt">Get the context being drawn upon cgcontextref <span class="attribute">context = <span class="attribute-value">Uigraphicsgetcurrentcontext (); </span></span></li></li> <li><li>All lines would be drawn ten points wide cgcontextsetlinewidth (context, 10);</li></li> <li class="alt"><li class="alt">Set the stroke color to light gray [[uicolor lightgraycolor] setstroke];</li></li> <li><li>Draw concentric circles from the outside on for (float <span class="attribute">Currentradius = <span class="attribute-value">maxradius; Currentradius <span class="tag">> 0; </span></span></span></li></li> <li class="alt"><li class="alt">currentradius- <span class="attribute">= <span class="attribute-value">) {</span> </span></li></li> <li><li>Cgcontextaddarc (context, center.x, center.y,</li></li> <li class="alt"><li class="alt">currentradius, 0.0, M_PI * 2.0, YES);</li></li> <li><li>Cgcontextstrokepath (context);</li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> </ol><p><p>The effect of this view is as Follows:</p></p><p><p></p></p><p><p>We can continue to draw something, such as drawing text, and add the following code behind this Method.</p></p> <ol class="dp-xml"> <li class="alt"><li class="alt">Create a String NSString *<span class="attribute">Text = @ "i am Zhu Qilin, not Zhu kylin"; </span></li></li> <li><li>Get a font to draw it in Uifont *<span class="attribute">font = [uifont boldsystemfontofsize:28]; </span></li></li> <li class="alt"><li class="alt">Where am I going to draw it? CGRect textrect;</li></li> <li><li><span class="attribute">textrect.size = [text sizewithfont:font]; </span></li></li> <li class="alt"><li class="alt"><span class="attribute">textrect.origin.x = <span class="attribute-value">center.x-textrect.size.width/2.0; </span></span></li></li> <li><li><span class="attribute">TEXTRECT.ORIGIN.Y = <span class="attribute-value">center.y-textrect.size.height/2.0; </span></span></li></li> <li class="alt"><li class="alt">Set the fill color of the current context to black [[uicolor blackcolor] setfill];</li></li> <li><li>Set the shadow to is offset 4 points right, 3 points down,</li></li> <li class="alt"><li class="alt">Dark gray and with a blur radius of 2 points cgsize <span class="attribute">offset = <span class="attribute-value">Cgsizemake (4, 3); </span></span></li></li> <li><li>Cgcolorref <span class="attribute">color = [[uicolor darkgraycolor] cgcolor]; </span></li></li> <li class="alt"><li class="alt">Cgcontextsetshadowwithcolor (context, offset, 2.0, color);</li></li> <li><li>Draw the string [text drawinrect:textrect</li></li> <li class="alt"><li class="alt">withfont:font];</li></li> </ol><p><p>Effect:</p></p><p><p></p></p><p><p>If the view is too large, we can place it in the middle of a uiscrollview so that it can be dragged. The relationship between Uiscrollview and view is as Follows:</p></p><p><p></p></p><p><p>Use the following code to create a view that is 4 times times larger than the iphone screen, and then show it through uiscrollview, with the following code:</p></p> <ol class="dp-xml"> <li class="alt"><li class="alt">-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions</li></li> <li><li>{</li></li> <li class="alt"><li class="alt">Create a form-size CGRect</li></li> <li><li>CGRect <span class="attribute">Wholewindow = [[self window] bounds]; </span></li></li> <li class="alt"><li class="alt">Create a Hypnosisview instance of a form size</li></li> <li><li><span class="attribute">view = [[hypnosisview alloc] initwithframe:wholewindow]; </span></li></li> <li class="alt"><li class="alt">Uiscrollview *<span class="attribute">ScrollView = [[uiscrollview alloc] initwithframe:wholewindow]; </span></li></li> <li><li>[[self window] addsubview:scrollview];</li></li> <li class="alt"><li class="alt">Make your view twice as large as the window CGRect reallybigrect;</li></li> <li><li><span class="attribute">Reallybigrect.origin = <span class="attribute-value">cgpointzero; </span></span></li></li> <li class="alt"><li class="alt"><span class="attribute">reallyBigRect.size.width = <span class="attribute-value">WholeWindow.size.width * 2.0; </span></span></li></li> <li><li><span class="attribute">reallyBigRect.size.height = <span class="attribute-value">WholeWindow.size.height * 2.0; </span></span></li></li> <li class="alt"><li class="alt">[scrollView setContentSize:reallyBigRect.size];</li></li> <li><li>Cgpoint offset;</li></li> <li class="alt"><li class="alt"><span class="attribute">offset.x = <span class="attribute-value">WholeWindow.size.width * 0.5; </span></span></li></li> <li><li><span class="attribute">Offset.y = <span class="attribute-value">WholeWindow.size.height * 0.5; </span></span></li></li> <li class="alt"><li class="alt">[scrollView setcontentoffset:offset];</li></li> <li><li>Create the view <span class="attribute">view = [[hypnosisview alloc] initwithframe:reallybigrect]; </span></li></li> <li class="alt"><li class="alt">[view Setbackgroundcolor:[uicolor clearcolor];</li></li> <li><li>[scrollView addsubview:view];</li></li> <li class="alt"><li class="alt">[scrollView release];</li></li> <li><li>[[uiapplication sharedapplication] Setstatusbarhidden:yes</li></li> <li class="alt"><li class="alt">withanimation:uistatusbaranimationfade];</li></li> <li><li>[[self window] makekeyandvisible];</li></li> <li class="alt"><li class="alt">Return YES;</li></li> <li><li>}</li></li> </ol><p><p>So we can drag to show the view that we can't see, such as:</p></p><p><p></p></p><p><p>With Uiscrollview we can also set the zoom function of the view to add the following code to The. This allows us to scale the view with two Fingers.</p></p> <ol class="dp-xml"> <ol class="dp-xml"> <li class="alt">Enable zooming</li> <li>[scrollView setminimumzoomscale:0.5];</li> <li class="alt">[scrollView setmaximumzoomscale:5];</li> <li>[scrollView setdelegate:self];</li> </ol> </ol><p><p>Summary: detailed description of the <strong>iOS development</strong> of the custom <strong>view</strong> of the content is finished, a simple summary of the use of custom <strong>view</strong> , I hope this article to you some help! In order to facilitate friends to learn more about the <strong>iOS development</strong> <strong>View</strong>, provide code download, address: http://files.cnblogs.com/zhuqil/Hypnosister.zip.</p></p><p><p>Customized view of iOS development</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.