你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS學習:用UIWindow自定義AlertView(最基本代碼)

IOS學習:用UIWindow自定義AlertView(最基本代碼)

編輯:IOS開發綜合

[cpp]
//  
//  ABCustomAlertView.h  
//  KnowledgeChoice  
//  
//  Created by  on 13-6-19.  
//  Copyright (c) 2013年 DoubleMan. All rights reserved.  
//  自定義相應的控件及代理就可以用了。。  
 
#import <UIKit/UIKit.h>  
 
@interface ABCustomAlertView : UIWindow 
 
// 顯示  
- (void)show; 
// 消失  
- (void)dismiss; 
 
@end 

//
//  ABCustomAlertView.h
//  KnowledgeChoice
//
//  Created by  on 13-6-19.
//  Copyright (c) 2013年 DoubleMan. All rights reserved.
//  自定義相應的控件及代理就可以用了。。

#import <UIKit/UIKit.h>

@interface ABCustomAlertView : UIWindow

// 顯示
- (void)show;
// 消失
- (void)dismiss;

@end
[cpp]
//  
//  ABCustomAlertView.m  
//  KnowledgeChoice  
//  
//  Created by on 13-6-19.  
//  Copyright (c) 2013年 DoubleMan. All rights reserved.  
//  
 
#import "ABCustomAlertView.h"  
 
@implementation ABCustomAlertView 
 
- (id)initWithFrame:(CGRect)frame 

    self = [super initWithFrame:frame]; 
    if (self) { 
        // Initialization code  
        self.windowLevel = UIWindowLevelAlert; 
        // 這裡,不能設置UIWindow的alpha屬性,會影響裡面的子view的透明度,這裡我們用一張透明的圖片  
        // 設置背影半透明  
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"alert_bg.png"]]; 
 
         
        UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)] autorelease]; 
        view.backgroundColor = [UIColor blackColor]; 
        view.center = CGPointMake(160, 240); 
 
        [self addSubview:view]; 
    } 
     
    return self; 

 
- (void)show { 
    [self makeKeyAndVisible]; 

 
- (void)dismiss { 
    [self resignKeyWindow]; 
    [self release]; 

 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // 點擊消失  
    [self dismiss]; 

 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     

 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
     

 
@end 

//
//  ABCustomAlertView.m
//  KnowledgeChoice
//
//  Created by on 13-6-19.
//  Copyright (c) 2013年 DoubleMan. All rights reserved.
//

#import "ABCustomAlertView.h"

@implementation ABCustomAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.windowLevel = UIWindowLevelAlert;
        // 這裡,不能設置UIWindow的alpha屬性,會影響裡面的子view的透明度,這裡我們用一張透明的圖片
        // 設置背影半透明
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"alert_bg.png"]];

       
        UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)] autorelease];
        view.backgroundColor = [UIColor blackColor];
        view.center = CGPointMake(160, 240);

        [self addSubview:view];
    }
   
    return self;
}

- (void)show {
    [self makeKeyAndVisible];
}

- (void)dismiss {
    [self resignKeyWindow];
    [self release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 點擊消失
    [self dismiss];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   
}

@end

這只是基本的思路,調用的時候只要調show和dismiss就可以顯示、消失了。。。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved