IPhone Development Study Notes 004-custom background transparent non-full screen pop-up window, subclass UIWindow

Source: Internet
Author: User

The final result is as follows. Click the above button to bring up a transparent and non-full screen pop-up window for the background, without using UIActionSheet and UIAlertView.




The following describes the specific process.

1. Create a single view application project, add related controls, and drag and drop the connection:
For example:

 



Create an OC class and inherit from UIWindow, for example:







 
 
CustomWindow. h:
 
# Import <UIKit/UIKit. h>
 
 
 
 
@ Interface CustomWindow: UIWindow {
 
UIView * superView;
 
UIView * backgroundView;
 
UIImageView * backgroundImage;
 
UIView * contentView;
 
BOOL closed;
 
}
 
 
 
 
@ Property (nonatomic, retain) UIView * superView;
 
@ Property (nonatomic, retain) UIView * backgroundView;
 
@ Property (nonatomic, retain) UIImageView * backgroundImage;
 
@ Property (nonatomic, retain) UIView * contentView;
 
 
 
 
-(CustomWindow *) initWithView :( UIView *) aView;
 
-(Void) show;
 
-(Void) close;
 
 
 
 
@ End
 
 
CustomWindow. m:
 
# Import "CustomWindow. h"
 
 
 
 
@ Implementation CustomWindow
 
 
 
 
@ Synthesize superView;
 
@ Synthesize backgroundView;
 
@ Synthesize backgroundImage;
 
@ Synthesize contentView;
 
 
 
 
-(UIImage *) pngWithPath :( NSString *) path
 
{
 
NSString * fileLocation = [[NSBundlemainBundle] pathForResource: path ofType: @ "png"];
 
NSData * imageData = [NSDatadataWithContentsOfFile: fileLocation];
 
UIImage * img = [UIImageimageWithData: imageData];
 
Return img;
 
}
 
 
 
 
-(CustomWindow *) initWithView :( UIView *) aView
 
