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 ...
随机推荐
- vue中组件传值的几种方式
一.父组件给子组件传值方式(步骤) 1.VC1(子组件)定义props[a,b,c] 注意:props中的每个值都可以加各种修饰,如数据类型,是否可为空,默认值... 2.VC2(父组件)引用子组件 ...
- 自定义Lock实现
1 package com.wyt.lock.test; 2 3 import java.util.concurrent.LinkedBlockingQueue; 4 import java.util ...
- 使用fopen,fscanf等函数报安全性问题的错误,unsafe...
方法一:项目-属性-C/C++-预处理器定义,添加_CRT_SECURE_NO_WARNINGS. 方法二:使用fopen_s,fscanf_s等安全函数.
- 面试:关于Zookeeper注册节点的上线和掉线
Zookeeper中有一个重要的部件Monitor(监控中心),它是Dubbo中服务治理体系中的重要一环. 监控中心在启动的时候,会通过Zookeeper的/dubbo/com.foo.BarServ ...
- JavaScript基础知识整理(对象的属性)
对象的定义 无序属性的集合,属性可以包括基本值,对象或函数. JavaScript中有两类属性,数据属性和访问器属性. 特征值 JavaScript使用特征值来描述属性的行为,因为是为实现JavaSc ...
- SpringMVC学习day03
第1个springmvc程序 这是根据狂神说视频和资料学习的,用于加强自己的记忆 入门学习 1. 步骤: 1.新建一个Moudle , springmvc-02-hello , 添加web的支持! 2 ...
- 2023-03-02 TypeError: null is not an object (evaluating 'ImageCropPicker.openPicker')
问题描述:rn项目使用到了一个插件react-native-image-crop-picker,运行后报错. 原因:安装该插件的时候没有link到android包里. 解决方案: react-nati ...
- -bash: pip: command not found
使用pip安装软件包时报错命令不存在 [root@test ~]# pip -V -bash: pip: command not found 机器上没有安装pip,需要手动进行安装 centos系统: ...
- 剑指 Offer 链表
06. 从尾到头打印链表 class Solution { public: //两个指针一起走 一次翻转一个方向 最后head.next =null ListNode* reverse1(ListNo ...
- 2021年RT-Thread开发者大会
Time:2021-12-18,地点:大中华6楼喜来登酒店 主办方: RT-Thread:寓意实时线程,瑞赛德 世界有成千上万个 RTOS(Real-time operating system,实时操 ...