code4App有很多伟大的上方UI特效代码,,好牛逼啊,这效果,太炫了,哇,怎么自己写不出来.事实上,再炫的特效,都是依据苹果系统的框架而来,假设我们了解系统框架实现的原理,也就能写出属于自己自己定义的控件,加上各种各样的动画.

这里,我就展示一个自己定义的UIAlertView效果控件,视图出现的时候动画-先放大-再缩小-最后成正常比例,消失的时候缩小加渐隐.调用也非常方便,不须要像系统先创建后[alert show],我在类内部就已经写好了,仅仅须要alloc创建,调用各个button相应的响应block即可.

@.h

#import <UIKit/UIKit.h>

typedef void(^Myblcok)();

@interface CustomAlertView : UIView

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle firstButtonTitles:(NSString *)firstTitle senondButtonTitles:(NSString *)secondTitle thirdButtonTitles:(NSString *)thirdTitle;

//  利用block将button的点击事件传出去
@property (nonatomic,copy)Myblcok cancelBlock;
@property (nonatomic,copy)Myblcok firstBlcok;
@property (nonatomic,copy)Myblcok secondBlock;
@property (nonatomic,copy)Myblcok thirdBlock; @end @interface UIImage (colorful) + (UIImage *)imageWithColor:(UIColor *)color; @end

@.m

//
// CustomAlertView.m
// CustomAlertView
//
// Created by 胡明涛 on 14-5-6.
// Copyright (c) 2014年 胡明涛. All rights reserved.
// #import "CustomAlertView.h" // 屏幕的物理高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height // 屏幕的物理宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define AlertViewHeight 260
#define AlertViewWidth 200 @interface CustomAlertView () @property (nonatomic,strong) UIView *backgroundView; // 底部View,阻挡其它事件响应
@property (nonatomic,strong) UILabel *titleLabel; // 标题
@property (nonatomic,strong) UIButton *cancelButton; // 取消 @end @implementation CustomAlertView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} - (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle firstButtonTitles:(NSString *)firstTitle senondButtonTitles:(NSString *)secondTitle thirdButtonTitles:(NSString *)thirdTitle{ self = [super initWithFrame:CGRectMake((ScreenWidth - AlertViewWidth)/2.0, (ScreenHeight-AlertViewHeight)/2 ,AlertViewWidth, AlertViewHeight)];
if (self) { [self createCustomAlertView];
self.titleLabel.text = title;
[self.cancelButton setTitle:cancelTitle forState:UIControlStateNormal]; if (thirdTitle != nil) { [((UIButton *)[self viewWithTag:200]) setTitle:firstTitle forState:UIControlStateNormal];
[((UIButton *)[self viewWithTag:201]) setTitle:secondTitle forState:UIControlStateNormal];
[((UIButton *)[self viewWithTag:202]) setTitle:thirdTitle forState:UIControlStateNormal]; }else{ [((UIButton *)[self viewWithTag:200]) setTitle:firstTitle forState:UIControlStateNormal];
((UIButton *)[self viewWithTag:200]).frame = CGRectMake(10, 60, self.bounds.size.width-20, 40);
[((UIButton *)[self viewWithTag:201]) setTitle:secondTitle forState:UIControlStateNormal];
((UIButton *)[self viewWithTag:201]).frame = CGRectMake(10, 130, self.bounds.size.width-20, 40);
[((UIButton *)[self viewWithTag:202]) removeFromSuperview]; } __block CustomAlertView * SELF = self;
[UIView animateWithDuration:0.2 animations:^{ SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.5, 0.5); }completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3); } completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0); } completion:^(BOOL finished) { }];
}]; }]; } // 阻碍其它响应事件
self.backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_backgroundView.backgroundColor = [UIColor blackColor];
_backgroundView.alpha = 0.3;
[[UIApplication sharedApplication].keyWindow addSubview:_backgroundView]; [[UIApplication sharedApplication].keyWindow addSubview:self]; return self;
} - (void)createCustomAlertView{ self.backgroundColor = [UIColor whiteColor]; self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.bounds.size.width, 40)];
_titleLabel.textColor = [UIColor redColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_titleLabel]; // 取消按钮
self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
_cancelButton.frame = CGRectMake(10,AlertViewHeight - 50,self.bounds.size.width-20,40);
_cancelButton.tag = 100;
[_cancelButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:227.0/255.0 green:100.0/255.0 blue:83.0/255.0 alpha:1]] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:@selector(didClickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_cancelButton]; for (int i = 0; i < 3; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 20 +i*60, self.bounds.size.width-20, 40);
button.tag = 200 + i;
[button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:87.0/255.0 green:135.0/255.0 blue:173.0/255.0 alpha:1]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(didClickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
} } - (void)didClickButtonAction:(UIButton *)button{ switch (button.tag) {
case 100:
if (_cancelBlock) { _cancelBlock();
[self dismissAlertView];
}
break;
case 200: if (_firstBlcok) { _firstBlcok();
[self dismissAlertView];
}
break;
case 201: if (_secondBlock) { _secondBlock();
[self dismissAlertView];
}
break;
case 202: if (_thirdBlock) { _thirdBlock();
[self dismissAlertView];
}
break;
default:
break;
} } // 取消视图
- (void)dismissAlertView{ [UIView animateWithDuration:1.0 animations:^{ self.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.0, 0.0);
self.alpha = 0.0;
self.backgroundView.alpha = 0.0;
}completion:^(BOOL finished) { self.cancelBlock = nil;
self.firstBlcok = nil;
self.secondBlock = nil;
self.thirdBlock = nil;
[_backgroundView removeFromSuperview];
[self removeFromSuperview];
_backgroundView = nil; }]; } @end @implementation UIImage (colorful) + (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;
} @end

