它们的定义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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Excel解密——okfone解密大师

    Excel解密——okfone解密大师Excel工作表为了保护数据,设置了打开密码,时间久了就把密码忘记了,这种情况该怎么办。这个情况可以考虑使用解密软件帮你将工作簿密码找回。okfoneExcel解密大师可以解决密码忘记的问题,使用教程如下:打开okfoneExcel解密大师,点击【找回密码】将Excel文件添加进去,选择找回方法,然后点击【开始】密码找回成功就会在软件界面上显示![…

    2022年6月28日
    124
  • java截取某个字符后面的字符串_java如何截取字符串

    java截取某个字符后面的字符串_java如何截取字符串提示:java截取某个字符之前或者之后的字符串文章目录一、java截取某个字符之前或者之后的字符串:1.截取”_”之前字符串2.截取”_”之后字符串二、截取正数第二个”_”后面的内容一、java截取某个字符之前或者之后的字符串:1.截取”_”之前字符串代码如下(示例)://java截取某个字符之前的字符串publicstaticvoidsubstringTest01(){Stringstr=”test_https://www.baidu.com/”;//截

    2022年10月7日
    5
  • 数据仓库之DWD层

    数据仓库之DWD层DWD(DataWareHouseDetail)数据明细层,主要是将从业务数据库中同步过来的ODS层数据进行清洗和整合成相应的事实表。事实表作为数据仓库维度建模的核心,需要紧紧围绕着业务过程来设计。在拿到业务系统的表结构后,进行大概的梳理,再与业务方沟通整个业务过程的流转过程,对业务的整个生命周期进行分析,明确关键的业务步骤,在能满足业务需求的前提下,尽可能设计出更通用的模型。业务方有时只仅仅只是考虑了当下的情况。例如业务想要一个审核通过人员的明细数据,我们设计了一个全量的审核明细表,过了几天,业务

    2022年6月26日
    44
  • webservice发送短信本地部署可以但是服务器部署发送短信息中文乱码

    webservice发送短信本地部署可以但是服务器部署发送短信息中文乱码

    2020年11月9日
    324
  • 网络流量统计技术

    网络流量统计技术一、netstream“NetStreamNetStream技术应用背景Internet的高速发展为用户提供了更高的带宽,支持的业务和应用日渐增多,传统流量统计如SNMP、端口镜像等,由于统计流量

    2022年7月4日
    27
  • WebGPU 初探 – Windows10上Chorme运行WebGPU程序

    WebGPU 初探 – Windows10上Chorme运行WebGPU程序WebGPU 是最新的 Web3D 图形 API 浏览器封装了现代图形 API Dx12 Vulkan Metal 这才是未来的标准 不像 WebGL2 0 苹果直接不支持 好 开始今天的分享 点击链接查看是否支持 WebGPU 首先分享链接 W3CWebGPU 组织 WebGPU 目前的进展可以查看 WebGPU 最新的进展和平台支持情况 WebGPU 文档 WebGPU 的官方介绍 说明比较详细 Web

    2025年7月2日
    4

发表回复

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

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