iOS实现可拖动的浮动菜单
更新时间:2022年08月08日 08:38:32 作者:shenyi0106
这篇文章主要为大家详细介绍了iOS实现可拖动的浮动菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了iOS实现可拖动的浮动菜单的具体代码,供大家参考,具体内容如下
实现一个可拖动的浮动菜单,效果如下:
这个设置图标是可以全屏拖动的,点击一下,可以出现一排设置按钮,可以用来进行功能的开关切换。
废话不多说了,上代码:
// // DragMenuView.h // // Created by Scott on 15-7-27. // Copyright (c) 2015年 yshen. All rights reserved. // #import <UIKit/UIKit.h> @interface DragMenuView : UIView { UIButton *m_selectBtn; UIView *m_contentView; } -(id)initDragMenuView; @end
很简单的头文件,就两个变量,一个初始化函数。
m_selectBtn是一个按钮控件,这个控件用来显示上图中那个螺丝刀和扳手的图片,它可以被任意拖动,只要点击它,就可以打开其他设置按钮。
m_contentView
initDragMenuView这个函数是这个类唯一的一个自有函数,它用来创建这个浮动菜单,并初始化浮动菜单。
下面是实现文件:
// // DragMenuView.m // // // Created by Scott on 15-7-27. // Copyright (c) 2015年 yshen. All rights reserved. // #import <QuartzCore/QuartzCore.h> #import "DragMenuView.h" #define MAX_WIDTH 48 #define MAX_HEIGHT 48 #define SPLIT_SPACE 5 #define SETTING_COUNT 1 @implementation DragMenuView @synthesize bRotation; - (id)initDragMenuView { CGRect mainRect = [[UIScreen mainScreen] bounds]; self = [super initWithFrame:CGRectMake(mainRect.size.width - MAX_WIDTH, /*mainRect.size.height / 2 - MAX_HEIGHT / 2*/50, MAX_WIDTH, MAX_HEIGHT)]; if (self) { // Initialization code self.backgroundColor = [UIColor clearColor]; m_selectBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT)]; [m_selectBtn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal]; [m_selectBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self insertSubview:m_selectBtn atIndex:9500]; m_contentView = [[UIView alloc] initWithFrame:CGRectMake(MAX_WIDTH + SPLIT_SPACE, 0, MAX_WIDTH * SETTING_COUNT, MAX_HEIGHT)]; m_contentView.userInteractionEnabled = YES; m_contentView.hidden = YES; //m_contentView.backgroundColor = [UIColor grayColor]; UIButton *setting = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT)]; [setting setEnabled:NO]; [setting setSelected:NO]; [setting setBackgroundImage:[UIImage imageNamed:@"mic.png"] forState:UIControlStateNormal]; [setting addTarget:self action:@selector(clickVoice:) forControlEvents:UIControlEventTouchUpInside]; [m_contentView addSubview:setting]; /* setting = [[UIButton alloc] initWithFrame:CGRectMake(MAX_HEIGHT, 0, MAX_HEIGHT, MAX_HEIGHT)]; [setting setBackgroundImage:[UIImage imageNamed:@"mic.png"] forState:UIControlStateNormal]; [setting addTarget:self action:@selector(clickAudio:) forControlEvents:UIControlEventTouchUpInside]; [m_contentView addSubview:setting]; setting = [[UIButton alloc] initWithFrame:CGRectMake(MAX_HEIGHT * 2, 0, MAX_HEIGHT, MAX_HEIGHT)]; [setting setBackgroundImage:[UIImage imageNamed:@"mic.png"] forState:UIControlStateNormal]; [setting addTarget:self action:@selector(clickAudio:) forControlEvents:UIControlEventTouchUpInside]; [m_contentView addSubview:setting]; setting = [[UIButton alloc] initWithFrame:CGRectMake(MAX_HEIGHT * 3, 0, MAX_HEIGHT, MAX_HEIGHT)]; [setting setBackgroundImage:[UIImage imageNamed:@"mic.png"] forState:UIControlStateNormal]; [setting addTarget:self action:@selector(clickAudio:) forControlEvents:UIControlEventTouchUpInside]; [m_contentView addSubview:setting]; */ [self insertSubview:m_contentView belowSubview:m_selectBtn]; } return self; } -(void)layoutMenuView:(BOOL)bShow { // 用户点击设置按钮时,需要弹出或者隐藏设置功能视图。这个视图的显示因为有边界问题,所以需要一些算法调整显示位置 CGRect mainRect = [[UIScreen mainScreen] bounds]; CGRect viewRect = [self frame]; int x = 0, y = viewRect.origin.y, w = 0, h = MAX_HEIGHT; if (bShow) // 显示 { w = (MAX_WIDTH * (SETTING_COUNT + 1) + SPLIT_SPACE); if ((viewRect.origin.x + w) > mainRect.size.width) //右边可显示区域不够显示,需要切换到左边来显示 { x = viewRect.origin.x - w + MAX_WIDTH; self.frame = CGRectMake(x, y, w, h); m_contentView.frame = CGRectMake(0, 0, MAX_WIDTH * SETTING_COUNT, MAX_HEIGHT); m_selectBtn.frame = CGRectMake(MAX_WIDTH * SETTING_COUNT + SPLIT_SPACE, 0, MAX_WIDTH, MAX_HEIGHT); } else // 右边显示区域够了,则直接在右边显示 { x = viewRect.origin.x; self.frame = CGRectMake(x, y, w, h); m_selectBtn.frame = CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT); m_contentView.frame = CGRectMake(MAX_WIDTH + SPLIT_SPACE, 0, MAX_WIDTH * SETTING_COUNT, MAX_HEIGHT); } } else //隐藏 { w = MAX_WIDTH; CGRect btnRect = m_selectBtn.frame; if (btnRect.origin.x == 0) //如果是直接右边显示的,则调整宽度即可隐藏复原 { x = viewRect.origin.x; self.frame = CGRectMake(x, y, w, h); } else // 如果是因为可显示区域不够,而调整到左边来显示的话,则需要调整X坐标和宽度。另外还需要重新调整子视图的位置。 { x = viewRect.origin.x + (MAX_WIDTH * SETTING_COUNT + SPLIT_SPACE); self.frame = CGRectMake(x, y, w, h); m_selectBtn.frame = CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT); } } } -(void)click:(id)sender { // 设置按钮的响应动作,根据当前位置和边界情况来显示设置视图 m_contentView.hidden = !m_contentView.hidden; [self layoutMenuView:!(m_contentView.hidden)]; } -(void)clickVoice:(id)sender { // 设置视图中,功能按钮的响应函数 UIButton *btn = (UIButton*)sender; [btn setSelected:![btn isSelected]]; [btn setAlpha:([btn isSelected]) ? 0.5f : 1.0f]; // 为了区分点击和未点击状态 // do somthing...... } @end
实现代码也就那么一点点,都是很简单的代码。
然后在ViewController中加上变量,并创建这个浮动菜单:
DragMenuView *m_dragMenu; // // whether support floating menu // add by yshen on 2015-7-27 // m_dragMenu = [[DragMenuView alloc]initDragMenuView]; UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewDidDragged:)]; [gesture setMaximumNumberOfTouches:1]; [gesture setMinimumNumberOfTouches:1]; [m_dragMenu addGestureRecognizer:gesture]; [self.view insertSubview:m_dragMenu atIndex:9999]; m_dragMenu.hidden = NO;
手势识别函数:
-(void)viewDidDragged:(UIPanGestureRecognizer*)gesture { if (gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) { CGPoint offset = [gesture translationInView:self.view]; int x = m_dragMenu.center.x + offset.x; int y = m_dragMenu.center.y + offset.y; [m_dragMenu setCenter:CGPointMake(x, y)]; [gesture setTranslation:CGPointMake(0, 0) inView:self.view]; } }
浮动菜单置顶:
[self.view bringSubviewToFront:m_dragMenu];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
最新评论