思路:传入一个底层的view,将悬浮按钮(用view实现)和展开的子按钮列表add在其上,子按钮列表开始将坐标和悬浮按钮对应好后先将其隐藏,悬浮按钮识别到tap手势后触发展示子按钮列表的方法。通过在touchMove中实现子按钮列表和悬浮按钮的中心坐标同步更新,实现同时一起拖动,其中限定了悬浮按钮在手指拿起后的坐标,通过在touchEnd作判断调整。关键点是tap手势有时会将touchBegan之后的方法砍断,在调用tap的回调函数后会执行touchCancel方法,所以tap的回调函数中必须带上touchEnd中的功能,将UI的效果恢复和位置纠正。

源码:

#import "SuspendedButton.h"
#import <QuartzCore/QuartzCore.h> #define BUTTON_AVAILABLE @"BUTTONAVAILABLE" @interface SuspendedButton ()
{
BOOL _isShowingButtonList;
BOOL _isOnLeft;
BOOL _isBackUp;
BOOL _isTest;
CGPoint _tempCenter;
CGPoint _originalPosition;
CGRect _windowSize;
UIView *_buttonListView;
UIView *_baseView;
} @property (nonatomic,retain) UIView *buttonListView;
@property (nonatomic,retain) UIView *baseView;
@end @implementation SuspendedButton
@synthesize buttonListView = _buttonListView;
@synthesize baseView = _baseView; static SuspendedButton *_instance = nil; #pragma mark - 继承方法
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
_isShowingButtonList = NO;
//_isBackUp = NO;
//self.hidden = YES;
//_isTest = NO;
//[self httpRequest]; [[NSNotificationCenter defaultCenter] removeObserver:self name:BUTTON_AVAILABLE object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showButton) name:BUTTON_AVAILABLE object:nil];
}
return self;
} - (void)dealloc
{
[_buttonListView release];
[_baseView release];
  
    [[NSNotificationCenterdefaultCenter] removeObserver:self]; [super dealloc];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"touchBegan"); _originalPosition = [[touches anyObject] locationInView:self];
