zoukankan      html  css  js  c++  java
  • 它们的定义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)


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

  • 相关阅读:
    10分钟教你Python+MySQL数据库操作
    10分钟教你用eclipse上传代码到GitHub
    分享几个有趣的程序代码(有些还是值得学习的哦)~
    10分钟教你生成超高逼格微信朋友圈
    1.maven中pom.xml文件中<exclusions>标签认不到问题
    2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.
    1. SpringBoot启动后,报异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
    9. 一个list拆分成多个list返回
    12. myeclipse遇到Initializing java tooling(1%)终极解决办法
    42. oracle通过两张表的一个字段对应,update其中一张表的某个字段
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4732334.html
Copyright © 2011-2022 走看看