模仿微信的Actionsheet

最近需要写一个类似微信的actionsheet然后就模仿写了个,实现方法简单,下面是代码:

@class GKDActionSheet;
typedef void(^GKDActionSheetBlock)(NSInteger buttonIndex);

@protocol GKDActionSheetDelegate <NSObject>
@optional
/**
 *  新增一个协议方法
 */
- (void)actionSheet:(GKDActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex;

@end

@interface GKDActionSheet : UIView

@property (nonatomic ,copy) GKDActionSheetBlock clickBlock;

@property (nonatomic ,weak) id<GKDActionSheetDelegate> delegate;
#pragma mark - 构造方法
/**
 *  初始化方法
 *
 *  @param buttonTitles 需要以数组形式传入
 *  @param cancleTitle  取消按钮的名称
 *  @param clicked      回调的block
 */

+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle clicked:(GKDActionSheetBlock)clicked;

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle  clicked:(GKDActionSheetBlock)clicked;

/**
 *  初始化方法
 *
 *  @param buttonTitles   传入参数数组
 *  @param cancleTitle    取消按钮
 *  @param redButtonIndex 红色字体按钮
 *  @param clicked        点击的回调
 */
+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex clicked:(GKDActionSheetBlock)clicked;

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex clicked:(GKDActionSheetBlock)clicked;
/**
 *  初始化方法
 *
 *  @param buttonTitles   参数数组
 *  @param cancleTitle    取消按钮
 *  @param redButtonIndex 红色按钮的索引
 *  @param delegate       协议
 */
+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex delegate:(id<GKDActionSheetDelegate>)delegate;

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex delegate:(id<GKDActionSheetDelegate>)delegate;
/**
 *  添加红色字体按钮
 */
@property (nonatomic, assign)NSInteger redButtonIndex;

/**
 *  show的方法
 */
- (void)show;

- (void)showInView:(UIView *)view;
@property (nonatomic, copy) NSString *cancleTitle;

@property (nonatomic, strong) NSMutableArray *buttonTitles;

@property (nonatomic, strong) UIView *maskView;

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) UIWindow *backgroundWindow;

@end

@implementation GKDActionSheet

+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle clicked:(GKDActionSheetBlock)clicked{
    return [[self alloc] initWithButtonTitles:buttonTitles andCancleTitle:cancleTitle clicked:clicked];
}

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle  clicked:(GKDActionSheetBlock)clicked{
    self = [super init];
    if (self) {
        self.redButtonIndex = -1;
        self.cancleTitle = cancleTitle;
        self.buttonTitles = [[NSMutableArray alloc] initWithArray:buttonTitles];
        self.clickBlock = clicked;
    }
    return self;
}

+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex clicked:(GKDActionSheetBlock)clicked {
    return [[self alloc] initWithButtonTitles:buttonTitles andCancleTitle:cancleTitle
                               redButtonIndex:redButtonIndex clicked:clicked];
}

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex clicked:(GKDActionSheetBlock)clicked {

    self = [super init];
    if (self) {
        self.redButtonIndex = redButtonIndex;
        self.cancleTitle = cancleTitle;
        self.buttonTitles = [[NSMutableArray alloc] initWithArray:buttonTitles];
        self.clickBlock = clicked;
    }
    return self;
}
+ (instancetype)sheetWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex delegate:(id<GKDActionSheetDelegate>)delegate{
    return [[self alloc]initWithButtonTitles:buttonTitles andCancleTitle:cancleTitle redButtonIndex:redButtonIndex delegate:delegate];
}

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles andCancleTitle:(NSString *)cancleTitle redButtonIndex:(NSInteger)redButtonIndex delegate:(id<GKDActionSheetDelegate>)delegate{
    self = [super init];
    if (self) {
        self.redButtonIndex = redButtonIndex;
        self.cancleTitle = cancleTitle;
        self.buttonTitles = [[NSMutableArray alloc] initWithArray:buttonTitles];
        self.delegate = delegate;
    }
    return self;
}

