类似于微信首页右上角弹出view

闲来无事,试着写写我的第一篇博客,只为了以后自己找着方便偷笑本人是一个菜鸟级程序猿,没有高级的代码片段,全是基础代码

首先创建一个继承于UIView的类,自行取名,此处我取名为RightView

RightView.h中:

#import <UIKit/UIKit.h>

@class RightView;
@protocol RightViewDelegate <NSObject>
//view 的代理,点击view上某一行执行的动作
- (void)rightView:(RightView *)rightView didSelectedIndex:(NSInteger)index;
@end

typedef void(^dismissCompletion)(void);//定义一个block,用于执行view消失后的操作

@interface RightView : UIView<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, assign) id<RightViewDelegate>delegate;

@property (nonatomic, assign) BOOL isShow;//用来记录view是否是展示状态



- (instancetype)initWithTitleArray:(NSArray *)titleArray imageArray:(NSArray *)imageArray origin:(CGPoint)rightViewPoint  width:(CGFloat)width triangleOrigin:(CGPoint)triangleOrigin;//titleArray:文本信息数据源数组    imageArray:图片数据源数组  rightViewPoint:本身起点坐标  width:本身宽度   triangleOrigin:所属三角起点坐标
- (void)showInView:(UIView *)aView;//展示view
- (void)dismissFromSuperView:(dismissCompletion)completion;//view消失 移除

@end

RightView.m中:

#import "RightView.h"

#define triangleHeight  6

@implementation RightView{
    
    UITableView             *table;
    CGPoint                 rightViewOriginalOrigin;//定义一个point记录view原始位置
    CGFloat                 rightViewOriginalWidth;//定义一个值记录view原始宽度
    CGFloat                 triangleOriginX;//定义一个值记录三角起始坐标的x坐标
    CGRect                  rightViewOriginalFrame;
    NSArray                 *titlesArray;
    NSArray                 *imagesArray;
}

- (instancetype)initWithTitleArray:(NSArray *)titleArray imageArray:(NSArray *)imageArray origin:(CGPoint)rightViewPoint  width:(CGFloat)width triangleOrigin:(CGPoint)triangleOrigin{
    self = [super init];
    if (self) {
        
        titlesArray = titleArray;
        imagesArray = imageArray;
        
        CGRect selfFrame = self.frame;
        selfFrame.origin.x = rightViewPoint.x;
        selfFrame.origin.y = rightViewPoint.y;
        selfFrame.size.width = width;
        selfFrame.size.height = 40*titleArray.count + triangleHeight;
        self.frame = selfFrame;
        
        rightViewOriginalOrigin = rightViewPoint;
        rightViewOriginalWidth = width;
        rightViewOriginalFrame = selfFrame;
        triangleOriginX = triangleOrigin.x;
        
        [self createTableView];
    }
    
    return self;
}

- (void)createTableView{
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, triangleHeight, self.frame.size.width, self.frame.size.height - triangleHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.layer.cornerRadius = 5.0f;
    table.scrollEnabled = NO;
    [self addSubview:table];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return titlesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"rightViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //    取值
    NSString *titleStr = [titlesArray objectAtIndex:indexPath.row];
    NSString *imageNameStr = [imagesArray objectAtIndex:indexPath.row];
    
    //在cell上创建一个imageView显示图片
    UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7.5, 25, 25)];
    [cell.contentView addSubview:cellImage];
    
    //在cell上创建一个label显示文字信息
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, tableView.frame.size.width - 55, 20)];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:titleLabel];
    
    if (imageNameStr != nil && ![imageNameStr isEqualToString:@""]) {
        cellImage.hidden = NO;
        titleLabel.frame = CGRectMake(40, 10, tableView.frame.size.width - 55, 20);
    }
    else{
        cellImage.hidden = YES;
        titleLabel.frame = CGRectMake(10, 10, tableView.frame.size.width - 15, 20);
    }
    
    //在cell上创建一个label用作分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, rightViewOriginalWidth - 15, 0.5)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [cell.contentView addSubview:lineLabel];
    
    
    
    cellImage.image = [UIImage imageNamed:imageNameStr];
    titleLabel.text = titleStr;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_delegate && [_delegate respondsToSelector:@selector(rightView:didSelectedIndex:)]) {
        [_delegate rightView:self didSelectedIndex:indexPath.row];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dismissFromSuperView:nil];
}

- (void)showInView:(UIView *)aView{
    self.frame = CGRectMake(rightViewOriginalOrigin.x, rightViewOriginalOrigin.y, rightViewOriginalWidth, 40*titlesArray.count);
    self.alpha = 1;
    self.isShow = YES;
    [aView addSubview:self];
}

- (void)dismissFromSuperView:(dismissCompletion)completion{
    
    __weak __typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.2 animations:^{

//        CGRect frame = rightViewOriginalFrame;
//        frame.origin.x += 10;
//        frame.size.width -= 10;
//        frame.size.height -= 20;
//        weakSelf.frame = frame;
////        weakSelf.frame = CGRectMake(rightViewOriginalOrigin.x + rightViewOriginalWidth, rightViewOriginalOrigin.y, 0, 0);
        weakSelf.alpha = 0;
        
    }completion:^(BOOL finished) {
        
        [weakSelf removeFromSuperview];
        weakSelf.isShow = NO;
        if (completion) {
            completion();
        }
    }];
    
}

- (void)drawRect:(CGRect)rect{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //画一个三角形
    CGPoint sPoints[3];//坐标点
    sPoints[0] = CGPointMake(triangleOriginX, triangleHeight); //左下角坐标
    sPoints[1] = CGPointMake(triangleOriginX + 5, 0); //上边坐标
    sPoints[2] = CGPointMake(triangleOriginX + 10, triangleHeight);//右下角坐标
    CGContextAddLines(context, sPoints, 3);//添加线
    CGContextClosePath(context);//封起来
    CGContextSetRGBFillColor(context, 255/255.0, 255/255.0, 255/255.0, 1);//内容填充颜色
    CGContextSetRGBStrokeColor(context, 255/255.0, 255/255.0, 255/255.0, 1);//路径填充颜色
    CGContextDrawPath(context, kCGPathFillStroke);//根据坐标绘制路径
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self dismissFromSuperView:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
好,这就是我封装的view 的代码


以下是用法

比如是在ViewController中引用此view,需先导入头文件

#import "RightView.h",并且遵循

RightViewDelegate协议,

在ViewController.m中:

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showRightView)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    rightView = [[RightView alloc] initWithTitleArray:@[@"你好",@"呀呼嗨", @"no"] imageArray:@[@"[10]",@"[11]", @"[12]"] origin:CGPointMake(self.view.frame.size.width - 110, 10 + 64) width:100 triangleOrigin:CGPointMake(75, 0)];
- (void)showRightView{

    if (rightView.isShow) {
        [rightView dismissFromSuperView:nil];
    }
    else{
        rightView.delegate = self;
        [rightView showInView:self.view];
    }
    
}



实现代理方法:
#define RightViewDelegate
- (void)rightView:(RightView *)rightView didSelectedIndex:(NSInteger)index{
    
    NSLog(@"你点击了第%ld行",index);
}

实现的效果就如下图



话不多说,只为自己能看,不为其他微笑


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33423148/article/details/50905266
个人分类: iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

类似于微信首页右上角弹出view

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