解析code4app上自定义AlerView(开源学习)
来源:互联网 发布:淘宝网手风琴 编辑:程序博客网 时间:2024/04/29 21:44
/*感谢来自开源Code4App的demo DXAlertView,来让我学到了很多*//*好了我们开始看看他们是怎么实现自己的demo的呢 呵呵*/
</pre><pre name="code" class="objc">DXAlerView.h@interface DXAlertView : UIView/*创建alerview上面的信息以及button*/- (id)initWithTitle:(NSString *)title contentText:(NSString *)content leftButtonTitle:(NSString *)leftTitle rightButtonTitle:(NSString *)rigthTitle;/*将以创建好的CustomAlerView进行显示在window上*/- (void)show;/*dispatch_block_t 定义一个返回为空的block typedef void (^dispatch_block_t)(void);*/@property (nonatomic, copy) dispatch_block_t leftBlock;@property (nonatomic, copy) dispatch_block_t rightBlock;@property (nonatomic, copy) dispatch_block_t dismissBlock;@end@interface UIImage (colorful)/*将UIColor转化为UIImage*/+ (UIImage *)imageWithColor:(UIColor *)color;@end
DXAlerView.m#define kAlertWidth 245.0f#define kAlertHeight 160.0f@interface DXAlertView (){ BOOL _leftLeave;}@property (nonatomic, strong) UILabel *alertTitleLabel;@property (nonatomic, strong) UILabel *alertContentLabel;@property (nonatomic, strong) UIButton *leftBtn;@property (nonatomic, strong) UIButton *rightBtn;@property (nonatomic, strong) UIView *backImageView;@end@implementation DXAlertView+ (CGFloat)alertWidth{ return kAlertWidth;}+ (CGFloat)alertHeight{ return kAlertHeight;}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}#define kTitleYOffset 15.0f#define kTitleHeight 25.0f#define kContentOffset 30.0f#define kBetweenLabelOffset 20.0f/*创建显示的控件*/- (id)initWithTitle:(NSString *)title contentText:(NSString *)content leftButtonTitle:(NSString *)leftTitle rightButtonTitle:(NSString *)rigthTitle{ if (self = [super init]) { self.layer.cornerRadius = 5.0; self.backgroundColor = [UIColor whiteColor]; self.alertTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kTitleYOffset, kAlertWidth, kTitleHeight)]; self.alertTitleLabel.font = [UIFont boldSystemFontOfSize:20.0f]; self.alertTitleLabel.textColor = [UIColor colorWithRed:56.0/255.0 green:64.0/255.0 blue:71.0/255.0 alpha:1]; [self addSubview:self.alertTitleLabel]; CGFloat contentLabelWidth = kAlertWidth - 16; self.alertContentLabel = [[UILabel alloc] initWithFrame:CGRectMake((kAlertWidth - contentLabelWidth) * 0.5, CGRectGetMaxY(self.alertTitleLabel.frame), contentLabelWidth, 60)]; self.alertContentLabel.numberOfLines = 0; self.alertContentLabel.textAlignment = self.alertTitleLabel.textAlignment = NSTextAlignmentCenter; self.alertContentLabel.textColor = [UIColor colorWithRed:127.0/255.0 green:127.0/255.0 blue:127.0/255.0 alpha:1]; self.alertContentLabel.font = [UIFont systemFontOfSize:15.0f]; [self addSubview:self.alertContentLabel]; CGRect leftBtnFrame; CGRect rightBtnFrame;#define kSingleButtonWidth 160.0f#define kCoupleButtonWidth 107.0f#define kButtonHeight 40.0f#define kButtonBottomOffset 10.0f if (!leftTitle) { rightBtnFrame = CGRectMake((kAlertWidth - kSingleButtonWidth) * 0.5, kAlertHeight - kButtonBottomOffset - kButtonHeight, kSingleButtonWidth, kButtonHeight); self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.rightBtn.frame = rightBtnFrame; }else { leftBtnFrame = CGRectMake((kAlertWidth - 2 * kCoupleButtonWidth - kButtonBottomOffset) * 0.5, kAlertHeight - kButtonBottomOffset - kButtonHeight, kCoupleButtonWidth, kButtonHeight); rightBtnFrame = CGRectMake(CGRectGetMaxX(leftBtnFrame) + kButtonBottomOffset, kAlertHeight - kButtonBottomOffset - kButtonHeight, kCoupleButtonWidth, kButtonHeight); self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.leftBtn.frame = leftBtnFrame; self.rightBtn.frame = rightBtnFrame; } [self.rightBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:87.0/255.0 green:135.0/255.0 blue:173.0/255.0 alpha:1]] forState:UIControlStateNormal]; [self.leftBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:227.0/255.0 green:100.0/255.0 blue:83.0/255.0 alpha:1]] forState:UIControlStateNormal]; [self.rightBtn setTitle:rigthTitle forState:UIControlStateNormal]; [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal]; self.leftBtn.titleLabel.font = self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14]; [self.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; self.leftBtn.layer.masksToBounds = self.rightBtn.layer.masksToBounds = YES; self.leftBtn.layer.cornerRadius = self.rightBtn.layer.cornerRadius = 3.0; [self addSubview:self.leftBtn]; [self addSubview:self.rightBtn]; self.alertTitleLabel.text = title; self.alertContentLabel.text = content; UIButton *xButton = [UIButton buttonWithType:UIButtonTypeCustom]; [xButton setImage:[UIImage imageNamed:@"btn_close_normal.png"] forState:UIControlStateNormal]; [xButton setImage:[UIImage imageNamed:@"btn_close_selected.png"] forState:UIControlStateHighlighted]; xButton.frame = CGRectMake(kAlertWidth - 32, 0, 32, 32); [self addSubview:xButton]; [xButton addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside]; self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; } return self;}- (void)leftBtnClicked:(id)sender{ _leftLeave = YES; [self dismissAlert]; if (self.leftBlock) { self.leftBlock(); }}- (void)rightBtnClicked:(id)sender{ _leftLeave = NO; [self dismissAlert]; if (self.rightBlock) { self.rightBlock(); }}/*显示在window上面*/- (void)show{ UIView *topVC = [self appRootWindow]; [topVC addSubview:self];}- (void)dismissAlert{ [self removeFromSuperview]; if (self.dismissBlock) { self.dismissBlock(); }}/*返回当前的程序的主窗口UIWindow*/- (UIView *)appRootWindow{ return [UIApplication sharedApplication].keyWindow;}/*UIView原生的接口 在从父视图移除是调用 进行动画移除 */- (void)removeFromSuperview{ [self.backImageView removeFromSuperview]; self.backImageView = nil; UIView *topVC = [self appRootWindow]; CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.bounds) - kAlertWidth) * 0.5, CGRectGetHeight(topVC.bounds), kAlertWidth, kAlertHeight); [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.frame = afterFrame; if (_leftLeave) { self.transform = CGAffineTransformMakeRotation(-M_1_PI / 1.5); }else { self.transform = CGAffineTransformMakeRotation(M_1_PI / 1.5); } } completion:^(BOOL finished) { [super removeFromSuperview]; }];}/*UIView 原生的接口 在将要进入父视图的时候 在这里进行动画移入*/- (void)willMoveToSuperview:(UIView *)newSuperview{ if (newSuperview == nil) { return; } UIView *topVC = [self appRootWindow]; if (!self.backImageView) { self.backImageView = [[UIView alloc] initWithFrame:topVC.bounds]; self.backImageView.backgroundColor = [UIColor blackColor]; self.backImageView.alpha = 0.6f; self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; } [topVC addSubview:self.backImageView]; self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2); CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(topVC.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight); [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.transform = CGAffineTransformMakeRotation(0); self.frame = afterFrame; } completion:^(BOOL finished) { }]; [super willMoveToSuperview:newSuperview];}@end@implementation UIImage (colorful)/*color 转化为image */+ (UIImage *)imageWithColor:(UIColor *)color{ CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}
0 0
- 解析code4app上自定义AlerView(开源学习)
- 自定义标签的学习(上)
- CocoaPods安装和使用教程(转载自Code4App)
- code4app网址
- 4.AlerView)-1
- PHP学习之路之Smarty自定义函数(上)
- ios学习第六天(一)自定义UIView上
- AngularJS1.X学习笔记8-自定义指令(上)
- Symbian学习笔记——解析XML文件(上)
- [cocos2d-x菜鸟学习记录]解析示例helloWorld(上)
- Yii 2.0学习日记:用户登陆详细解析(上)
- 强化学习ROS实战-RLagent解析(上)
- 自定义ViewPagerIndicator(上)
- Shiro 学习笔记(5)—— 自定义权限解析器和角色解析器
- XML 解析(上)
- Code4app素材网站
- swift 源码网站 code4app
- http://code4app.com/course
- cat access.2012010.log | awk '{a[$1]++} END {for(b in a) print b"\t"a[b]}' | sort -k2 -r | head -n 5
- ubuntu14.04创建超级用户root注意事项
- Codeforces 461C. Appleman and a Sheet of Paper
- POJ-3617
- 自动打包各种渠道的shell
- 解析code4app上自定义AlerView(开源学习)
- Oracle常用SQL
- 【MFC】第3课 MFC程序框架学习笔记
- 【肤色检测 (I)】实测Ycrcb之cr分量+otsu阈值化
- java,hibernate和sql server对应的数据类型
- IOS自动化打包
- Android项目实践
- 微信自定义开发demo
- Spring的国际化