它们的定义UIAlertView

它们的定义UIAlertView

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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

           这里,我就展示一个自己定义的UIAlertView效果控件,视图出现的时候动画-先放大-再缩小-最后成正常比例,消失的时候缩小加渐隐.调用也非常方便,不须要像系统先创建后,我在类内部就已经写好了,仅仅须要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它们的定义UIAlertView

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117256.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【收藏】神器 Nginx 的学习手册

    【收藏】神器 Nginx 的学习手册点击上方阿拉奇学Java,选择设为星标优质文章,及时送达来源:blog.csdn.net/yujing1314/article/details/107000737Nginx是一个高性…

    2022年9月17日
  • pycharm配置python运行环境_pycharm安装django

    pycharm配置python运行环境_pycharm安装django新手下载安装pycharm并且配置pycharm下载安装pycharm电脑是32位就选择32位电脑是64位就选择64位选择next就搞定了配置环境变量第一步、找到pycharm的程序文件的地址右键->属性->复制“起始位置”里面的位置第二步、添加到电脑的环境变量我的电脑->右键->属性->高级系统设置->高级->环境变量->Administrator->找到Path->编辑->新建->粘贴pycharm程序文件地址-&

    2022年8月28日
  • Android多线程研究(4)——从一道面试题说起

    Android多线程研究(4)——从一道面试题说起

    2021年11月28日
  • html实战-制作静态网页「建议收藏」

    html实战-制作静态网页「建议收藏」教程视频:http://edu.csdn.net/course/detail/535 从42开始制作的网页:http://www.cnos.co/整体思路:先布局再CSS控制骨架搭好了,初始化样式,后再弄CSS样式。注意:将CSS的样式导入到外部样式表时,要注意图片的位置,以及对外部样式表的引入  1、头部的背景图片的高度128px来自素材的图片高,容器的宽度大

    2022年9月5日
  • pycharm的安装选项_tomcat环境配置

    pycharm的安装选项_tomcat环境配置pycharm安装以及其环境的配置说明此次我们使用win10系统安装pycharm的64位社区版,并且对Anaconda3中自带的Python3进行环境的配置,如果您没有Anaconda3甚至是没有Python3环境,可以参考Anaconda3安装教程及说明,如果您的pip源未更改,这里推荐您改为使用国内的pip源,这样可以更快的下载组件,方法见修改pip源至国内镜像网站。教程从开始菜单中找到你的AnacondaPrompt并打开…

    2022年8月29日
  • Java学习路线图(完整详细2021版)

    Java学习路线图(完整详细2021版)作为一个男人我感觉必须得做点什么来证明一下自己,现在我又回来了,准备把自己的节操准备补一下 另外给各位未来的Java程序员说一句,别的我不清楚,学习编程请从一而终 咱们学习编程就挺难的,有这些先驱者来带领咱们学习,咱们应该感激,而且最重要的事跟着你选定的一家一直学下去 因为每家学校的学习大纲都是不一样的,但是程序员其实都是一样的,这句话你细品!仔细的品! 我不希望你忙忙碌碌的整理那么多东西,挑肥拣瘦的,最后自己学的东西还是缺失的,要不就…

    2022年5月17日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号