自定义AlertView

(注:需要添加 CoreGraphics.framework)

CustomAlertViewDelegate.h文件

CustomAlertViewDelegate.h
1 #import <Foundation/Foundation.h>
2 @class CustomAlertView;
3 
4 @protocol CustomAlertViewDelegate <NSObject>
5 -(void) alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
6 @end

CustomAlertView.h文件

CustomAlertView.h
 1 #import <UIKit/UIKit.h>
 2 #import "CustomAlertViewDelegate.h"
 3 
 4 @interface CustomAlertView : UIView{
 5   
 6 }
 7 
 8 @property (nonatomic, retain) id<CustomAlertViewDelegate> alertViewDelegate;
 9 @property (nonatomic, retain) UIView *customSuperView;
10 @property (nonatomic, retain) UIView *customEmptyView;
11 
12 - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<CustomAlertViewDelegate>*/)delegate superView:(UIView *) superView  cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles;
13 
14 -(void) show;
15 
16 -(void) buttonClicked:(id) sender;
17 
18 @end

CustomAlertView.m文件

CustomAlertView.m
 1 #import "CustomAlertView.h"
 2 
 3 #import <QuartzCore/QuartzCore.h>
 4 #import <QuartzCore/CAAnimation.h>
 5 
 6 @implementation CustomAlertView
 7 
 8 @synthesize alertViewDelegate;
 9 @synthesize customSuperView;
10 @synthesize customEmptyView;
11 
12 - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<CustomAlertViewDelegate>*/)delegate superView:(UIView *) superView  cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles{
13     self = [super init];
14     CALayer *layer=[self layer];
15     [layer setCornerRadius:10];
16     [layer setBackgroundColor:[[UIColor orangeColor] CGColor]];
17     if (self) {
18         //设置标题
19         UILabel *titleLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 2, 270, 40)];
20         [titleLable setTextAlignment:UITextAlignmentCenter];
21         [titleLable setText:title];
22         [titleLable setBackgroundColor:[UIColor orangeColor]];
23         [self addSubview:titleLable];
24         [titleLable release];
25         
26         //设置Message
27         UIFont *font=[UIFont fontWithName:@"Arial" size:22];
28         CGSize msgSize= [message sizeWithFont:font constrainedToSize:CGSizeMake(270, 100) 
29                                 lineBreakMode:UILineBreakModeCharacterWrap];
30         
31         UILabel *msgLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 50, 270, msgSize.height)];
32         msgLable.text=message;
33         msgLable.font=font;
34         msgLable.lineBreakMode=UILineBreakModeCharacterWrap;
35         msgLable.textAlignment=UITextAlignmentCenter;
36         [msgLable setBackgroundColor:[UIColor orangeColor]];
37         [msgLable setNumberOfLines:0];
38         [self addSubview:msgLable];
39         [msgLable release];
40         
41         //添加按钮
42         UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeCustom];
43         [btnCancel setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
44         [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
45         [btnCancel setFrame:CGRectMake(5, msgSize.height+60, 100, 30)];
46         [btnCancel setTag:101];
47         [btnCancel addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
48         [self addSubview:btnCancel];
49         
50         UIButton *btnOk=[UIButton buttonWithType:UIButtonTypeCustom];
51         [btnOk setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
52         [btnOk setTitle:@"确定" forState:UIControlStateNormal];
53         [btnOk setFrame:CGRectMake(170, msgSize.height+60, 100, 30)];
54         [btnOk addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
55         [self addSubview:btnOk];
56         
57         self.customSuperView=superView;
58         self.alertViewDelegate=delegate;
59         [self setFrame:CGRectMake(20, 150, 280, msgSize.height+100)];
60     }
61    
62     return self;
63 }
64 
65 -(void) show{
66     UIView *emptyView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
67     self.customEmptyView=emptyView;
68     [emptyView release];
69     CATransition *transition=[CATransition animation];
70     transition.timingFunction=UIViewAnimationCurveEaseInOut;
71     transition.type=kCATransitionPush;
72     transition.subtype=kCATransitionFromTop;
73     transition.duration=0.5;
74     
75     [self.layer addAnimation:transition forKey:nil];
76     
77     [self.customSuperView addSubview:self.customEmptyView];
78     
79     [self.customSuperView addSubview:self];
80 }
81 
82 -(void) buttonClicked:(id) sender{
83     UIButton *button=(UIButton *) sender;
84     
85     if (self.alertViewDelegate!=nil) {
86         [self.customEmptyView removeFromSuperview];
87         if (button.tag==101) {
88             [self.alertViewDelegate alertView:self clickedButtonAtIndex:0];
89         }else {
90              [self.alertViewDelegate alertView:self clickedButtonAtIndex:1];
91         }
92         [self removeFromSuperview];
93     }
94 
95 }
96 
97 @end

所用到的图片:

 

运行结果:

posted on 2012-04-25 16:07  iYiming  阅读(483)  评论(0编辑  收藏  举报

导航