1、本章博客主要实现几个不同类型的浮动框。第一种:EasyThouch (苹果的小白点):功能近是苹果。第二种:是一种不可移动的浮动按钮,就一个单一的点击事件。第三:是扇形按钮浮动。这是公司App里面使用到三个情况,然后就自己写了一下。茹克满足您的需求,不防Mark一下。

2、效果图:

3、上代码,不多说,大部分注释很详细。

//

//  FloatingButton.h

//  FloatingButton

//

//  Created by MAC on 2016/10/28.

//  Copyright © 2016年 NetworkCode小贱. All rights reserved.

//

#import <UIKit/UIKit.h>

#import "BlackView.h"

typedef NS_ENUM(NSInteger,FloatingButtonType) {

/* 类似苹果的小白点*/

FloatingButtonType_Default = 0,

/* 左边固定扇形按钮*/

FloatingRightDownType,

/* 中间*/

FloatingCenterType

};

@interface FloatingButton : UIImageView<BlackViewDelegate>{

/* 判断是否还在移动*/

UIView * backView;

/*存储按钮*/

NSMutableArray * BtnArray;

}

/* 移动的类型*/

@property(assign,nonatomic)FloatingButtonType FloatingType;

/* 判断是否可以移动*/

@property(assign,nonatomic) BOOL isMove;

/* 创建对象的方法*/

-(instancetype)initWithFrame:(CGRect)frame type:(FloatingButtonType)type;

@end

//

//  FloatingButton.m

//  FloatingButton

//

//  Created by MAC on 2016/10/28.

//  Copyright © 2016年 NetworkCode小贱. All rights reserved.

//

#import "FloatingButton.h"

#import <objc/runtime.h>

@implementation FloatingButton

/* 创建对象*/

-(instancetype)initWithFrame:(CGRect)frame type:(FloatingButtonType)type{

if (self = [super initWithFrame:frame]) {

/*默认移动开启*/

self.userInteractionEnabled = YES;

self.contentMode = UIViewContentModeScaleAspectFit;

/* 添加单机手势*/

[self addTapGestureRecognizer:self sel:@selector(tapClick:)];

self.FloatingType = type;

}

return self;

}

#pragma mark -- 添加单机手势

-(void)addTapGestureRecognizer:(id)tager sel:(SEL)sel{

UITapGestureRecognizer *  TapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:sel];

[tager addGestureRecognizer:TapGestureRecognizer];

}

/* 点击的事件处理*/

