弹出菜单

封装的一个方法

.h文件中

#import <UIKit/UIKit.h>

@protocol LrdOutputViewDelegate <NSObject>

@required

- (void)didSelectedAtIndexPath:(NSIndexPath *)indexPath;

@end

typedef void(^dismissWithOperation)();

typedef NS_ENUM(NSUInteger, LrdOutputViewDirection) {

kLrdOutputViewDirectionLeft = 1,

kLrdOutputViewDirectionRight

};

@interface LrdOutputView : UIView

@property (nonatomic, weak) id<LrdOutputViewDelegate> delegate;

@property (nonatomic, strong) dismissWithOperation dismissOperation;

//初始化方法

//传入参数:模型数组,弹出原点,宽度,高度(每个cell的高度)

- (instancetype)initWithDataArray:(NSArray *)dataArray

origin:(CGPoint)origin

width:(CGFloat)width

height:(CGFloat)height

direction:(LrdOutputViewDirection)direction;

//弹出

- (void)pop;

//消失

- (void)dismiss;

@end

@interface LrdCellModel : NSObject

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *imageName;

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName;

.m文件中

宏定义

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10)

#define LeftToView 10.f

#define TopToView 10.f

在@interface里面声明属性

最后在@implementation中实现方法

- (instancetype)initWithDataArray:(NSArray *)dataArray

origin:(CGPoint)origin

width:(CGFloat)width

height:(CGFloat)height

direction:(LrdOutputViewDirection)direction {

if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {

//背景色为clearColor

self.backgroundColor = [UIColor clearColor];

self.origin = origin;

self.height = height;

self.width = width;

self.direction = direction;

self.dataArray = dataArray;

if (height <= 0) {

height = 44;

}

if (direction == kLrdOutputViewDirectionLeft) {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y, width, height * _dataArray.count) style:UITableViewStylePlain];

}else {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y, -width, height * _dataArray.count) style:UITableViewStylePlain];

}

_tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];

_tableView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];

_tableView.bounces = NO;

_tableView.layer.cornerRadius = 2;

_tableView.delegate = self;

_tableView.dataSource = self;

//注册cell

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

[self addSubview:self.tableView];

//cell线条

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

[self.tableView setSeparatorInset:CellLineEdgeInsets];

}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

[self.tableView setLayoutMargins:CellLineEdgeInsets];

}

}

return self;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.dataArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return self.height;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

cell.textLabel.textColor = [UIColor whiteColor];

cell.textLabel.font = [UIFont systemFontOfSize:15];

cell.backgroundColor = [UIColor clearColor];

//取出模型

LrdCellModel *model = [self.dataArray objectAtIndex:indexPath.row];

cell.textLabel.text = model.title;

cell.imageView.image = [UIImage imageNamed:model.imageName];

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];

cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];

 return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//通知代理处理点击事件

if ([self.delegate respondsToSelector:@selector(didSelectedAtIndexPath:)]) {

[self.delegate didSelectedAtIndexPath:indexPath];

}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

[self dismiss];

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

[cell setSeparatorInset:CellLineEdgeInsets];

}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:CellLineEdgeInsets];

}

}

//画出尖尖

- (void)drawRect:(CGRect)rect {

//拿到当前视图准备好的画板

CGContextRef context = UIGraphicsGetCurrentContext();

//利用path进行绘制三角形

CGContextBeginPath(context);//标记

if (self.direction == kLrdOutputViewDirectionLeft) {

CGFloat startX = self.origin.x + 20;

CGFloat startY = self.origin.y;

CGContextMoveToPoint(context, startX, startY);//设置起点

CGContextAddLineToPoint(context, startX + 5, startY - 5);

CGContextAddLineToPoint(context, startX + 10, startY);

}else {

CGFloat startX = self.origin.x - 20;

CGFloat startY = self.origin.y;

CGContextMoveToPoint(context, startX, startY);//设置起点

CGContextAddLineToPoint(context, startX + 5, startY - 5);

CGContextAddLineToPoint(context, startX + 10, startY);

}

CGContextClosePath(context);//路径结束标志,不写默认封闭

[self.tableView.backgroundColor setFill]; //设置填充色

[self.tableView.backgroundColor setStroke];

CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path;

}

- (void)pop {

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

[keyWindow addSubview:self];

//动画效果弹出

self.alpha = 0;

CGRect frame = self.tableView.frame;

self.tableView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);

