它们的定义UIAlertView

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)

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

时间: 2024-11-13 10:04:22

它们的定义UIAlertView的相关文章

iphone:自定义UIAlertView

由于项目中有这样一个需求:需要在保存是弹出框选择保存的地点.选择UIAlertView来实现,但是要在UIAlertView中增加UISwitch的控件,这就需要自定义一个继承UIAlertView的类来自定义UIAlertView了. 实现效果如下:(还没加图的) 我需要在点击确定的时候,知道两个Switch的状态,才能进一步做相应的功能. 自定义了SaveAlertView类. 在.h中,需要自定义一个@protocol,作为把switch状态传出去的出口. 声明相应的委托.看源码 #imp

ios:UIAlertView自动消失

在写程序的过程中用到很多提示的信息,于是非常自然地就要使用UIAlertView控件.但是这些提示的信息有时候只需提示就行,不用操作,那么此时就要这个提示框自动消失就OK了. UIAlertView弹出后2s让其自动消失,两种方法: (1)结合NSTimer 定义UIAlertView *baseAlert; - (void) performDismiss: (NSTimer *)timer { [baseAlert dismissWithClickedButtonIndex:0 animate

UIAlertView, UIAlertViewController

iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺寸变化效果(比如说旋转)中发挥了重要的作用,它有效地节省了程序员们的工作量(天地良心啊).还有,某 些旧的UIKit控件也同样发生了许多变化,比如说Alert Views.Action Sheets.Popovers以及Search Bar Controllers.本文将会对Alert Views和

(转)iOS 常用宏定义

#ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小------------------------- //NavBar高度 #define NavigationBar_HEIGHT 44 //获取屏幕 宽度.高度 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UI

UITableView与UIAlertView的 Delegate方法实现

一 UITableView Delegate 方必须遵循 UITableViewDelegate协议 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 这句是定义cell右边的尖角号 #pragma mark - 代理方法 #pragma mark 返回indexPath这行cell的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtInd

提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一,包含通知中心的使用)

iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了-但是也无所谓,有新东西不怕,学会使用了就行.接下来会探讨一下这些个新的Controller. - (void)showOkayCancelAlert { NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);

iOS开发中使用宏定义提高开发效率

iOS开发中使用宏定义提高开发效率 (2013-07-10 10:47:33) 转载▼ iOS开发中,巧妙的使用宏定义,可以提高开发效率,本篇简单介绍一下宏的定义,设置,应用,并在未来实践中不断追加一些常用的宏定义. 调试Log iPhone应用程序开发调试的时候,在代码中加入NSLog的暴力调试方法是很频繁的,但是在release的时候要删除这些调试代码,那工作量是烦躁,这样的情况下,试用宏就会显得非常的方便. 看下面的例子: #ifdef DEBUG #define LOG(...) NSL

iOS宏定义的使用与规范

http://my.oschina.net/leejan97/blog/354904 宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中,下面我抛砖引玉,对一些常用的宏进行分类.分文件定义,希望对大家有所帮助. 定义尺寸类的宏 DimensMacros.h //状态栏高度 #define STATUS_BAR_HEIGHT 20 //NavBar高度 #define

ios中使用宏定义进行调试

第一种,在控制台上输出日志信息: #ifdef DEBUG #define DLog(format,...) NSLog((@"DLog %s - [Line %d] %s\n\n" format), __PRETTY_FUNCTION__,__LINE__, __FUNCTION__,##__VA_ARGS__) #else #define DLog(format,...) do {} while(0) #endif 让NSLog只在debug build的时候起作用.将这个功能添加