OC(十六):自定义 actionsheet

这几天忙的如🐶,因为习惯了冬天的冷,中午休息小憩一会 ,但是还是有点不是很过瘾,还是周末给力,一觉睡到自然醒,忽然间看到公司微信群老大提的 bug 出现在眼前,心情顿时不好了,老大,这可是周末,是没有工资的 freetime, i want to be a free man!!!
今天首要任务就是 fix bug, 砍了几个但是还是有些问题,明天继续, ono, 今天已经是明天了.洗洗睡了,😴 都是瞎扯一通,莫怪干货在下面,可劲造~

先上图

结果图
层级图

. h

//
//  ShowNumOfSeletedPics.h
//  FileTransfer
//
//  Created by HMC on 16/9/6.
//  Copyright © 2016年 SKing. All rights reserved.
//

#import <UIKit/UIKit.h>



@interface ShowMenuOfBigPics : UIView

@property (copy, nonatomic) void (^btnClickedBlock)(NSInteger index);

//-(instancetype)initWithFrame:(CGRect)frame array:(NSArray * )btnTitleArray;


/** 自定义初始化函数

 @param frame 大小
 @param cancelButtonTitle 取消按钮 不可空
 @param otherButtonTitles 操作类按钮
 @return self
 */
- (instancetype)initWithFrame:(CGRect)frame  cancelButtonTitle:(nonnull NSString *)cancelButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ...  NS_REQUIRES_NIL_TERMINATION;

-(void)showInSuperView:(UIView *)view;

-(void)hide;

@end

.m

//
//  ShowNumOfSeletedPics.m
//  FileTransfer
//
//  Created by HMC on 16/9/6.
//  Copyright © 2016年 SKing. All rights reserved.
//

#define heightOfBtn

#import "ShowMenuOfBigPics.h"


@interface ShowMenuOfBigPics()
//view 的背景
@property (strong, nonatomic) UIView * bgView;
//按钮的背景
@property (strong, nonatomic) UIView * btnBGView;


@property (assign, nonatomic) NSInteger arrayCount;


@end

@implementation ShowMenuOfBigPics


-(instancetype)initWithFrame:(CGRect)frame cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION{
    
    NSMutableArray * arrayTitle = [NSMutableArray array];

    //指向参数的指针变量
    va_list btnTitle;
    //初始化上面的变量 otherButtonTitles 是可选参数的第一个参数
    va_start(btnTitle, otherButtonTitles);
    
    if(otherButtonTitles){
    
        //第一个直接放进数组
        [arrayTitle addObject:otherButtonTitles];
        //循环遍历放入数组
            //返回下一个参数,nsstring 为类型, 并指向下一个参数
        while ((otherButtonTitles = va_arg(btnTitle, NSString *))) {
           
            [arrayTitle addObject:otherButtonTitles];
        }

    }
    NSLog(@"%@",arrayTitle) ;
    
    //释放指针变量
    va_end(btnTitle);
    
    
    
    if (self = [super initWithFrame:frame]) {
        
        _arrayCount = arrayTitle.count;
        
        [self addSubview:self.bgView];
        
        //按钮的背景
        _btnBGView = [[UIView alloc]initWithFrame:(CGRect){0,HEIGHTOFSCREEN ,WIDTHOFSCREEN,(arrayTitle.count + 1 ) * 50 + 5 }];
        
        _btnBGView.backgroundColor = setRGBColor(233, 233, 233, 1.0);
        [self addSubview:_btnBGView];
        
        //创建按钮组
        for (int i = 0 ;i <= arrayTitle.count ; i++) {
            
            UIButton * btn = [[UIButton alloc]initWithFrame:(CGRect){0, 50.5 * i,WIDTHOFSCREEN,50}];
            
            
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = i ;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i != arrayTitle.count) {
                
                [btn setTitle:arrayTitle[i] forState:UIControlStateNormal];
                
            }else{
                
                
                btn.frame = (CGRect){0, 50.5 * i + 5,WIDTHOFSCREEN,50};
                [btn setTitle:cancelButtonTitle forState:UIControlStateNormal];
                
            }
            
            [_btnBGView addSubview:btn];
        }
        
        
    }

    return self;
}



-(UIView *)bgView{

    if (!_bgView) {
        
        //_bgView添加手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        
        _bgView = [[UIView alloc]initWithFrame:(CGRect){0,0,WIDTHOFSCREEN,HEIGHTOFSCREEN - 55}];
        _bgView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        [_bgView addGestureRecognizer:tap];
    }
    
    return _bgView;
}


-(void)btnClicked:(UIButton *)btn {
    
    NSLog(@"点击了按钮");
    //[self hide];
    self.btnClickedBlock(btn.tag);
    
    
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
}

-(void)showInSuperView:(UIView *)view{
    
    
    [view addSubview:self];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.btnBGView.frame = (CGRect){0,HEIGHTOFSCREEN - (self.arrayCount + 1 ) * 50 - 5,WIDTHOFSCREEN,(self.arrayCount + 1 ) * 50 + 5 };
        
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)hide{
    
    self.bgView.hidden = YES;
    
    [UIView animateWithDuration:1.0 animations:^{
        
        self.frame = (CGRect){0,HEIGHTOFSCREEN ,WIDTHOFSCREEN ,0};
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
    
}
@end

调用

ShowMenuOfBigPics * menuView = [[ShowMenuOfBigPics alloc] initWithFrame:(CGRect){0,0,WIDTHOFSCREEN,HEIGHTOFSCREEN} cancelButtonTitle:@"取消" otherButtonTitles:@"保存图片",nil];
    
    [menuView showInSuperView:self.view];
    
    weaklySelf(menuView);
    menuView.btnClickedBlock = ^(NSInteger index){

        switch (index) {
            case 0:
                NSLog(@"保存图片");
                [self savePicToLib:self.currentImage];
                
                break;
            case 1:
            {
                NSLog(@"取消");
                
            }
                break;
            default:
                break;
        }
        
        [weakSelf hide];
        
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值