背景:

我不知道大伙们有没有遇到要做一个类似于小圆点的控件的需求,我在CocoaChina论坛上看别人说,这样的东西是会被拒审的,因为和系统的小圆点效果一样了。
管他呢,反正公司既然要求我做,那我做就是咯。


开发思路:

目前,我学习了两种方法。

一种较为简单,自定义一个UIImageView来实现。把UIImageView的交互打开,并且实现一个拖拽的手势,拖拽手势的方法里面实现对应效果即可。

另一种,稍微复杂一点点,直接用一个UIButton来实现就好。和上面一样,也是实现一个拖拽的手势,只是拖拽手势里面算法复杂点。

两种都有各自不同的特点,看大家喜欢哪种,我比较喜欢第二种,因为第二种逼格更高点。

除了上述两种方法,还有一种方法:就是直接把控件添加到window里面去。你会发现,上述两种方法,不过都是加在根控制器的view里面去的。试想,如果当前是UIViewController还好说,如果是NavigationController呢?如果是TabBarController呢?如果是NavigationController或者是TabBarController的话,那么这个控件就会被遮挡的。怎么办?加在window上就好啦。

不过,如果项目中没有太大的硬性需求,最好不要加载window上,怪怪的。


方法一:

//
//  ViewController.m
//  SuspendDemo
//
//  Created by HZhenF on 2017/9/12.
//  Copyright © 2017年 HZhenF. All rights reserved.
//#import "ViewController.h"
#import "SuspendImgV.h"
#import "OneViewController.h"#define kScreenWidth    [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight   [[UIScreen mainScreen] bounds].size.height@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];SuspendImgV * v = [[SuspendImgV alloc] initWithFrame:CGRectMake(100, 100, kItemWidth, kItemHeight) topMargin:66 btomMargin:44];v.backgroundColor = [UIColor redColor];v.image = [UIImage imageNamed:@"lufei.jpg"];[self.view addSubview:v];UIView  * topV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 66)];topV.backgroundColor = [UIColor yellowColor];UIView  * centerV = [[UIView alloc] initWithFrame:CGRectMake(0, 66, kScreenWidth, kScreenHeight - 110)];centerV.backgroundColor = [UIColor lightGrayColor];UIView  * btomV = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 44, kScreenWidth, 44)];btomV.backgroundColor = [UIColor orangeColor];[self.view addSubview:topV];[self.view addSubview:centerV];[self.view addSubview:btomV];UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];[btn setTitle:@"点我" forState:UIControlStateNormal];[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];//将悬浮视图扔到最顶层[self.view bringSubviewToFront:v];
}-(void)btnAction
{OneViewController *oneVC = [[OneViewController alloc] init];UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];[self presentViewController:nav animated:YES completion:nil];
}@end
//
//  SuspendImgV.h
//  SuspendDemo
//
//  Created by HZhenF on 2017/9/12.
//  Copyright © 2017年 HZhenF. All rights reserved.
//#import <UIKit/UIKit.h>#define kItemWidth 60
#define kItemHeight 60@interface SuspendImgV : UIImageView- (instancetype)initWithFrame:(CGRect)frametopMargin:(CGFloat)topbtomMargin:(CGFloat)btom;@end
//
//  SuspendImgV.m
//  SuspendDemo
//
//  Created by HZhenF on 2017/9/12.
//  Copyright © 2017年 HZhenF. All rights reserved.
//#import "SuspendImgV.h"typedef NS_ENUM(NSInteger, Position) {PositionLeft = 0,PositionRight = 1,
};#define kHeight self.superview.frame.size.height
#define kWidth  self.superview.frame.size.width@interface SuspendImgV ()@property (nonatomic, assign) Position  position;
@property (nonatomic, assign) CGFloat   topMargin;
@property (nonatomic, assign) CGFloat   btomMargin;@end@implementation SuspendImgV- (instancetype)initWithFrame:(CGRect)frame topMargin:(CGFloat)top btomMargin:(CGFloat)btom;
{if (self = [super init]) {self.frame = frame;self.userInteractionEnabled = YES;_topMargin = top;_btomMargin = btom;[self addGesture];}return self;
}- (void)addGesture
{UIPanGestureRecognizer  * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];[self addGestureRecognizer:pan];
}- (void)configFrameWithAnimation:(BOOL)aniamtion
{CGPoint p = self.frame.origin;CGRect  aimR = CGRectZero;if (self.center.x < kWidth/2) {aimR = CGRectMake(2, p.y, kItemWidth, kItemHeight);_position = PositionLeft;}else{aimR = CGRectMake(kWidth - kItemWidth - 2, p.y, kItemWidth, kItemHeight);_position = PositionRight;}if (aniamtion) {[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{self.frame = aimR;} completion:nil];}else{self.frame = aimR;}
}- (void)panAction:(UIPanGestureRecognizer *)pan
{if (pan.state == UIGestureRecognizerStateEnded|| pan.state == UIGestureRecognizerStateCancelled){//结束[self configFrameWithAnimation:YES];}else {//移动//当然也可以相对于key window,只是self.superView的宽高和window不一致时,需要注意计算时的偏移量CGPoint p = [pan locationInView:self.superview];p.y = MAX(p.y, _topMargin + kItemHeight/2);p.y = MIN(kHeight - _btomMargin - kItemHeight/2, p.y);[UIView animateWithDuration:0.15 animations:^{self.center = p;}];}
}@end

方法二:

//
//  ViewController.m
//  SmallPoint
//
//  Created by HZhenF on 2017/9/18.
//  Copyright © 2017年 GZHYTechnology. All rights reserved.
//#import "ViewController.h"// 屏幕高度
#define susScreenH [UIScreen mainScreen].bounds.size.height
// 屏幕宽度
#define susScreenW [UIScreen mainScreen].bounds.size.width
//悬浮按钮宽高
#define kSuspendBtnWidth 100 * susScreenW / 375@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];CGFloat X = 0;CGFloat Y = 100;CGFloat W = 100;CGFloat H = 100;UIButton *btnOne = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, W, H)];[btnOne setImage:[UIImage imageNamed:@"girl"] forState:UIControlStateNormal];[self.view addSubview:btnOne];UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];[btnOne addGestureRecognizer:panGesture];}/**拖拽事件@param recognizer 当前拖拽的手势*/
-(void)panAction:(UIPanGestureRecognizer *)recognizer
{//移动状态UIGestureRecognizerState recState =  recognizer.state;switch (recState) {case UIGestureRecognizerStateBegan:break;case UIGestureRecognizerStateChanged:{UIViewController *viewVC = self;CGPoint translation = [recognizer translationInView:viewVC.navigationController.view];recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);}break;case UIGestureRecognizerStateEnded:{CGPoint stopPoint = CGPointMake(0, susScreenH / 2.0);if (recognizer.view.center.x < susScreenW / 2.0){if (recognizer.view.center.y <= susScreenH/2.0) {//左上if (recognizer.view.center.x  >= recognizer.view.center.y) {stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);}else{stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);}}else{//左下if (recognizer.view.center.x  >= susScreenH - recognizer.view.center.y) {stopPoint = CGPointMake(recognizer.view.center.x, susScreenH - kSuspendBtnWidth/2.0);}else{stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);}}}else{if (recognizer.view.center.y <= susScreenH/2.0) {//右上if (susScreenW - recognizer.view.center.x  >= recognizer.view.center.y) {stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);}else{stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0, recognizer.view.center.y);}}else{//右下if (susScreenW - recognizer.view.center.x  >= susScreenH - recognizer.view.center.y) {stopPoint = CGPointMake(recognizer.view.center.x, susScreenH - kSuspendBtnWidth/2.0);}else{stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0,recognizer.view.center.y);}}}if (stopPoint.x - kSuspendBtnWidth/2.0 <= 0) {stopPoint = CGPointMake(kSuspendBtnWidth/2.0, stopPoint.y);}if (stopPoint.x + kSuspendBtnWidth/2.0 >= susScreenW) {stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0, stopPoint.y);}if (stopPoint.y - kSuspendBtnWidth/2.0 <= 0) {stopPoint = CGPointMake(stopPoint.x, kSuspendBtnWidth/2.0);}if (stopPoint.y + kSuspendBtnWidth/2.0 >= susScreenH) {stopPoint = CGPointMake(stopPoint.x, susScreenH - kSuspendBtnWidth/2.0);}[UIView animateWithDuration:0.5 animations:^{recognizer.view.center = stopPoint;}];}break;default:break;}[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}@end

iOS-仿小圆点效果相关推荐

  1. css伪类元素实现小圆点效果

    前言: 使用伪类元素  ::before,::after  来实现 小圆点效果. 效果图: 实现方式: 1.父级元素 postion:relative; //定位属性,可为absolute //必须 ...

  2. Android进度条/等待加载——旋转小圆点效果

    进度条/等待加载--旋转小圆点效果 1 效果图 2.思想 12个小圆点叠放(i=0,1,-11) 动画一:依次从0度旋转到30i度 动画二:依次从30i度旋转到360度 因为不牵扯用户交互,所以用最基 ...

  3. iOS仿QQ分组效果

    本篇主要讲解仿QQ分组效果的实现,通过本遍的学习,估计都可以自己去实现了(老司机可以),在这里只说仿QQ分组的效果,代码简单,逻辑清晰.其他的功能的可以自行添加,好了,进入主题吧. 效果图 下面的是其 ...

  4. html5点赞仿抖音,iOS仿抖音—加载点赞动画效果

    iOS仿抖音短视频 前言 前段时间比较忙,最近终于有时间就继续对仿抖音的demo进行更新,本次更新的主要是抖音上的几种动画,下面先来看下效果图: 抖音 说明 经过观察发现抖音主要要以下几种动画效果: ...

  5. iOS: 教你给UI控件添加Badge(消息提醒小圆点)

    PPBadgeView 1.前言 最近项目的一个需求是在UIView, UITabBarItem, UIBarButtonItem 这三种类型的控件上添加消息提醒小圆点(Badge),一开始找了一个星 ...

  6. android 圆点指示器,ViewPager加上小圆点指示器效果

    分析 环境 环境:Android Studio 4.0 语言:Java 特点:简单,易懂,效果爆炸 效果 效果2.gif ViewPager类的来历 ViewPager是android扩展包v4包中的 ...

  7. js轮播图片小圆点变化_用jQuery实现圆点图片轮播效果

    在页面的指定位置实现的图片自动的左右轮流切换展示效果,当点击图片左下的标签(或中间的小圆点)切换到对应的图片.接下来通过本文给大家分享用jQuery实现圆点图片轮播效果实例代码,需要的朋友参考下 图片 ...

  8. ViewPager 添加广告页面小圆点指示器效果

    介绍 我们的应用几乎都有启动引导页介绍,通常是3~4张引导图,然后进入我们的主界面.大家可以发现,我们大部分引导界面都会有一种指示器(也就是小圆点,这里比较常见).除了引导页以外呢,我们常见的广告页也 ...

  9. 仿网易新闻顶部菜单html,iOS仿网易新闻滚动导航条效果

    本文实例为大家分享了iOS滚动导航条效果展示的具体代码,供大家参考,具体内容如下 实现效果 效果:选择不同的栏目,下面出现不同的视图,栏目条可以滚动:下面的视图也可以滚动,滚动时上面对应的栏目要选中颜 ...

最新文章

  1. Android --- 好用的图片查看器
  2. php安卓交互安全,php结合安卓客户端实现查询交互实例
  3. php和web服务器,php与web服务器关系
  4. ESP8266固件烧录软件flash_download_tools的安装过程
  5. 自己集成的android容联云IMdemo效果展示
  6. 首度亮相服贸会,亚马逊云科技如何演绎“大象起舞”?
  7. Microsoft Office下载链接
  8. luogu P2123 皇后游戏
  9. 【数据库系统综合实验】教学管理信息系统—学生选课及课程安排数据库综合实验
  10. 11.2.5 云计算、大数据时代
  11. python求全排列
  12. 基于Echarts实现可视化数据大屏实时监测地图
  13. 对象、对象的属性、对象字面量、枚举对象中的属性、可变类型、变量和对象——JS对象
  14. Server 2016 + Win10 搭建CA证书登录环境
  15. latex伪代码添加注释_【其他】如何用写代码的方式进行文字编辑 Markdown 的简明教程...
  16. 【C#】求最大公约数
  17. anaconda安装pythonocc
  18. 大学要考的证书英语计算机,大学里必考的证书盘点 英语四六级、计算机证书上榜...
  19. JWT之token机制与双token详解
  20. 火车订票系统属于哪方面的计算机应用,客机、火车订票系统属于()。 - 问答库...

热门文章

  1. VisualFreeBasic的pcre/pcre2正则表达式修复
  2. 简报 | 智利推出全新加密货币和金融科技监管法案
  3. 算法学习——求有重复元素的全排列(递归)
  4. rtx2050相当于gtx什么显卡 rtx2050显卡什么水平
  5. 电工实习proteus的基本使用
  6. 【总结】线段树 进阶
  7. 什么是ADS-B信号?
  8. 调频扩频frequency-hopping-spread-spectrum
  9. 弱引用是什么,和其他引用有啥区别?
  10. jmeter调用接口,往数据库录入数据乱码问题