iOS实现悬浮按钮

 更新时间:2021年01月25日 08:01:09   作者:林大帅6688  
这篇文章主要为大家详细介绍了iOS实现悬浮按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

开发中会遇到有悬浮按钮功能。

效果

上代码

SuspensionButton.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SuspensionButton : UIButton

@property(nonatomic, assign)BOOL MoveEnable;
@property(nonatomic, assign)BOOL MoveEnabled;
@property(nonatomic, assign)CGPoint beginpoint;

@end

NS_ASSUME_NONNULL_END

SuspensionButton.m

#import "SuspensionButton.h"

@implementation SuspensionButton

- (instancetype)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
 self.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 51, 50, 51, 51);
 [self setBackgroundImage:[UIImage imageNamed:@"icon_move"] forState:UIControlStateNormal];
 [self setTitle:@"Button" forState:UIControlStateNormal];
 self.titleLabel.font = [UIFont systemFontOfSize:10];
 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 _MoveEnable = YES;
 }
 return self;
}

//开始触摸的方法
//触摸-清扫
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 _MoveEnabled = NO;
 [super touchesBegan:touches withEvent:event];
 if (!_MoveEnable) {
 return;
 }
 UITouch *touch = [touches anyObject];
 _beginpoint = [touch locationInView:self];
}

//触摸移动的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 _MoveEnabled = YES;//单击事件可用
 if (!_MoveEnable) {
 return;
 }
 UITouch *touch = [touches anyObject];
 CGPoint currentPosition = [touch locationInView:self];
 //偏移量
 float offsetX = currentPosition.x - _beginpoint.x;
 float offsetY = currentPosition.y - _beginpoint.y;
 //移动后的中心坐标
 self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
 
 //x轴左右极限坐标
 if (self.center.x > (self.superview.frame.size.width - self.frame.size.width / 2)) {
 CGFloat x = self.superview.frame.size.width - self.frame.size.width / 2;
 self.center = CGPointMake(x, self.center.y + offsetY);
 } else if (self.center.x < self.frame.size.width / 2) {
 CGFloat x = self.frame.size.width / 2;
 self.center = CGPointMake(x, self.center.y + offsetY);
 }
 
 //y轴上下极限坐标
 if (self.center.y > (self.superview.frame.size.height - self.frame.size.height)) {
 CGFloat x = self.center.x;
 CGFloat y = self.superview.frame.size.height - self.frame.size.height * 1.5;
 self.center = CGPointMake(x, y);
 } else if (self.center.y <= self.frame.size.height) {
 CGFloat x = self.center.x;
 CGFloat y = self.frame.size.height * 1.2;
 self.center = CGPointMake(x, y);
 }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 if (!_MoveEnable) {
 return;
 }
 
 if (self.center.x >= self.superview.frame.size.width / 2) {//向右侧移动
 //偏移动画
 [UIView beginAnimations:@"move" context:nil];
 [UIView setAnimationDuration:1];
 [UIView setAnimationDelegate:self];
 self.frame = CGRectMake(self.superview.frame.size.width - 51, self.center.y - 25.5, 51, 51);
 //提交UIView动画
 [UIView commitAnimations];
 } else {//向左侧移动
 
 [UIView beginAnimations:@"move" context:nil];
 [UIView setAnimationDuration:1];
 [UIView setAnimationDelegate:self];
 self.frame=CGRectMake(0.f,self.center.y - 25.5, 51, 51);
 //提交UIView动画
 [UIView commitAnimations];
 }
 
 //不加此句话,UIButton将一直处于按下状态
 [super touchesEnded: touches withEvent: event];
 
}
@end

使用

ViewController.m

#import "ViewController.h"
#import "SuspensionButton.h"//悬浮按钮

@interface ViewController ()

@property(nonatomic, strong) SuspensionButton *suspensionButton;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 [self.view addSubview:self.suspensionButton];
}

- (SuspensionButton *)suspensionButton {
 if(_suspensionButton == nil) {
 _suspensionButton = [SuspensionButton buttonWithType:UIButtonTypeCustom];
 _suspensionButton.backgroundColor = [UIColor grayColor];
 _suspensionButton.layer.masksToBounds = YES;
 _suspensionButton.layer.cornerRadius = self.suspensionButton.frame.size.width/2;
 [_suspensionButton addTarget:self action:@selector(suspensionButtonClick) forControlEvents:UIControlEventTouchUpInside];
 }
 return _suspensionButton;
}

- (void)suspensionButtonClick {
 
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • IOS UITableView颜色设置的实例详解

    IOS UITableView颜色设置的实例详解

    这篇文章主要介绍了IOS UITableView颜色设置的实例详解的相关资料,这里提供了几种方法帮助大家掌握这部分内容,需要的朋友可以参考下
    2017-08-08
  • IOS9.0 LaunchScreen.StroyBoard自定义启动图片详解

    IOS9.0 LaunchScreen.StroyBoard自定义启动图片详解

    这篇文章主要介绍了IOS9.0 LaunchScreen.StroyBoard自定义启动图片详解的相关资料,需要的朋友可以参考下
    2017-02-02
  • iOS开发KVO实现细节解密

    iOS开发KVO实现细节解密

    这篇文章主要为大家介绍了iOS开发KVO实现细节解密,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • iOS 11 BUG的发现、定位和解决

    iOS 11 BUG的发现、定位和解决

    这篇文章主要为大家详细介绍了iOS 11 BUG的发现、定位和解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • iOS 泛型中nullable、null resettable、null kindof 用法详解

    iOS 泛型中nullable、null resettable、null kindof 用法详解

    这篇文章主要介绍了iOS 泛型中nullable、null resettable、null kindof 用法详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • iOS实现折叠单元格

    iOS实现折叠单元格

    这篇文章主要为大家详细介绍了iOS实现折叠单元格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Nib文件是什么?Nib文件打开方法

    Nib文件是什么?Nib文件打开方法

    这篇文章主要介绍了Nib文件是什么?Nib文件打开方法,nib文件是Cocoa App的界面资源,这种文件直接用xcode是不能被打开的,要修改后才能打开,本文就讲解了修改方法,需要的朋友可以参考下
    2015-04-04
  • iOS TableView头视图根据偏移量下拉缩放效果

    iOS TableView头视图根据偏移量下拉缩放效果

    这篇文章主要为大家详细介绍了iOS TableView头视图根据偏移量下拉缩放效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • 总结IOS关闭键盘/退出键盘的五种方式

    总结IOS关闭键盘/退出键盘的五种方式

    IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现例如点击键盘以外的空白区域来将键盘关闭的功能,以下是我总结出的几种关闭键盘的方法。
    2016-08-08
  • iOS开发输入自动填充UITextField背景色

    iOS开发输入自动填充UITextField背景色

    如何在iOS中实现输入时自动填充背景色的效果,首先,我们设置UITextField的背景色为初始颜色,然后,通过设置UITextField的代理,并监听UITextField的输入事件,我们在用户开始输入时将其背景色改变为高亮颜色,在用户结束输入时恢复为初始颜色
    2023-10-10

最新评论