当前位置:编程学习 > wap >>

iPhone开发学习笔记004——自定义背景透明非全屏弹出窗口,子类化UIWindow

最终要实现的效果如下,点击上面的按钮可以弹出一个背景透明非全屏的弹出窗口,不使用UIActionSheet和UIAlertView.

\\


下面说说具体过程。

一、新建一个single view application工程,并且添加相关的控件并拖拽连接:
如下图:\

 

\

新建一个OC类,继承自UIWindow,如下图:

\

\



 
 
 CustomWindow.h:
 
#import <UIKit/UIKit.h>
 
 
 
 
@inte易做图ce 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]) {     
 
      
 
        //内容view
 
        self.contentView = aView;
 
      
 
        //初始化主屏幕
 
        [selfsetFrame:[[UIScreenmainScreen]bounds]];
 
        self.windowLevel =UIWindowLevelStatusBar;
 
        self.backgroundColor = [UIColorcolorWithRed:0green:0blue:0 alpha:0.1];
 
      
 
        //添加根view,并且将背景设为透明.
 
        UIView *rv = [[UIViewalloc]initWithFrame:[selfbounds]];
 
        self.superView = rv;
 
        [superViewsetAlpha:0.0f];
 
        [self addSubview:superView];
 
        [rv release];
 
      
 
        //设置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];
 
  
 
        //用圆角png图片设为弹出窗口背景.
 
        UIImageView *bi = [[UIImageViewalloc]initWithImage:[[selfpngWithPath:@"alert_window_bg"]stretchableImageWithLeftCapWidth:13.0topCapHeight: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
 
        [backgroundViewaddSubview:self.contentView];
 
        [self.contentViewsetFrame:frame];
 
      
 
        closed =NO;
 
     
 
    }
 
    returnself;
 
}
 
 
 
 
//显示弹出窗口
 
-(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
 
{
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,