{
 
If (self = [superinit]) {
 

 
// Content view
 
Self. contentView = aView;
 

 
// Initialize the home screen
 
[SelfsetFrame: [[UIScreenmainScreen] bounds];
 
Self. windowLevel = UIWindowLevelStatusBar;
 
Self. backgroundColor = [UIColorcolorWithRed: 0 green: 0 blue: 0 alpha: 0.1];
 

 
// Add the Root view and set the background to transparent.
 
UIView * rv = [[UIViewalloc] initWithFrame: [selfbounds];
 
Self. superView = rv;
 
[SuperViewsetAlpha: 0.0f];
 
[Self addSubview: superView];
 
[Rv release];
 

 
// Set the background view.
 
CGFloat offset =-6.0f;
 
UIView * bv = [[UIViewalloc] initWithFrame: CGRectInset (CGRectMake (0, 0, self. contentView. bounds. size. width, self. contentView. bounds. size. height), offset, offset)];
 
Self. backgroundView = bv;
 
[Bv release];
 

 
// Set the png Image with rounded corners as the pop-up background.
 
UIImageView * bi = [[UIImageViewalloc] initWithImage: [[selfpngWithPath: @ "alert_window_bg"] stretchableImageWithLeftCapWidth: 13.0 topCapHeight: 9.0];
 
Self. backgroundImage = bi;
 
[BackgroundImagesetFrame: [backgroundViewbounds];
 
[BackgroundViewinsertSubview: backgroundImageatIndex: 0];
 

 
[BackgroundViewsetCenter: CGPointMake (superView. bounds. size. width/2, superView. bounds. size. height/2)];
 
[SuperViewaddSubview: backgroundView];
 

 
CGRect frame = CGRectInset ([backgroundViewbounds],-1 * offset,-1 * offset );
 

 
// View the content
 
[BackgroundViewaddSubview: self. contentView];
 
[Self. contentViewsetFrame: frame];
 

 
Closed = NO;
 

 
}
 
Returnself;
 
}
 
 
 
 
// Display the pop-up window
 
-(Void) show
 
{
 
[SelfmakeKeyAndVisible];
 
[SuperView setAlpha: 1.0f];
 
}
 
 
 
 
-(Void) dialogIsRemoved
 
{
 
Closed = YES;
 
[ContentViewremoveFromSuperview];
 
ContentView = nil;
 
[BackgroundViewremoveFromSuperview];
 
BackgroundView = nil;
 
[SuperViewremoveFromSuperview];
 
SuperView = nil;
 
[Self setAlpha: 0.0f];
 
[SelfremoveFromSuperview];
 
Self = nil;
 

 
// NSLog (@ "==>%s, % s, % d", _ FUNCTION __, _ FILE __, _ LINE __);
 
}
 
 
 
 
-(Void) close
 
{
 
[UIViewsetAnimationDidStopSelector: @ selector (dialogIsRemoved)];
 
[SuperView setAlpha: 0.0f];
 
// NSLog (@ "==>%s, % s, % d", _ FUNCTION __, _ FILE __, _ LINE __);
 
}
 
Next, add the custom CustomWindow object member in CustomAlertWindowViewController, and add a button attribute and Press Me on the XIB interface! The button is connected, for example:
 
 
 
 
At this point, CMD + R is running, which is the effect of the first figure. The occupy button does not respond because we did not add the corresponding action for the button, next, click the button to bring up a window, which is displayed by the custom window customWindow in CustomAlertWindowViewController. To view the code in customWindow, you need to add a rounded PNG image as the background. The PNG image does not have to be the same size as the content view contentView. You only need four rounded corners and solid colors in the middle, to use a commonly used function CG_EXTERNCGRect CGRectInset (CGRect rect, CGFloat dx, CGFloat dy), this method can achieve four corners unchanged, pull between the centers, for more information about this function, see APPLE's official documentation! Add alert_window_bg.png to project Supporting Files.
 
 
Read CustomWindow carefully. m code, it is not difficult to see that the CustomWindow needs to input a view as the contentView. In fact, this part of code is displayed on a blog on the "right of water" on the Internet, the code in is only slightly changed, but not much changed. For details, refer to the blog.
 
Well, let's just talk about it. Next we will create a new view, which we will use xib to implement, that is, ContentView. xib, as shown in sequence:
 
 
 
 
 
 
 
Note that the background of the Root view of ContentView. xib must be set to clear color, that is, transparent. Next, we will use this in CustomAlertWindowViewController to initialize the view in the xib as a parameter to the contentView of customWindow. First, we need to dynamically load the xib file. The specific code used is as follows:
 
-(Void) viewDidLoad
 
{
 
[SuperviewDidLoad];
 
// Do any additional setup after loading the view, typically from a nib.
 
[ButtonaddTarget: selfaction: @ selector (buttonAction :) forControlEvents: UIControlEventTouchUpInside];
 
}
 
 
 
 
-(Void) buttonAction :( id) sender {
 
NSLog (@ "++ doAction executing. ++ ");
 
NSArray * nib = [[NSBundlemainBundle] loadNibNamed: @ "ContentView" owner: selfoptions: nil];
 
UIView * tmpContentView = [nibobjectAtIndex: 0];
 

 
UIButton * tmpButton = (UIButton *) [tmpContentViewviewWithTag: 2];
 
[TmpButton addTarget: selfaction: @ selector (okAction :) forControlEvents: UIControlEventTouchUpInside];
 

 
CustomWindow = [[CustomWindowalloc] initWithView: tmpContentView]; // pass the view in the newly loaded xib as a parameter to the contentView of CustomWindow.
 
[CustomWindowshow];
 

 
}
 
 
 
 
-(Void) okAction :( id) sender {
 
NSLog (@ "++ okAction executing. ++ ");
 
[CustomWindowclose];
 
CustomWindow. hidden = true;
 
}
 
 
Finally, run the command to display the effect of the second image.
Because of the time, the introduction is not very detailed, but the key points are all introduced. The core of it is the implementation of CustomWindow. Here is just for future reference and a record will be kept, A big difference between application development and underlying development is that there are a lot of application knowledge points, which are fragmented and need to be accumulated constantly for fear of forgetting. So let's record them here!
Demo: http://www.bkjia.com/uploadfile/2012/0225/20120225115002326.rar
 
From Code Heaven

Related Article

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.