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

  1. //  
  2. //  ABCustomAlertView.h  
  3. //  KnowledgeChoice  
  4. //  
  5. //  Created by  on 13-6-19.  
  6. //  Copyright (c) 2013年 DoubleMan. All rights reserved.  
  7. //  自定義相應的控件及代理就可以用了。。  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ABCustomAlertView : UIWindow  
  12.   
  13. // 顯示  
  14. - (void)show;  
  15. // 消失  
  16. - (void)dismiss;  
  17.   
  18. @end  

  1. //  
  2. //  ABCustomAlertView.m  
  3. //  KnowledgeChoice  
  4. //  
  5. //  Created by on 13-6-19.  
  6. //  Copyright (c) 2013年 DoubleMan. All rights reserved.  
  7. //  
  8.   
  9. #import "ABCustomAlertView.h"  
  10.   
  11. @implementation ABCustomAlertView  
  12.   
  13. - (id)initWithFrame:(CGRect)frame  
  14. {  
  15.     self = [super initWithFrame:frame];  
  16.     if (self) {  
  17.         // Initialization code  
  18.         self.windowLevel = UIWindowLevelAlert;  
  19.         // 這裏,不能設置UIWindow的alpha屬性,會影響裏面的子view的透明度,這裏我們用一張透明的圖片  
  20.         // 設置背影半透明  
  21.         self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"alert_bg.png"]];  
  22.   
  23.           
  24.         UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)] autorelease];  
  25.         view.backgroundColor = [UIColor blackColor];  
  26.         view.center = CGPointMake(160, 240);  
  27.   
  28.         [self addSubview:view];  
  29.     }  
  30.       
  31.     return self;  
  32. }  
  33.   
  34. - (void)show {  
  35.     [self makeKeyAndVisible];  
  36. }  
  37.   
  38. - (void)dismiss {  
  39.     [self resignKeyWindow];  
  40.     [self release];  
  41. }  
  42.   
  43. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  44.     // 點擊消失  
  45.     [self dismiss];  
  46. }  
  47.   
  48. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
  49.       
  50. }  
  51.   
  52. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
  53.       
  54. }  
  55.   
  56. @end  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章