Custom View for iOS development

Source: Internet
Author: User

IOS developmentCustomViewIs the content to be introduced in this article, in the iOS SDKViewIs UIView, we can easily customizeView. Create a Window-based Application program and add a class named Hypnosister to it. This class inherits from UIObject. Modify this class to inherit from: UIView

 
 
  1. @interface HypnosisView : UIView 

CustomViewThe key is to define the drawRect: method, because it is mainly changed by reloading this method.View. For example, you can use the following code to plot the effect of many rings.View.

 
 
  1. View Code   
  2. - (void)drawRect:(CGRect)rect   
  3. {      
  4. // What rectangle am I filling?    CGRect bounds = [self bounds];      
  5. // Where is its center?    CGPoint center;      
  6. center.x = bounds.origin.x + bounds.size.width / 2.0;      
  7. center.y = bounds.origin.y + bounds.size.height / 2.0;      
  8. // From the center how far out to a corner?    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;      
  9. // Get the context being drawn upon    CGContextRef context = UIGraphicsGetCurrentContext();      
  10. // All lines will be drawn 10 points wide    CGContextSetLineWidth(context, 10);      
  11. // Set the stroke color to light gray    [[UIColor lightGrayColor] setStroke];      
  12. // Draw concentric circles from the outside in    for (float currentRadius = maxRadius; currentRadius > 0;   
  13. currentRadius -= 20)    {          
  14. CGContextAddArc(context, center.x, center.y,                          
  15.                           currentRadius, 0.0, M_PI * 2.0, YES);         
  16.  CGContextStrokePath(context);      
  17. }  

The view effect is as follows:

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

 
 
  1. // Create a string NSString * text = @ "I am Zhu Yulin, not Zhu Qilin ";
  2. // Get a font to draw it in UIFont * font = [UIFont boldSystemFontOfSize: 28];
  3. // Where am I going to draw it? CGRect textRect;
  4. TextRect. size = [text sizeWithFont: font];
  5. TextRect. origin. x = center. x-textRect. size. width/2.0;
  6. TextRect. origin. y = center. y-textRect. size. height/2.0;
  7. // Set the fill color of the current context to black [[UIColor blackColor] setFill];
  8. // Set the shadow to be offset 4 points right, 3 points down,
  9. // Dark gray and with a blur radius of 2 points CGSize offset = CGSizeMake (4, 3 );
  10. CGColorRef color = [[UIColor darkGrayColor] CGColor];
  11. CGContextSetShadowWithColor (context, offset, 2.0, color );
  12. // Draw the string [text drawInRect: textRect
  13. WithFont: font];

Effect:

If the view is too large, we can place it in the middle of a UIScrollView, so that we can drag it. The relationship between UIScrollView and View is as follows:

Use the following code to create a View four times larger than the iPhone screen, and then use UIScrollView to display the View. The Code is as follows:

 
 
  1. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
  2. {
  3. // Create a CGRect of the form size
  4. CGRect wholeWindow = [[self window] bounds];
  5. // Create a form-sized HypnosisView instance
  6. View = [[HypnosisView alloc] initWithFrame: wholeWindow];
  7. UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: wholeWindow];
  8. [[Self window] addSubview: scrollView];
  9. // Make your view twice as large as the window CGRect reallyBigRect;
  10. ReallyBigRect. origin = CGPointZero;
  11. ReallyBigRect. size. width = wholeWindow. size. width * 2.0;
  12. ReallyBigRect. size. height = wholeWindow. size. height * 2.0;
  13. [ScrollView setContentSize: reallyBigRect. size];
  14. CGPoint offset;
  15. Offset. x = wholeWindow. size. width * 0.5;
  16. Offset. y = wholeWindow. size. height * 0.5;
  17. [ScrollView setContentOffset: offset];
  18. // Create the view = [[HypnosisView alloc] initWithFrame: reallyBigRect];
  19. [View setBackgroundColor: [UIColor clearColor];
  20. [ScrollView addSubview: view];
  21. [ScrollView release];
  22. [[UIApplication sharedApplication] setStatusBarHidden: YES
  23. WithAnimation: UIStatusBarAnimationFade];
  24. [[Self window] makeKeyAndVisible];
  25. Return YES;
  26. }

In this way, we can drag to display invisible views, such:

With UIScrollView, we can also set the view scaling function to add the following code. In this way, we can use two fingers to scale the view.

 
 
  1. // Enable zooming      
  2. [scrollView setMinimumZoomScale:0.5];      
  3. [scrollView setMaximumZoomScale:5];     
  4.  [scrollView setDelegate:self]; 

Summary: DetailsIOS developmentCustomViewAfter the introduction, I briefly summarized the customViewI hope this article will be helpful to you! This article aims to make it easier for you to learnIOS developmentInView, Provides code download, address: http://files.cnblogs.com/zhuqil/Hypnosister.zip.

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.