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

我自己就写了个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. FlyMcu烧录提示写入出错在初始化:连接,耗时3437延秒

    取消勾选RamIsp的选项就可以了

  2. C# 实时显示时间

    c#实时显示时间 - vv彭 - 博客园 (cnblogs.com)

  3. JavaWeb 验证码

    package com.gen; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.s ...

  4. MATH1851 Trigonometric Formula Notes

    大学里老师都默认我们学过 正割 \(\sec\),余割 \(\csc\) 与余切 \(\cot\) 再加上高中的一些公式都有点遗忘,开个贴做个笔记 常规的 \(\sin x, \cos x, \tan ...

  5. ENGG1310 Electricity and electronics P1.1 microelectronics & optoelectronics

    课程内容笔记,自用,不涉及任何 assignment,exam 答案 Notes for self use, not included any assignments or exams 听说这节课内容 ...

  6. elementUI中table组件前端自己实现序号排序

    <el-table-column type="index" label="序号" width="50" align="cen ...

  7. 刘蓉年谱.PDF

    书本详情 刘蓉年谱.PDF 所有责任者: 陆宝千著 所有题名: 并列正题名:A chronological biography of Liu JungChronological biography o ...

  8. Linux 第九章( 网卡配置,双网卡绑定,密钥,管理远程会话 )

    /etc/hosts.allow  允许    //默认是先匹配允许在匹配拒绝 /etc/hosts.deny   拒绝 service iptables save   //保存iptables配置 ...

  9. Neo4j权威指南学习笔记第一章

    1.创建图数据库 1.1.创建电影节点 CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to ...

  10. M1 IntelliJ IDEA2022安装报错解决方法

    下载地址:http://pan.jizhouyun.com/s/I3QJVzk3et 报错一:系统权限问题 报错示例: 1.XX已损坏,打不开:您应该将它移到废纸娄/已损坏 2.打不开:您应该推出磁盘 ...