时间:2021-05-20
前言
开发中会遇到有悬浮按钮功能。
效果
上代码
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;@endNS_ASSUME_NONNULL_ENDSuspensionButton.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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了android应用内部悬浮可拖动按钮简单实现代码,分享给大家,具体如下:可以悬浮在activity上面,在加载fragment时悬浮按钮不会消失实现方
今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子。首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager
ReactNative悬浮按钮组件:react-native-action-button,纯JS组件,支持安卓和IOS双平台,支持设置子按钮,支持自定义位置和样
华为p40手机关闭悬浮按钮的方法,可以通过点击系统导航方式把悬浮导航开关关闭即可。具体可以通过以下步骤操作来实现:华为p40关闭手机悬浮按钮教程在华为p40手机
悬浮操作按钮(FloatingActionButton,FAB),或者说悬浮按钮,是Android应用中最常见的一个控件。悬浮按钮通常是圆形,底部的Materi