- (void)setUpMainView {
    UIView *maskView = [UIView new];
    maskView.alpha = 0;
    maskView.userInteractionEnabled = NO;
    maskView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    maskView.backgroundColor = [UIColor blackColor];
    [self addSubview:maskView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [maskView addGestureRecognizer:tap];
    _maskView = maskView;

    UIView *backgroundView = [UIView new];
    backgroundView.backgroundColor = [UIColor clearColor];

    if (self.buttonTitles.count > 0) {
        for (int i = 0; i < self.buttonTitles.count; i++) {
            // 添加按钮

            if(((NSString *)self.buttonTitles[i]).length == 0){ //为空的话就不显示
                continue;
            }
            UIButton *btn = [UIButton new];
            btn.tag = i;
            btn.backgroundColor = [UIColor whiteColor];
            btn.titleLabel.font = [UIFont systemFontOfSize:17];
            [btn setBackgroundImage:[self createImageWithColor:RGB(217, 217, 217)] forState:UIControlStateHighlighted];
            [btn setTitle:self.buttonTitles[i] forState:UIControlStateNormal];

            //红色文字
            UIColor *titleColor = nil;
            if (i == self.redButtonIndex) {
                titleColor = DefalutMainColorRed;
            } else {
                titleColor = [UIColor blackColor];
            }

            [btn setTitleColor:titleColor forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];

            CGFloat y = BTN_H * i;
            [btn setFrame:CGRectMake(0, y, SCREEN_W, BTN_H)];

            [backgroundView addSubview:btn];
        }
    }
    for (int i = 0; i < self.buttonTitles.count; i++) {
        // 分割线
        if (i == 0) continue;

        UIView *line = [UIView new];
        line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.6];
        CGFloat y = i * BTN_H;
        line.frame = CGRectMake(0, y, SCREEN_W, DefaultLineWidth);
        [backgroundView addSubview:line];
    }
    //放个取消按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.tag = self.buttonTitles.count;
    cancelBtn.backgroundColor = [UIColor whiteColor];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [cancelBtn setTitle:self.cancleTitle forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn setBackgroundImage:[self createImageWithColor:RGB(217, 217, 217)] forState:UIControlStateHighlighted];
    [cancelBtn addTarget:self action:@selector(didClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];

    CGFloat y = BTN_H * self.buttonTitles.count + 5.0f;
    cancelBtn.frame = CGRectMake(0, y, SCREEN_W, BTN_H);
    [backgroundView addSubview:cancelBtn];

    CGFloat bottomH = BTN_H * self.buttonTitles.count + BTN_H + 5.0f;
    //放在屏幕下
    [backgroundView setFrame:CGRectMake(0, SCREEN_H, SCREEN_W, bottomH)];
    _backgroundView = backgroundView;

    self.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
}

- (void)didClickCancelBtn{

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //变下去
        _maskView.alpha = 0;
        _maskView.userInteractionEnabled = NO;
        CGRect frame = _backgroundView.frame;
        frame.origin.y += frame.size.height;
        _backgroundView.frame = frame;

    } completion:^(BOOL finished) {
        if ([self.delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) {
            [self.delegate actionSheet:self didClickedButtonAtIndex:self.buttonTitles.count];
        }
        __weak typeof(self) weakSelf = self;
        if (self.clickBlock) {
            self.clickBlock(weakSelf.buttonTitles.count);
        }
        self.backgroundWindow.hidden = YES;
        [self removeFromSuperview];
    }];

}
- (void)didClickBtn:(UIButton *)btn {
    [self dismiss:nil];
    if ([self.delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self didClickedButtonAtIndex:btn.tag];
    }
    if (self.clickBlock) {
        self.clickBlock(btn.tag);
    }
}

- (void)dismiss:(UITapGestureRecognizer *)tap {

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //变下去
        _maskView.alpha = 0;
        _maskView.userInteractionEnabled = NO;
        CGRect frame = _backgroundView.frame;
        frame.origin.y += frame.size.height;
        _backgroundView.frame = frame;

    } completion:^(BOOL finished) {

        self.backgroundWindow.hidden = YES;

        [self removeFromSuperview];
    }];
}

- (void)showInView:(UIView *)view {
    [self setUpMainView];
    [self addSubview:self.backgroundView];
    [view addSubview:self];

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //从下面冒出来
        _maskView.alpha = 0.5;
        _maskView.userInteractionEnabled = YES;
        CGRect frame = _backgroundView.frame;
        frame.origin.y -= frame.size.height;
        _backgroundView.frame = frame;

    } completion:nil];
}

