iOS开发之实现自定义浮动操作框效果
今天有个需求是如上图实现类似微信的自定义浮动操作框效果
我自己就写了个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开发之实现自定义浮动操作框效果的更多相关文章
- iOS开发多线程篇—自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框 今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...
- iOS开发之各种动画各种页面切面效果
因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一些干货,切勿错过哦.今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发中常用的动画.说道动画其 ...
- 【转】iOS开发之各种动画各种页面切面效果
原文: http://www.cnblogs.com/ludashi/p/4160208.html?utm_source=tuicool 因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一 ...
- Tooltip浮动提示框效果(掌握里面的小知识)
使用原生JavaScript设计和实现Tooltip浮动提示框特效,了解代码简化.事件绑定.事件冒泡等技巧和知识. 特效四个关键点:显示:鼠标移到ToolTip超链接上时,ToolTip提示框可以显示 ...
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ...
- iOS开发15:自定义UITableViewCell
上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格.不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图.添加子视 ...
- iOS开发UI篇—自定义layer
一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图. 绘制图形的步骤: ...
- 【ios开发】使用自定义的TableViewCell
当系统自带的cell无法满足我们的要求的时候,我们就可以自定义自己的cell. 先看看效果,这个效果有点重复造轮子的感觉,因为UITableView已经实现了这种布局. 打造自己的cell只需简单的3 ...
- iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View
一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...
随机推荐
- centos7安装php8
原文: http://www.manongjc.com/detail/25-qpyxndyogppmfdf.html 前言 centos7默认源的php版本只有5.4,版本太老,而mediawiki需 ...
- [737] Interlude OpCodez
[737] Interlude Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 RequestEnterWorld ...
- PVE使用vlan
- sxt_(001_002)_web简介
一.web 网络.网页二.web应用 运行在网络上的应用程序.三.网络应用的分类 3.1 c/s:client/server 如:qq.yy.lol 优点: 个性化更容易实现 更安全 占用网络资源少. ...
- Docker CLI docker attach 常用命令
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化.Docker是内核 ...
- 谷歌是如何改进 GKE、Cloud Run 的 gVisor 文件系统性能的?
灵活的应用程序架构.CI/CD 管道和容器工作负载通常运行不受信任的代码,因此应该与敏感的基础设施隔离.一种常见的解决方案是部署纵深防御产品(如使用gVisor的GKE Sandbox)以通过额外的保 ...
- Xcode 12.x下载了iOS10.x模拟器无法创建对应Device问题修复
转自: https://hiraku.tw/2021/04/6428/ 感谢原作者,如有侵权请评论联系删除文章 在升級到 Xcode 12 之後,有些人發現雖然 Xcode 允許安裝低版本的模擬器,但 ...
- pytest 之conftest.py是什么
conftest.py是pytest框架的固定写法:可以把hook和fixture写在这个文件里,就会自动去调用:conftest.py相当于可以编写自己的插件: 也可以理解为pytest特有的本地测 ...
- CAD梦想看图手机版20211101更新(手机版CAD软件)
CAD梦想看图手机版20211101更新(手机版CAD软件)1. 新界面风络2. 增加图块库功能3. 适配Android 114. 修改图块中的,多线义线的线型可能显示不对问题5. 修改图块中套用图块 ...
- clickhouse-数据副本踩坑
数据副本--失败,看日志 vim /var/log/clickhouse-server/clickhouse-server.err.log select * from system.replicati ...