-(void)tapClick:(UITapGestureRecognizer*)TapGesturerecognizer{

/*图标的变换处理*/

if (self.FloatingType == FloatingButtonType_Default) {

/* 创建一个背景层*/

if (backView==nil) {

backView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

[[UIApplication sharedApplication].keyWindow addSubview:backView];

}

/*隐藏图标*/

[self addTapGestureRecognizer:backView sel:@selector(showClick:)];

self.hidden = YES;

/* 弹出黑色的图片*/

BlackView * BlackView2 = [[BlackView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.width * 0.5)];

BlackView2.delegate = self;

BlackView2.center = backView.center;

BlackView2.backgroundColor = [UIColor blackColor];

BlackView2.layer.masksToBounds =YES;

BlackView2.layer.cornerRadius = 8;

BlackView2.center = self.center;

[backView addSubview:BlackView2];

[UIView animateWithDuration:0.5 animations:^{

BlackView2.center = backView.center;

}];

}else if (self.FloatingType==FloatingRightDownType){

/* 获取这个对象*/

NSString * ClickStr = @"isClick";

id objectPram =objc_getAssociatedObject(self, &ClickStr);

if (objectPram) {

self.transform = CGAffineTransformMakeRotation(0);

/*

objc_removeAssociatedObjects : 是内联属性,没权删除。只能删除与之相关的所有对象

*/

objc_setAssociatedObject(self, &ClickStr, nil, OBJC_ASSOCIATION_ASSIGN);

objc_removeAssociatedObjects(objectPram);

/* 收起按钮*/

[self setThreeBtnHidden];

}else{

objc_setAssociatedObject(self, &ClickStr, @"Click", OBJC_ASSOCIATION_COPY_NONATOMIC);

self.transform = CGAffineTransformMakeRotation(M_2_PI);

/* 展示按钮*/

[self createThreeBtn];

}

}else if (self.FloatingType==FloatingCenterType){

UIAlertView * Al  = [[UIAlertView alloc]initWithTitle:@"提示" message:@"我被点击了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

[Al show];

}

}

-(void)createThreeBtn{

NSArray * ImageArray = @[[UIImage imageNamed:@"timePic"],[UIImage imageNamed:@"timesPic"],[UIImage imageNamed:@"wenPic"]];

BtnArray = [NSMutableArray arrayWithCapacity:0];

for (int i =0 ; i<3; i++) {

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-55, [UIScreen mainScreen].bounds.size.height-55, 40, 40);

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

btn.tag = i;

[btn setBackgroundImage:ImageArray[i] forState:UIControlStateNormal];

[[UIApplication sharedApplication].keyWindow addSubview:btn];

[self rotationAnimation:btn isDrain:YES];

[UIView animateWithDuration:0.5 animations:^{

if (i==0) {

btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-155, [UIScreen mainScreen].bounds.size.height - 55, 40, 40);

}else if (1==i){

btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-(sqrt(2)*50+20)-35, [UIScreen mainScreen].bounds.size.height -(sqrt(2)*50+20)-35, 40, 40);

}else{

btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-55, [UIScreen mainScreen].bounds.size.height - 155, 40, 40);

}

}];

[BtnArray addObject:btn];

}

}

/*收起动画*/

-(void)setThreeBtnHidden{

for (UIButton * temp in BtnArray) {

[self rotationAnimation:temp isDrain:NO];

[UIView animateWithDuration:0.25 animations:^{

temp.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-55, [UIScreen mainScreen].bounds.size.height-55, 40, 40);

} completion:^(BOOL finished) {

[temp removeFromSuperview];

}];

}

BtnArray = nil;

}

/* 设置按钮的旋转*/

- (void)rotationAnimation:(UIButton*)animBtnView isDrain:(BOOL)isD{

CABasicAnimation* Animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

[animBtnView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];

if (isD) {

[Animation setToValue:@(M_PI*2)];

}else{

[Animation setToValue:@(-M_PI*2)];

}

[Animation setRepeatCount:1];

[Animation setDuration:0.5f];

[animBtnView.layer addAnimation:Animation forKey:@"rotationAnim"];

}

/* 扇形按钮的点击事件*/