- (void)show {
    [self setUpMainView];

    self.backgroundWindow.hidden = NO;
    [self addSubview:self.backgroundView];
    [self.backgroundWindow addSubview:self];

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //从下面冒出来
        _maskView.alpha = 0.5;
        _maskView.userInteractionEnabled = YES;
        CGRect frame = _backgroundView.frame;
        frame.origin.y -= frame.size.height;
        _backgroundView.frame = frame;

    } completion:nil];

}

//生成一个纯色图片
- (UIImage *)createImageWithColor:(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;
}

- (UIWindow *)backgroundWindow {
    if (!_backgroundWindow) {
        _backgroundWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundWindow.windowLevel = UIWindowLevelAlert;
        _backgroundWindow.backgroundColor  = [UIColor clearColor];
        _backgroundWindow.hidden = NO;
    }
    return _backgroundWindow;
}
时间: 2024-10-15 20:19:24

模仿微信的Actionsheet的相关文章

ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,还有Android等支持也比较好,如果你是搞Android的或以后准备走Android,建议还是用zxing的比较好,毕竟都一个母亲(goole)生的,QRCode就不用说了吧,虽说技术无国界,但是国人还是有点.... 好,言归正传,java用ZXi

使用swift写sprite Kit的模仿微信打飞机游戏

通过写这个东西来学习swift和sprite Kit,参考自https://github.com/croath/PlaneWarOSX,联系了原作者,他也表示感兴趣. 传送门:https://github.com/ljlin/PlaneWarSwift 使用swift写sprite Kit的模仿微信打飞机游戏,布布扣,bubuko.com

模仿微信朋友圈发布时间,将过去时间格式化成xx(秒/分/小时/天)前

模仿微信朋友圈发布时间,使用扩展方法将将过去时间展示成xx(秒/分/小时/天)前,以留言列表中的留言时间为例,先来看一下直接的时间展示效果(date.ToString("yyyy/MM/dd HH:mm:ss")) 感觉很一般,没有什么特别 下面 我们写一个拓展方法,将留言时间格式化成xx(秒/分/小时/天)前 decimal.Truncate(data)//取decimal整数位 public static class HtmlExpansion { //只格式化2天内的时间 pub

Android 模仿微信启动动画

本文内容 环境 项目结构 演示微信启动动画 本文演示微信启动动画.请点击此处下载,自行调试. 下载 Demo 环境 Windows 2008 R2 64 位 Eclipse ADT V22.6.2,Android 4.4.3 SAMSUNG GT-I9008L,Android OS 2.2.2 项目结构 图 1 项目结构 com.example.weixinbootanimationdemo 包,是三个Activity对应的类,分别是启动时停留的 5 秒动画:5 秒后进入 ViewPager 动

Android 模仿微信启动动画(转)

本文内容 环境 项目结构 演示微信启动动画 本文演示微信启动动画.请点击此处下载,自行调试. 顺便抱怨一下,实践性(与研究性质的相对)技术博的“七宗罪”: 第一宗罪,错字连篇,逻辑不清: 第二宗罪,文章冗长,排版混乱: 第三宗罪,拿来主义,问题不在抄,在自己不做验证.模仿是入门和深入学习的.躲不开的第一步: 第四宗罪,文章空洞,没源代码: 第五宗罪,有源代码,但有缺失: 第六宗罪,源代码不全也就算了,还不提供 Demo 下载: 第七宗罪,有 Demo 可以下载,但他 NND 的还要积分. 下载

(转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,还有Android等支持也比较好,如果你是搞Android的或以后准备走Android,建议还是用zxing的比较好,毕竟都一个母亲(gool

本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI(转载)

首页 资讯 精华 论坛 问答 博客 专栏 群组 更多 ▼ 您还未登录 ! 登录 注册 机遇&速度 博客 微博 相册 收藏 留言 关于我 android 仿微信聊天界面,以及语音录制功能 博客分类: android 录音 android 录音android 仿微信聊天界面android 仿微信录音UIandroidandroid 语音 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图:     第一:chat.xml设计 Xml代码   <?xml version=&q

最新模仿微信(凡信) ——Android源码

最新模仿微信(凡信) 这又是一个模仿微信的,不多确实很像,大家可以看看,用的eclipse 下载地址:http://www.devstore.cn/code/info/956.html 运行截图:    

ListView下拉刷新模仿微信眼睛

ListView下拉刷新模仿微信眼睛 下载地址:http://www.devstore.cn/code/info/747.html 运行截图: