Custom View for iOS development

Source: Internet
Author: User

The view in the IOS SDK is UIView, and we can easily customize a view.

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

@interface Hypnosisview: UIView

The key to customizing view is to define the DrawRect: method, because it is primarily overloaded to change the look of the view. For example, you can use the following code to draw a view of the effect of a lot of loops.

We can continue to draw something, such as drawing text, and add the following code behind this method.

Create a string
NSString *text = @ "I am Zhu Qilin, not Zhu Kylin";
Get a font to draw it in
Uifont *font = [Uifont boldsystemfontofsize:28];
Where am I going to draw it?
CGRect Textrect;
Textrect.size = [text Sizewithfont:font];
textrect.origin.x = center.x-textrect.size.width/2.0;
TEXTRECT.ORIGIN.Y = center.y-textrect.size.height/2.0;

[[Uicolor Blackcolor] setfill];


Cgsize offset = Cgsizemake (4, 3);
Cgcolorref color = [[Uicolor darkgraycolor] cgcolor];
Cgcontextsetshadowwithcolor (context, offset, 2.0, color);
Draw the string
[Text Drawinrect:textrect
Withfont:font];



Effect:

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:

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:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{


Create a form-size cgrect
CGRect Wholewindow = [[self window] bounds];

Create a Hypnosisview instance of a form size
view = [[Hypnosisview alloc] Initwithframe:wholewindow];

Uiscrollview *scrollview = [[Uiscrollview alloc] Initwithframe:wholewindow];
[[Self window] addsubview:scrollview];
Make your view twice as large as the window
CGRect Reallybigrect;
Reallybigrect.origin = Cgpointzero;
ReallyBigRect.size.width = wholeWindow.size.width * 2.0;
ReallyBigRect.size.height = wholeWindow.size.height * 2.0;
[ScrollView setContentSize:reallyBigRect.size];
Cgpoint offset;
Offset.x = wholeWindow.size.width * 0.5;
OFFSET.Y = wholeWindow.size.height * 0.5;
[ScrollView Setcontentoffset:offset];

Create the View
view = [[Hypnosisview alloc] initwithframe:reallybigrect];
[View Setbackgroundcolor:[uicolor Clearcolor];
[ScrollView Addsubview:view];
[ScrollView release];
[[UIApplication Sharedapplication] Setstatusbarhidden:yes
Withanimation:uistatusbaranimationfade];
[[Self window] makekeyandvisible];
return YES;


}



So we can drag to show the view that we can't see, such as:

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.

   Enable zooming
[ScrollView setminimumzoomscale:0.5];
[ScrollView Setmaximumzoomscale:5];
[ScrollView setdelegate:self];



Summary: The text simply summarizes the use of custom view.

Custom view for iOS development

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.