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
- @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.
- View Code
- - (void)drawRect:(CGRect)rect
- {
- // What rectangle am I filling? CGRect bounds = [self bounds];
- // Where is its center? CGPoint center;
- center.x = bounds.origin.x + bounds.size.width / 2.0;
- center.y = bounds.origin.y + bounds.size.height / 2.0;
- // From the center how far out to a corner? float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
- // Get the context being drawn upon CGContextRef context = UIGraphicsGetCurrentContext();
- // All lines will be drawn 10 points wide CGContextSetLineWidth(context, 10);
- // Set the stroke color to light gray [[UIColor lightGrayColor] setStroke];
- // Draw concentric circles from the outside in for (float currentRadius = maxRadius; currentRadius > 0;
- currentRadius -= 20) {
- CGContextAddArc(context, center.x, center.y,
- currentRadius, 0.0, M_PI * 2.0, YES);
- CGContextStrokePath(context);
- }
- }
The view effect is as follows:
We can continue to draw something, such as drawing text, and add the following code after this method.
- // Create a string NSString * text = @ "I am Zhu Yulin, not Zhu Qilin ";
- // 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;
- // Set the fill color of the current context to black [[UIColor blackColor] setFill];
- // Set the shadow to be offset 4 points right, 3 points down,
- // Dark gray and with a blur radius of 2 points 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 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:
- -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
- {
- // Create a CGRect of the form size
- CGRect wholeWindow = [[self window] bounds];
- // Create a form-sized HypnosisView instance
- 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 = [[HypnosisView alloc] initWithFrame: reallyBigRect];
- [View setBackgroundColor: [UIColor clearColor];
- [ScrollView addSubview: view];
- [ScrollView release];
- [[UIApplication sharedApplication] setStatusBarHidden: YES
- WithAnimation: UIStatusBarAnimationFade];
- [[Self window] makeKeyAndVisible];
- Return YES;
- }
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.
- // Enable zooming
- [scrollView setMinimumZoomScale:0.5];
- [scrollView setMaximumZoomScale:5];
- [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.