Iphone development my Sina Weibo client-User Login custom pop-up window (1.2)

Source: Internet
Author: User

The purpose of this article is to develop a custom pop-up dialog window component, that is, the translucent information Prompt window shown in Figure 6 above, in fact, UIActionSheet and UIAlertView are already available in the system library for such functional components, but the display looks not very beautiful and is not very well matched with my design, at the same time, the appearance of these two components is poorly defined and almost cannot change the display appearance. Therefore, we decided to develop a component by ourselves. The idea is to create an object that inherits the components of the UIWindow component, in this way, this component is a UIWindow, and then add the corresponding translucent View to this UIWindow to achieve the display effect, when we need to display the pop-up window, we only need to set this UIWindow as the main display window, such as executing the code: [self makeKeyAndVisible]. The idea of implementation is very simple. Now let's get started.

1. Use Xcode to open the previous project and create an Objective-C class file named UIDialogWindow. This UIDialogWindow contains three UIView components from top to bottom and one UIImageView, this is the superView of the Root View, the backgroundView of the masked view, and the backgroundImage of the translucent rounded corner background view, which is used as the view of the display content of the window, this is provided only when the UIDialogWindow is used, because the content displayed by the pop-up window in each place must be very different. Only in this way can the flexibility of components be improved.

2. Open UIDialogWindow. h file. The Code is as follows. In this file, the four views mentioned above are defined and an initialization method and a display window method are defined, A close display window method can basically complete a function task in the pop-up window.

# Import <Foundation/Foundation. h>

# Import <QuartzCore/QuartzCore. h>

# Import "Global. h"

@ Interface UIDialogWindow: UIWindow {

UIView * view; UIView * superView;

UIView * backgroundView;

UIImageView * backgroundImage;

BOOL isClose;

}

@ Property (nonatomic, retain) UIView * view; @ property (nonatomic, retain) UIView * superView; @ property (nonatomic, retain) UIView * backgroundView;

@ Property (nonatomic, retain) UIImageView * backgroundImage;

-(UIDialogWindow *) initWithView :( UIView *) aView;

-(Void) show;-(void) close;

@ End

2. Open UIDialogWindow. m file. First, we will compile the initialization method. The initialization method is to add the four views mentioned above to the window in a certain order. The specific code is as follows, note that the size of the right content view in the pop-up window is determined. In the initialization method, the size of the content view is obtained first, then, adjust the size of the other three views based on the size.

-(UIDialogWindow *) initWithView :( UIView *) aView

{

If (self = [super init])

{

// Content view

Self. view = aView;

[Self setFrame: [[UIScreen mainScreen] bounds]; self. windowLevel = UIWindowLevelStatusBar;

Self. backgroundColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.1];

// Root view

UIView * sv = [[UIView alloc] initWithFrame: [self bounds];

Self. superView = sv;

[SuperView setAlpha: 0.0f];

[Self addSubview: superView];

[Sv release];

CGFloat d =-7.0f;

UIView * bv = [[UIView alloc] initWithFrame: CGRectInset (CGRectMake (0, 0, self. view. bounds. size. width, self. view. bounds. size. height), d, d)]; self. backgroundView = bv;

// View the background of the rounded corner Image

UIImageView * bi = [[UIImageView alloc] initWithImage: [[Global pngWithPath: @ "dialog_bg2"] stretchableImageWithLeftCapWidth: 13.0 topCapHeight: 9.0];

Self. backgroundImage = bi;

[BackgroundImage setFrame: [backgroundView bounds];

[BackgroundView insertSubview: backgroundImage atIndex: 0];

[BackgroundView setCenter: CGPointMake (superView. bounds. size. width/2, superView. bounds. size. height/2)];

[SuperView addSubview: backgroundView];

CGRect frame = CGRectInset ([backgroundView bounds],-1 * d,-1 * d );

// View the content

[BackgroundView addSubview: self. view];

[Self. view setFrame: frame];

IsClose = NO;

[Bv release];

[Bi release];

}

Return self ;}

3. When the initialization method adds these views to the window, the root view is set to transparent ([superView setAlpha: 0.0f]). in this case, we still cannot see the displayed pop-up window because it is transparent. At this time, we are writing a method to display the pop-up window. The implementation of this code is very simple, in fact, you can also consider adding certain animation effects to make the display process more dynamic.

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.