[UIView animateWithDuration:0.2 animations:^{

self.alpha = 1;

self.tableView.frame = frame;

}];

}

- (void)dismiss {

//动画效果淡出

[UIView animateWithDuration:0.2 animations:^{

self.alpha = 0;

self.tableView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);

} completion:^(BOOL finished) {

if (finished) {

[self removeFromSuperview];

if (self.dismissOperation) {

self.dismissOperation();

}

}

}];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if (![touch.view isEqual:self.tableView]) {

[self dismiss];

}

}

@end

#pragma mark - LrdCellModel

@implementation LrdCellModel

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName {

LrdCellModel *model = [[LrdCellModel alloc] init];

model.title = title;

model.imageName = imageName;

return model;

}

时间: 2024-08-23 09:12:44

弹出菜单的相关文章

弹出菜单的创建与使用

-------------siwuxie095 工程名:TestSwingPopupMenu 包名:com.siwuxie095.popupmenu 类名:MyFrame.java 工程结构目录如下: MyFrame.java: package com.siwuxie095.popupmenu; import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.awt.

Android ListView两种长按弹出菜单方式

* 知识点1:ListView item:两种长按弹出菜单方式* 知识点2:ListView SimpleAdapter的使用*  知识点 3:在java代码中创建一个ListView*/ -----------------------------------------------------Activity[mw_shl_code=java,true]package org.gxl.com; public class ListOnLongClickActivity extends Activ

弹出菜单 阻止冒泡

<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>弹出菜单,阻止冒泡</title> <style> </style> <script src="jquery-1.9.1.js"></script> <script> $(fu

糍粑大叔的独游之旅-u3d实现弹出菜单(上)-动态列表

在u3d5.x中,使用ugui作为默认的界面系统,但控件实在太少,很多需求都不能满足,比如弹出菜单(PopupMenu) 我也懒得去网上找现成的实现,再加上现有代码已经有很多有关列表控件的功能,不想再重新动这些代码. 所以自己实现一个,目前先只实现核心.搭建控件相关类的骨干,后期再慢慢丰富和做的更花哨. 开篇之前声明,我的u3d理解非常有限,有很多也许本身自带的功能或有现成库功能我不知道,所以选择了自己探索或实现, 感觉太low欢迎给出好的意见. 定义和代码结构 PopupMenu是点击鼠标或按

第5章(7) 弹出菜单(PopUp Menus)

分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果用户在菜单外单击,则直接消失弹出的菜单.当菜单消失时,会引发DismissEvent事件(利用此事件可在菜单消失时做一些后续处理). 二.示例7-- Demo07PopupMenu 1.运行效果 2.添加菜单项 在Resources文件夹下添加一个menu子文件夹,然后在此子文件夹下添加一个名为de

iOS开发——实战技术OC篇&amp;关于蒙板和弹出菜单

关于蒙板和弹出菜单 一:关于蒙板 蒙板的作用一般就是用来实现不能做其他操作还有一些模糊效果提示用户 我们只需要自定义一个View,并且创建两个类方法用来给外界调用实现显示和隐藏 + (void)show; + (void)hide; 显示:直接添加到window上,并且设置和window同样大小,然后设置相应的透明度(alpla) 1 + (void)show 2 3 { 4 5 6 7 8 9 iCocosCover *cover = [[self alloc] init]; 10 11 12

用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)

     用PopupWindow实现弹出菜单是一个比较好的方式.当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐. 这个实例的效果是这样的:点击按钮后,一个菜单从屏幕的右边滑入到屏幕中,点击按钮/空白处后菜单消失. 布局文件时一个按钮,我就不贴出代码了.下面是菜单的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&

弹出菜单的实现

void CNumEdit::OnRButtonDown(UINT nFlags, CPoint point){ // TODO: 在此添加消息处理程序代码和/或调用默认值 //CEdit::OnRButtonDown(nFlags, point); //弹出菜单 //定义一个菜单类 CMenu popMenu; //加载菜单资源 popMenu.LoadMenu(IDR_MENU1); //屏幕坐标和客户区域坐标转换 GetCursorPos(&point); //弹出菜单 popMenu.G

自己写了一个弹出菜单,有间隙也可以

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

css+html+js实现多级下拉和弹出菜单

本文将使用css+html+js实现横向菜单.具有多级弹出菜单下拉. 首先我们来看看效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgyOTE4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 首先应该写html部分的代码,代码比較简单,代码例如以下: <body> <div id="men