类似于微信首页右上角弹出view
2016-03-16 15:09
393 查看
闲来无事,试着写写我的第一篇博客,只为了以后自己找着方便本人是一个菜鸟级程序猿,没有高级的代码片段,全是基础代码首先创建一个继承于UIView的类,自行取名,此处我取名为RightViewRightView.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消失 移除 @endRightView.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);}实现的效果就如下图话不多说,只为自己能看,不为其他
相关文章推荐
- 微信模板消息 群发 突破 4条限制
- 微信浏览器取消缓存的方法
- Android接入微信支付的坑
- ittun.com微信公众平台本地测试方案 URL 80端口限制
- 【微信第三方平台】授权实现及所使用的框架解析
- 体验了一天企业微信,你的工作以后可能离不开它
- 体验了一天企业微信,你的工作以后可能离不开它
- C# 微信支付证书使用
- 微信消息回复 该公众号暂时无法提供服务请稍后再试
- 微信接收消息与返回给微信的消息中的tousername、fromusername的值
- 微信分享收藏(包括接入)
- 微信授权步骤与详解 -- c#篇
- Force.com微信开发系列 WeChat 扫描带参数二维码事件
- 仿微信popupwindow的实现方式
- 记录微信分享不成功的可能原因
- 淘宝上大量贩卖微信号,这些人买这么多微信号目的是
- JS 控制VIEWPORT在微信上正常显示
- popwindow下拉菜单 显示位置问题 类似微信的菜单栏
- 百度翻译新API C#代码(winfrom,webform....)小程序
- 微信服务项目表