iOS开发用Tableview实现能改变字体颜色的高仿系统的ActionSheet

本文介绍如何在iOS开发中,为满足特定颜色需求,通过自定义UIView来实现一个高仿系统ActionSheet的功能,特别是改变字体颜色。在iOS8以后,由于系统自带的UIActionSheet和UIAlertController无法满足定制需求,作者提供了JoopicActionSheet的头文件、实现文件及Swift使用代理的代码示例,并给出了相关代码仓库链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于项目设计ActionSheet的颜色是橘色,而系统自带的 UIActionSheet 或 UIAlertController 都是只有蓝色或红色,在ios8之后,重写willPresentActionSheet方法时actionSheet.subviews时里没有东西,所以只能自己自定义一个UIView


1. JoopicActionSheet.h

//
//  JoopicActionSheet.h
//  joopic
//
//  Created by jianxiong li on 16/1/14.
//  Copyright © 2016年 joobot. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol JoopicActionSheetDelegate <NSObject>

-(void)didSelectIndex : (NSInteger) index;

@end

@interface JoopicActionSheet : UIView

@property (nonatomic , strong) id<JoopicActionSheetDelegate> delegate;

-(id)initWithList : (NSArray *)list title : (NSString *) title;

-(void) showInView : (UIViewController *)controller;

@end

2.JoopicActionSheet.m

//
//  JoopicActionSheet.m
//  joopic
//
//  Created by jianxiong li on 16/1/14.
//  Copyright © 2016年 joobot. All rights reserved.
//

#import "JoopicActionSheet.h"

#define RGBCOLOR(r, g, b)       [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
//获取设备的物理高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//获取设备的物理宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define JoopicActionSheetItemHeight 57

@interface JoopicActionSheet ()<UITableViewDataSource , UITableViewDelegate , UIGestureRecognizerDelegate>

@property (nonatomic , strong) UITableView *tableview;
@property (nonatomic , strong) NSArray *listData;
@property (nonatomic , strong) NSString * title;
@property (nonatomic , strong) UIView *customerView;

@end

@implementation JoopicActionSheet

-(id) initWithList:(NSArray *)list title:(NSString *)title{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.backgroundColor = RGBACOLOR(0, 0, 0, 0);
        
        //table
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth-20, JoopicActionSheetItemHeight * [list count]) style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.scrollEnabled = NO;
        _tableview.layer.cornerRadius = 15;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //取消按钮
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10,CGRectGetHeight(_tableview.frame)+8, ScreenWidth-20, JoopicActionSheetItemHeight)];
        cancelButton.layer.cornerRadius =15;
 
        cancelButton.layer.backgroundColor = [UIColor whiteColor].CGColor;
        
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"bule_light.png"] forState:UIControlStateHighlighted];
        
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
      
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
        
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        cancelButton.userInteractionEnabled = YES;
        [cancelButton addGestureRecognizer:tapRecognizer];
        

        _customerView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth,CGRectGetHeight(_tableview.frame)+ JoopicActionSheetItemHeight + 18)];
        _customerView.backgroundColor = [UIColor clearColor];
        [_customerView addSubview:_tableview];
        [_customerView addSubview:cancelButton];
        
        UIColor *lineColor = RGBACOLOR(0, 0, 0, 0.3);
        for(int i = 1 ;i<list.count;i++){
            UIView *separateLine = [[UIView alloc]initWithFrame:CGRectMake(10,JoopicActionSheetItemHeight*i, ScreenWidth-20,0.5)];
            separateLine.backgroundColor = lineColor;
            [_customerView addSubview:separateLine];
        }
        
        
        [self addSubview:_customerView];
        
        
        _listData = list;
        _title = title;
        
    }
    return self;
}

//如果tableview处于uiview上面,uiview整个背景有点击事件,但是我们需要如果我们点击tableview的时候,处理tableview的点击事件,而不是uiview的事件。在这里,我们需要判断我们点击事件是否在uiview上还是在uitableview上。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isKindOfClass:[self class]]){
        return YES;
    }
    return NO;
}

-(void)animeData{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
    tapGesture.delegate = self;
    [self addGestureRecognizer:tapGesture];
    self.userInteractionEnabled = YES;
    
    [UIView animateWithDuration:0.25f animations:^{
        self.backgroundColor = RGBACOLOR(0, 0, 0, 0.5);
        CGRect originRect = _customerView.frame;
        originRect.origin.y = ScreenHeight - CGRectGetHeight(_customerView.frame);
        _customerView.frame = originRect;
    } completion:^(BOOL finished) {
        
    }];
}

-(void) tappedCancel{
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0;
        CGRect originRect = _customerView.frame;
        originRect.origin.y = ScreenHeight;
        _customerView.frame = originRect;
    } completion:^(BOOL finished) {
        if (finished) {
            for (UIView *v in _customerView.subviews) {
                [v removeFromSuperview];
            }
            [_customerView removeFromSuperview];
        }
    }];
}

- (void)  showInView:(UIViewController *)controller{
    if (controller) {
        [controller.view addSubview:self];
    }else{
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
    }
    [self animeData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_listData count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return JoopicActionSheetItemHeight;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentifier = @"TitleCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text = _title;
        cell.textLabel.text = [_listData objectAtIndex:indexPath.row] ;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor greenColor];
        cell.textLabel.highlightedTextColor = [UIColor redColor];

        return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self tappedCancel];
    if (_delegate != nil && [_delegate respondsToSelector:@selector(didSelectIndex:)]) {
        [_delegate didSelectIndex:indexPath.row];
        return;
    }
}

@end


3. swift使用代码 实现代理 JoopicActionSheetDelegate

    //JoopicActionSheetDelegate
    func didSelectIndex(index: Int) {
        print(index)
    }



4. swift使用代码 在XXX_header.h中加入 #import "JoopicActionSheet.h"

   

        let listData = NSArray(array: ["拍照","相册选择","从系统相册选择"])
        let joopicActionSheet:JoopicActionSheet = JoopicActionSheet(list: listData as [AnyObject], title: "")
        joopicActionSheet.delegate = self
        joopicActionSheet.showInView(self.parentViewController?.parentViewController) 
        //因为主页在tabBar<span style="font-family: Arial, Helvetica, sans-serif;">中所以要</span><span style="font-family: Arial, Helvetica, sans-serif;">joopicActionSheet.showInView(self.parentViewController?.parentViewController) 否则 </span><span style="font-family: Arial, Helvetica, sans-serif;">joopicActionSheet.showInView(self)</span>


参考原始文档与代码地址:http://blog.youkuaiyun.com/yixiangboy/article/details/46778417    http://code.cocoachina.com/view/126525      https://github.com/yixiangboy/ActionSheetExtension

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值