_tempCenter = self.center; self.backgroundColor = [UIColor blueColor]; CGAffineTransform toBig = CGAffineTransformMakeScale(1.2, 1.2); [UIView animateWithDuration:0.1 animations:^{
// Translate bigger
self.transform = toBig; } completion:^(BOOL finished) {}];
} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"touchMove"); CGPoint currentPosition = [[touches anyObject] locationInView:self];
float detaX = currentPosition.x - _originalPosition.x;
float detaY = currentPosition.y - _originalPosition.y; CGPoint targetPositionSelf = self.center;
CGPoint targetPositionButtonList = _buttonListView.center;
targetPositionSelf.x += detaX;
targetPositionSelf.y += detaY;
targetPositionButtonList.x += detaX;
targetPositionButtonList.y += detaY; self.center = targetPositionSelf;
_buttonListView.center = targetPositionButtonList;
} - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"touchCancell"); // 触发touchBegan后,tap手势被识别后会将touchMove和touchEnd的事件截取掉触发自身手势回调,然后运行touchCancell。touchBegan中设置的按钮状态在touchEnd和按钮触发方法两者中分别设置还原。
} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"touchEnd"); CGAffineTransform toNormal = CGAffineTransformTranslate(CGAffineTransformIdentity, /1.2, /1.2);
CGPoint newPosition = [self correctPosition:self.frame.origin]; [UIView animateWithDuration:0.1 animations:^{ // Translate normal
self.transform = toNormal;
self.backgroundColor = [UIColor greenColor]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.4 animations:^{
self.frame = CGRectMake(newPosition.x, newPosition.y, self.bounds.size.width, self.bounds.size.height);
[self adjustButtonListView];
}]; }];
} #pragma mark - 类方法
+ (SuspendedButton *)suspendedButtonWithCGPoint:(CGPoint)pos inView:(UIView *)baseview
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[SuspendedButton alloc] initWithCGPoint:pos];
_instance.baseView = baseview;
[_instance constructUI];
[baseview addSubview:_instance];
[_instance release];
}); return _instance;
} + (void)deleteSuspendedButton
{
[_instance removeFromSuperview];
} #pragma mark - 辅助方法
- (id)initWithCGPoint:(CGPoint)pos
{
_windowSize = [SDKFunctionLib rectFromWinSize_CurrentWinSize]; //封装了获取屏幕Size的方法 CGPoint newPosition = [self correctPosition:pos]; return [self initWithFrame:CGRectMake(newPosition.x, newPosition.y, , )];
} - (CGPoint)correctPosition:(CGPoint)pos
{
CGPoint newPosition; if ((pos.x + > _windowSize.size.width) || (pos.x > _windowSize.size.width/-)) {
newPosition.x = _windowSize.size.width - ;
_isOnLeft = NO;
} else {
newPosition.x = ;
_isOnLeft = YES;
} (pos.y + > _windowSize.size.height)?(newPosition.y = _windowSize.size.height - ):((pos.y < )?newPosition.y = :(newPosition.y = pos.y)); return newPosition;
} - (void)constructUI
{
self.backgroundColor = [UIColor greenColor];
self.alpha = 0.6;
self.layer.cornerRadius = ; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tiggerButtonList)];
tap.numberOfTapsRequired = ;
tap.numberOfTouchesRequired = ;
[self addGestureRecognizer:tap];
[tap release]; NSUInteger numOfButton = ;
self.buttonListView = [[[UIView alloc] initWithFrame:CGRectMake(, , numOfButton*, )] autorelease];
_buttonListView.backgroundColor = [UIColor blueColor];
_buttonListView.alpha = ;
_buttonListView.layer.cornerRadius = ; [self createButtonByNumber:numOfButton withSize:CGSizeMake(, ) inView:(UIView *)_buttonListView];
_buttonListView.hidden = YES;
[_baseView addSubview:_buttonListView];
} - (void)createButtonByNumber:(NSUInteger)number withSize:(CGSize)size inView:(UIView *)view
{
//子按钮的UI效果自定义
for (NSUInteger i = ; i < number; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(optionsButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i + ;
button.frame = CGRectMake( + i*size.width, , size.width, size.height);
button.tintColor = [UIColor redColor];
[view addSubview:button];
}
} - (void)adjustButtonListView
{
if (_isOnLeft) {
_buttonListView.frame = CGRectMake(, self.center.y - , _buttonListView.bounds.size.width, _buttonListView.bounds.size.height);
} else {
_buttonListView.frame = CGRectMake((_windowSize.size.width - - _buttonListView.bounds.size.width), self.center.y - , _buttonListView.bounds.size.width, _buttonListView.bounds.size.height);
}
} #pragma mark - 按钮回调
- (void)tiggerButtonList
{
//NSLog(@"tiggerTap"); _isShowingButtonList = !_isShowingButtonList; CGAffineTransform toNormal = CGAffineTransformTranslate(CGAffineTransformIdentity, /1.2, /1.2);
[UIView animateWithDuration:0.1 animations:^{
// Translate normal
self.transform = toNormal;
self.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) { [UIView animateWithDuration:0.1 animations:^{
self.center = _tempCenter;
[self adjustButtonListView];
} completion:^(BOOL finished) { [UIView animateWithDuration:0.5 animations:^{
_buttonListView.hidden = !_isShowingButtonList;
_isShowingButtonList ? (_buttonListView.alpha = 0.6) : (_buttonListView.alpha = );
}];
}];
}];
} - (void)optionsButtonPressed:(UIButton *)button
{
//NSLog(@"buttonNumberPressed:%d",button.tag); switch (button.tag) {
case :
break;
case :
break;
case :
break;
case :
break;
default:
break;
}
} #pragma mark - 网络请求方法
-(void)httpRequest
{
NSMutableString *tempURL = [[NSMutableString alloc] init];
NSString *requesURL = URL_SUSPENDED_BUTTON;
//自定义需求的目标url [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
//封装了网络请求的方法
[HttpRequest getRequestWithURLString:tempURL isSynchronism:NO andFinishHandle:^(NSData *data, NSError *error) { if (error)
{ if (!_isBackUp)
{
_isBackUp = YES;
[self httpRequest];
}
else
{
_isBackUp = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
} else {
_isBackUp = NO;
NSDictionary *requestDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:Nil];
NSString *code = [requestDic objectForKey:@"code"]; if ([code isEqualToString:@""]) {
[[NSNotificationCenter defaultCenter] postNotificationName:BUTTON_AVAILABLE object:nil];
}
}
}];
[tempURL release];
} #pragma mark - 网络请求回调
- (void)showButton
{
self.hidden = NO;
} @end