-(void)btnClick:(UIButton*)Btn{

NSString * Str = nil;

if (Btn.tag==0) {

Str = @"钟表";

}else if (Btn.tag==1){

Str = @"闹钟";

}else if (Btn.tag ==2){

Str = @"问答";

}

UIAlertView * Al  = [[UIAlertView alloc]initWithTitle:@"提示" message:Str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

[Al show];

}

#pragma mark -- 上方的点击事件

-(void)actionClick:(NSInteger)actionTag{

NSString * Str = nil;

if (actionTag==0) {

Str = @"主屏幕";

}else if (actionTag==1){

Str = @"通知中心";

}else if (actionTag ==2){

Str = @"语音";

}else{

Str = @"收藏";

}

UIAlertView * Al  = [[UIAlertView alloc]initWithTitle:@"提示" message:Str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

[Al show];

}

#pragma mark -- 显示图标

-(void)showClick:(UITapGestureRecognizer*)TapGesturerecognizer{

[backView removeFromSuperview];

backView = nil;

self.hidden = NO;

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

}

*/

#pragma mark -- 移动处理

/* 图标的移动处理*/

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

/* 开始触摸*/

if (self.FloatingType==FloatingButtonType_Default) {

/* 白点变量*/

}

}

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

/* 获取手指*/

UITouch * Touch = [[touches allObjects] firstObject];

/*获取手指和屏幕的结合点*/

CGPoint CurrentPoint = [Touch locationInView:self];

/* 获取上一次手指和屏幕的结合点*/

CGPoint PreviousPoint = [Touch previousLocationInView:self];

/* 获取当前图标的大小*/

float halfWidth = self.bounds.size.width * 0.5;

float halfHight = self.bounds.size.height * 0.5;

float AppWidth = [UIScreen mainScreen].bounds.size.width;

float AppHigth = [UIScreen mainScreen].bounds.size.height;

/*移动开始*/

if (self.FloatingType == FloatingButtonType_Default) {

float x_offset =0.0, y_Offset=0.0;

CGPoint TempPoint = CGPointZero ;

CGPoint selfCenterPoint = self.center;

x_offset =selfCenterPoint.x - PreviousPoint.x + CurrentPoint.x ;

y_Offset =selfCenterPoint.y - PreviousPoint.y + CurrentPoint.y  ;

if (x_offset<=halfWidth && y_Offset<=halfHight) {

TempPoint = CGPointMake(halfWidth, halfHight);

}else if (x_offset<=halfWidth && y_Offset>=AppHigth-halfHight){

TempPoint = CGPointMake(halfWidth, AppHigth - halfHight);

}else if (x_offset>=AppWidth - halfWidth && y_Offset<=halfHight){

TempPoint = CGPointMake(AppWidth-halfWidth,halfHight);

}else if (x_offset>=AppWidth - halfWidth && y_Offset>=AppHigth-halfHight){

TempPoint = CGPointMake(AppWidth-halfWidth,AppHigth-halfHight);

}else if (x_offset<=halfWidth){

TempPoint = CGPointMake(halfWidth,y_Offset);

}else if (y_Offset<=halfHight){

TempPoint = CGPointMake(x_offset,halfHight);

}else if (x_offset>=AppWidth-halfWidth){

TempPoint = CGPointMake(AppWidth - halfWidth ,y_Offset);

}else if (y_Offset>AppHigth-halfHight){

TempPoint = CGPointMake(x_offset, AppHigth-halfHight);

}else if (x_offset>halfWidth&&x_offset<AppWidth&&y_Offset>halfHight&&y_Offset<AppHigth){

TempPoint = CGPointMake(x_offset, y_Offset);

}

self.center = TempPoint;

}else if (self.FloatingType==FloatingRightDownType){

return;

}

}

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

/* 移动取消*/

}

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

/* 移动结束*/

}

@end

4、使用方法

//右下角的按钮

FloatingButton  * RightDownBtn = [[FloatingButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-55, [UIScreen mainScreen].bounds.size.height-55 , 40, 40) type:FloatingRightDownType];

RightDownBtn.isMove = NO;

RightDownBtn.image = [UIImage imageNamed:@"addPic"];

[self.view addSubview:RightDownBtn];