@调用

- (void)didClickButtonAction{

    CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"HMT" cancelButtonTitle:@"取消" firstButtonTitles:@"first" senondButtonTitles:@"second" thirdButtonTitles:@"third"];
[alertView setCancelBlock:^(){ NSLog(@"点击了取消button");
}];
[alertView setFirstBlcok:^(){ NSLog(@"点击了firstbutton");
}];
//............下面相似
}

@效果:(是不是认为有的时候也能取代UIActionSheet)

版权声明:本文博客原创文章。博客,未经同意,不得转载。

它们的定义UIAlertView的更多相关文章

  1. iphone:自定义UIAlertView

    由于项目中有这样一个需求:需要在保存是弹出框选择保存的地点.选择UIAlertView来实现,但是要在UIAlertView中增加UISwitch的控件,这就需要自定义一个继承UIAlertView的 ...

  2. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  3. iOS之常用宏定义

    下面我为大家提供一些常用的宏定义! 将这些宏定义 加入到.pch使用 再也不用 用一次写一次这么长的程序了 //-------------------获取设备大小------------------- ...

  4. JDStatusBarNotification和一些宏定义

    // //  AddTopicViewController.m //  vMeet2 // //  Created by 张源海 on 16/6/30. //  Copyright © 2016年 h ...

  5. iOS宏定义的使用与规范

    宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中 定义尺寸类的宏 D ...

  6. objective-c 宏定义UIAlertController公用方法

    IOS的方法经常都有更迭,以前弹出框使用 AlertView,现在使用UIAlertController AlertView的宏定义 #define showMessage(__MESSAGE__) ...

  7. iOS开发中常用到的宏定义

    1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7系统判断: #define IsIOS7 ([[[UIDevice cu ...

  8. iOS常用宏 定义

    总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...

  9. 高效的iOS宏定义

    iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性:将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便 ...

随机推荐

  1. java实现代理domino web邮件下载

    [背景] 近日在研究domino web邮件下载功能,下载的邮件能够导入foxmail.outlook邮件client,下载的邮件格式为eml. [參考代码](须要下载jmail.jar包) priv ...

  2. Windows IOT

    Windows IOT 开发入门(准备工作)   终于抽出空来了,将最近研究的东西记录下来,物联网,万物皆可联网.然后可以做到智能家居,智能生活,智能城市....一大堆.吹牛的就不说了. 在实际应用中 ...

  3. WPF 3D:MeshGeometry3D的定义和光照

    原文 WPF 3D:MeshGeometry3D的定义和光照 由于WPF计算光照会根据整个平面的方向向量,所以如果在不同面上使用同一个点可能会达到不同的光照效果.让我们用不同的定义Mesh的方法来演示 ...

  4. HTML5管理与实际历史的分析(history物)

    HTML5新进入历史的管理,更新history对象允许国家的经营历史更方便. 在现代Web应用.用户"前进"和"退却"button切换历史页面.这使得新的页码不 ...

  5. 80x86汇编小站站长简单介绍-2014年08月23日

    [序言] 旧版的"80x86汇编小站站长简单介绍"已经过时了, 因此于2013年10月01日花费1个小时又一次更新和排版一次. [人生格言]  1] 一生都用头脑而不是情绪解决这个 ...

  6. 使用Intellij Idea生成可执行文件jar,开关exe文件步骤

    确保其Java代码是没有问题的,在IDEA常执行的,然后.按以下步骤: 步骤一:打开File -> Project Structure -> Artifacts,例如以下图 步骤二:点击& ...

  7. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  8. 【转】HLSL基础

    原文地址http://blog.csdn.net/chpdirect1984/article/details/1911622 目录 前言 1.HLSL入门 1.1什么是着色器 1.2什么是HLSL 1 ...

  9. Android访问服务器(TOMCAT)乱码引发的问题

    1.浏览器往服务器发送的请求主要可分为2种:get.post:delete.head等不赘述. GET方式: 从浏览器上直接敲地址,最大特点就是参数直接跟在地址后面. POST方式:表单提交等. 2. ...

  10. HDU 2012 素性

    素性 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...