它们的定义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

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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • winscp、xshell连接不上,网络错误连接xx被拒绝

    winscp、xshell连接不上,网络错误连接xx被拒绝winscp网络错误连接被拒绝。解决方法:1、关闭windows的防火墙。一般用于提示网络问题导致的连接不上。2、清除ssh连接缓存密码~/.ssh文件夹下,直接暴力删除known_hosts文件,或打开文件删除对应ip连接保存的秘钥。3、linux清除缓存密码https://blog.csdn.net/weixin_34910922/article/details/1158751784、wincp上清除缓存…

  • Java applet详解

    Java applet详解1.为啥使用applet?如果不是因为计算机二级或是某些该死的考试中需要出题,,我想我是不会理会这中东西的,毕竟这货淘汰了,为啥使用?为了考试。注:applet是和html或者是jsp一起使用的,不能单独运行(当然你可以使用appletviewer命令或者是ide去运行),具体的使用将在代码中体现。2.applet生命周期初始化init():在这个方法中可以设置一些初始值…

  • 运行时异常与非运行时异常的区别

    运行时异常与非运行时异常的区别

  • nuget无法下载_百度云下载慢的最佳解决办法

    nuget无法下载_百度云下载慢的最佳解决办法解决NuGet下载太慢的问题

  • linux软件_LINUX教程

    linux软件_LINUX教程常用指令ls      显示文件或目录   -l     列出文件详细信息l(list)   -a     列出当前目录下所有文件及目录,包括隐藏的a(all)mkdir    创建目录   -p     创建目录,若无父目录,则创建p(parent)cd       切

  • 1192啥意思_有向图的拓扑排序算法

    1192啥意思_有向图的拓扑排序算法由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出,Yali Company总经理Mr.Z心情好,决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。于是Mr.Z下令召开 m 方会谈。每位参加会谈的代表提出了自己的意见:“我认为员工 a 的奖金应该比 b 高!”Mr.Z决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。每位员工奖金最少为100元,且必须是整数。输入格式第一行包含整数 n,m,分别表示公司内员工数以及参会代表数。接下来 m

发表回复

您的电子邮箱地址不会被公开。

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