效果:

(IOS)悬浮按钮Demo的更多相关文章

  1. FloatingActionButtonDemo【悬浮按钮的使用,顺带snackBar的使用】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 FloatingActionButton简称FAB. 一. 对于App或某个页面中是否要使用FloatingActionButton ...

  2. WPF实现窗体中的悬浮按钮

    WPF实现窗体中的悬浮按钮,按钮可拖动,吸附停靠在窗体边缘. 控件XAML代码: <Button x:Class="SunCreate.Common.Controls.FloatBut ...

  3. 在TableView上添加悬浮按钮

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...

  4. Android FloatingActionButton(FAB) 悬浮按钮

    FloatingActionButton 悬浮按钮                                                                            ...

  5. Android用悬浮按钮实现翻页效果

    今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子. 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.LayoutParams.那么在An ...

  6. 移除IOS下按钮的原生样式

    写WAP页面的时候  一定要加上这组样式,以避免在IOS下面按钮被系统原生样式影响 input,textarea {outline-style:none;-webkit-appearance:none ...

  7. 如何在TableView上添加悬浮按钮

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...

  8. Ion-affix & Ion-stick 仿IOS悬浮列表插件

    Ion-affix & Ion-stick 仿IOS悬浮列表插件 Ion-affix 1.相关网页 Ion-affix 2.环境准备: 执行命令 bower install ion-affix ...

  9. Android 5.0新控件——FloatingActionButton(悬浮按钮)

    Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...

随机推荐

  1. python正则表达式实例

    1.将"(332.21)luck李."中(332.21)抽取出来同时能够 将”(23)luck李.“中的(23)抽取出来 pp = re.compile('(\(\d*(.\d*) ...

  2. [转]spring 监听器 IntrospectorCleanupListener简介

    "在服务器运行过程中,Spring不停的运行的计划任务和OpenSessionInViewFilter,使得Tomcat反复加载对象而产生框架并用时可能产生的内存泄漏,则使用Introspe ...

  3. [vc]如何对radio按钮分组

    如何使用多组? 多组和一组是一样的使用,只要搞清楚哪个是哪一组的就行了.再为对话框添加Radio3和Radio4.很简单,先为这些RadioButton排个顺序,就是排列他们的TABORDER.在对话 ...

  4. 关于express4不再支持body-parser

    express的bodyParser能将表单里的数据格式化,bodyParser原是绑定在express中的,但从express4开始,不在绑定了 如果依然直接使用app.use(express.bo ...

  5. CSS3 Media Query

    在移动端火爆的今日,一个好的web应用不仅仅要有对应移动平台的APP,自己web网页也需要对不同屏幕大小的移动设备进行支持,也就是我们所说的响应式web页面. 本篇文章就来介绍下最常见的响应式页面的实 ...

  6. codeforces 632E. Thief in a Shop fft

    题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...

  7. [LeetCode]题解(python):144-Binary Tree Preorder Traversal

    题目来源: https://leetcode.com/problems/binary-tree-preorder-traversal/ 题意分析: 前序遍历一棵树,递归的方法很简单.那么非递归的方法呢 ...

  8. IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)

    利用设备的机载陀螺仪可以实现,当你旋转手机屏幕时,里面的画面不会随着视图更新而移动,以平衡物理运动. 下面例子利用少量简单的几何变换执行该操作.他建立一个运动管理器,订阅设备运动更新,然后基于运动管理 ...

  9. ESX与ESXi管理员必备25个命令

    [转载] 正 如所有的虚拟化管理员都知道,要应对VMware基础设施上需要的更多虚拟化管理,关键在于任务的自动化.虽然VMware ESX主机可以管理与vSphere客户端用户界面,数据中心管理员往往 ...

  10. mysql 函数执行权限

    mysql> show grants for query_all@'115.236.1x0.x'; +---------------------------------------------- ...