今天有个需求是如上图实现类似微信的自定义浮动操作框效果

我自己就写了个demo,大家感兴趣的可以试试,下面是代码

VC代码如下

#import "TestCustomMenuItemVC.h"
#import "CustomMenuItemView.h" @interface TestCustomMenuItemVC () /** 移动视图,给这个视图来添加浮动窗*/
@property (strong, nonatomic) UIView *moveView; @end @implementation TestCustomMenuItemVC - (UIView *)moveView
{
if (_moveView == nil) {
_moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
_moveView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_moveView];
}
return _moveView;
} - (void)viewDidLoad
{
[super viewDidLoad];
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//解析出随机点击页面的坐标
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.view];
NSLog(@"point x-%@ y-%@", @(point.x), @(point.y));
//随机改变移动视图宽高
NSInteger width = 50 + arc4random() % 200;
NSInteger height = 20 + arc4random() %80;
NSInteger ox = point.x - width/2;
NSInteger oy = point.y - height/2;
//对越界判断处理
if (ox < 10) {
ox = 10;
} else if (ox > self.view.bounds.size.width - 10 - width) {
ox = self.view.bounds.size.width - 10 - width;
}
if (oy < 30) {
oy = 30;
}
self.moveView.frame = CGRectMake(ox, oy, width, height);
//添加浮动窗
CustomMenuItemView *view = [[CustomMenuItemView alloc] init];
[view showInView:self.view frame:self.moveView.frame];
} @end

自定义浮动框视图类如下 CustomMenuItemView.h

#import <UIKit/UIKit.h>

@interface CustomMenuItemView : UIView

- (void)showInView:(UIView *)view frame:(CGRect)rect;

@end

CustomMenuItemView.m

#import "CustomMenuItemView.h"

@implementation CustomMenuItemView

- (void)showInView:(UIView *)view frame:(CGRect)rect
{
self.frame = view.bounds;
[view addSubview:self];
NSInteger jiantouSize = 10;
NSInteger width = 250;
NSInteger height = 120;
UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
bgview.backgroundColor = [UIColor darkGrayColor];
bgview.layer.masksToBounds = YES;
bgview.layer.cornerRadius = 6;
[self addSubview:bgview];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, jiantouSize, jiantouSize)];
imageView.image = [UIImage imageNamed:@"down"];
[self addSubview:imageView]; NSInteger ox = rect.origin.x + rect.size.width/2 - width/2;
NSInteger oy = rect.origin.y - height/2 - jiantouSize - height/2;
NSInteger jiantouCenterY = rect.origin.y - jiantouSize/2; if (oy <= 50) {
imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI);
oy = rect.origin.y + rect.size.height + jiantouSize;
jiantouCenterY = rect.origin.y + rect.size.height + jiantouSize/2;
} if (oy > view.bounds.size.height - jiantouSize - height - 10) {
oy = view.bounds.size.height - height - 10;
jiantouCenterY = view.bounds.size.height - jiantouSize/2 - height - 10;
} if (ox <= 10) {
ox = 10;
} else if (ox >= view.bounds.size.width - 10 - width) {
ox = view.bounds.size.width - 10 - width;
}
bgview.frame = CGRectMake(ox, oy, width, height);
imageView.center = CGPointMake(rect.origin.x + rect.size.width/2, jiantouCenterY);
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.hidden = YES;
self.removeFromSuperview;
} @end

iOS开发之实现自定义浮动操作框效果的更多相关文章

  1. iOS开发多线程篇—自定义NSOperation

    iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...

  2. 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框     今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...

  3. iOS开发之各种动画各种页面切面效果

    因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一些干货,切勿错过哦.今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发中常用的动画.说道动画其 ...

  4. 【转】iOS开发之各种动画各种页面切面效果

    原文: http://www.cnblogs.com/ludashi/p/4160208.html?utm_source=tuicool 因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一 ...

  5. Tooltip浮动提示框效果(掌握里面的小知识)

    使用原生JavaScript设计和实现Tooltip浮动提示框特效,了解代码简化.事件绑定.事件冒泡等技巧和知识. 特效四个关键点:显示:鼠标移到ToolTip超链接上时,ToolTip提示框可以显示 ...

  6. iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮

    iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ...

  7. iOS开发15:自定义UITableViewCell

    上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格.不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图.添加子视 ...

  8. iOS开发UI篇—自定义layer

    一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图. 绘制图形的步骤: ...

  9. 【ios开发】使用自定义的TableViewCell

    当系统自带的cell无法满足我们的要求的时候,我们就可以自定义自己的cell. 先看看效果,这个效果有点重复造轮子的感觉,因为UITableView已经实现了这种布局. 打造自己的cell只需简单的3 ...

  10. iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View

    一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...

随机推荐

  1. Codeforces Round #827 (Div. 4) 复盘+题解

    原比赛链接 复盘: ABC签到,手速太慢了. D捣鼓了好久才想起来从更小的值域出发去做. E简单二分答案. 然后就time out了.D题搞错方向浪费太久时间了. F思维题,考虑到初值.字符集,然后是 ...

  2. python菜鸟学习: 11. 装饰器的基础用法

    # -*- coding: utf-8 -*-# decorator# 定义:本质是函数,就是为其他函数添加附件功能# 原则:# 1.不能修改被装饰的函数的源代码# 2.不能修改被装饰的函数的调用方式 ...

  3. python 链表推导式x for xx in yy

    一.(x for xx in yy )当x为固定参数 如: n = 10 # 生成n个0 matrix = [0 for i in range(n)]# 这里面0代表返回值,后面的for循环就是返回的 ...

  4. kali对安卓的渗透(内网穿透)

    前言:随着移动端的增加,安卓占比巨大,人们对手机的安全防范意识薄弱,手机为了人们更加的方便,缺乏防护软件,甚至好多木马不需要做免杀. 现在我来做安卓的渗透,不需要在内网也能进行,实现了内网穿透.(有公 ...

  5. ASP.NET Core Filter如何支持依赖注入

    通过Filter来支持:分别有IResourceFilter AuthorizeFilter ActionFilter ExceptionFilter ResultFilter,Filter也被称为拦 ...

  6. JUC续

    设计模式,保护性暂停.解耦 生产者消费者模式 park.unpark 线程状态转换 锁超时 锁超时可以解决哲学家就餐问题 公平锁 条件变量 线程控制顺序

  7. GIS空间分析和建模复习重点4

    27.三维地形分析的方法 常用的有 ·坡度分析 ·坡向分布 ·填挖方分析 ·山体阴影分析 ·通视分析 28.空间分析实际应用 (1)城市应急避难场所是城市防灾减灾规划的重要组成部分,应急避难场所的建设 ...

  8. IaaS--云虚拟机(一)(何恺铎《深入浅出云计算》笔记整理)

    [概念讲解] 云虚拟机的体系结构,就是全面解耦的计算存储分离的设计思想. 传统的虚拟化,往往是对单一物理机器资源的纵向切割,计算.存储.网络等各方面的能力都是一台物理机的子集.因此,从可伸缩性的角度来 ...

  9. raster2pgsql 执行命令

    raster2pgsql -s 4326 -I -C -M /home/radar_202210251000.tif public.radar_data_xx | psql -h 120.46.210 ...

  10. 3516A调试

    最近在调Hi3516A的板,硬件不知道为什么如此设计,用一片16bit4G的ddr,16Mspi flash,按理如果是A应该是2片16bit的ddr,组成32位总线,现在怕是只能当D来用了,编译成功 ...