浮动按钮、扇形按钮、EasyThouch相关推荐

  1. android 扇形按钮,扇形按钮控制(PieControl Pro )

    官方网详细介绍 PieControlPro也是一款较为好用的扇形虚似按键,设定起來相对性简易,扇形虚拟按键常见的作用都一应俱全.(来源于酷友安卓系统天尊的发觉)作用表明:*能够 挑选从显示屏左边.右边 ...

  2. Android 遥控器按键 布局,Android 类遥控器圆形控件 扇形按钮 带中间按钮

    Android 类遥控器圆形控件 扇形按钮 带中间按钮 前言 效果图 使用方法 添加依赖 在布局中使用 参数设定 设定是否有中间按钮 设定正常情况下的中间按钮的背景颜色 设定中间按钮的圆圈描边颜色 设 ...

  3. iOS 9应用开发教程之使用代码添加按钮美化按钮

    iOS 9应用开发教程之使用代码添加按钮美化按钮 丰富的用户界面 在iOS9中提供了很多的控件以及视图来丰富用户界面,对于这些视图以及控件我们在上一章中做了简单的介绍.本章我们将详细讲解这些视图. i ...

  4. 纯干货 | UI界面中按钮设计CTA按钮\订阅按钮

    CTA按钮的目的是在鼓励用户采取某种行动的用户界面的互动元素.此操作为特定页面或屏幕提供链接(例如:购买,联系,订阅等),换句话说,它将普通用户变为订阅用户.因此,从技术上讲,它可以是通过号召性用文本 ...

  5. Panel中加入3个 按钮,按钮上分别显示打开,关闭,返回

    Panel中加入3个 按钮,按钮上分别显示打开,关闭,返回 在这里插入代码片 package p1;import java.awt.Color; import java.awt.FlowLayout; ...

  6. cocos2dx点击按钮更改按钮图片

    cocos2dx中有一个加载纹理的方法 //正常态的按钮常用这个 LoadTextureNormal(); //禁用态的按钮 loadTextureDisabled("res/Default ...

  7. 渐变色按钮功能按钮绘制C语言示例

    渐变色按钮功能按钮绘制C语言示例 本文介绍C语言绘图示例: 1.渐变色按钮  绘制  详见drawcover()  2.功能按钮小图形 绘制       详见newgame ()     3.游戏图形 ...

  8. 1.2文字排版、颜色、表格、图像形状、Jumbotron、信息提示框、按钮、按钮组、徽章、加载效果、分页、列表组、卡片、下拉菜单、折叠

    Bootstrap 5 默认设置 Bootstrap 5 默认的 font-size 为 16px, line-height 为 1.5. 此外,所有的 <p> 元素 margin-top ...

  9. 爬虫训练场项目前端之 Bootstrap 信息提示框,按钮与按钮组,徽章,进度条

    爬虫训练场项目前端之 Bootstrap 信息提示框,按钮与按钮组,徽章,进度条 Bootstrap5 信息提示框 Bootstrap 5 按钮和按钮组 Bootstrap 5 徽章 进度条 Boot ...

最新文章

  1. Go 语言编程 — 编码规范指南
  2. python qtablewedgit_PyQt5-高级控件使用(QTableWidget)
  3. shell脚本——字符串 数组
  4. 深入V8引擎-Time模块介绍
  5. 陕西专科学校王牌计算机专业,陕西省高职专科院校排名+王牌专业
  6. 为什么要在沙河中设置小堤坝?
  7. 浙商证券计算机组成原理,中国海洋大学计算机组成原理期末模拟参考答案.doc...
  8. NandFlah 相关知识详解
  9. 最详细iOS打包流程
  10. jumpserver-登录提示Server error occur, contact administrator
  11. java编程导出pdf_java中根据模板生成pdf文件
  12. 企业wifi认证登录靠谱吗
  13. win7 win 2008 R2多系统multi OS
  14. [面面面]搞定计算机面试常见知识点——Java篇
  15. 企业微信社群运营该怎么做?
  16. JavaScript的深浅拷贝
  17. 【FPGA】Spartan®-7器件XC7S75-1FGGA484C、XC7S15-1FTGB196C现场可编程门阵列芯片
  18. 统计_三门问题:贝叶斯解答
  19. Arcgis使用教程(五)ARCGIS空间数据处理之影像镶嵌(拼接)与裁剪
  20. js页面循环input,checked赋值

热门文章

  1. sqlserver2008日志已满解决方法(转载)
  2. caffe-mnist别手写数字
  3. matplotlib的默认字体_matplotlib默认字体设置
  4. 第四届蓝桥杯C++B组国(决)赛真题
  5. python股票量化交易(9)---使用TA-Lib库实现股价走势对比图
  6. [Common]如何判断是usim卡还是sim卡 - MTK物联网在线解答 - 技术论坛
  7. 对于组件的可重用性,大佬给出来6个级别的见解,一起过目一下!
  8. vue已知商家位置调用高德、百度和腾讯地图显示商家位置和导航(a标签href在url后拼接参数)
  9. laravel8 QQ登录
  10. 无线网密码怎么改服务器停用,无线网怎么修改密码?更改无线网密码的操作步骤...