iOS實現(xiàn)懸浮按鈕
更新時間:2021年01月25日 08:01:09 作者:林大帥6688
這篇文章主要為大家詳細介紹了iOS實現(xiàn)懸浮按鈕,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
前言
開發(fā)中會遇到有懸浮按鈕功能。
效果
上代碼
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) {//向右側(cè)移動 //偏移動畫 [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 {//向左側(cè)移動 [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將一直處于按下狀態(tài) [super touchesEnded: touches withEvent: event]; } @end
使用
ViewController.m
#import "ViewController.h" #import "SuspensionButton.h"http://懸浮按鈕 @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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
iOS中在APP內(nèi)加入AppStore評分功能的實現(xiàn)方法
這篇文章主要介紹了iOS中在APP內(nèi)加入AppStore評分功能的實現(xiàn)方法,文中筆者給大家整理了三種方式,大家可以根據(jù)自己的需求選擇,需要的朋友可以參考下2017-11-11iOS開發(fā)中以application/json上傳文件實例詳解
在和sever后臺交互的過程中、有時候、他們需要我們iOS開發(fā)者以“application/json”形式上傳,具體實例代碼大家參考下本文2017-07-07Swift實現(xiàn)iOS應(yīng)用中短信驗證碼倒計時功能的實例分享
這篇文章主要介紹了Swift實現(xiàn)iOS應(yīng)用中短信驗證碼倒計時功能的實例分享,開啟和關(guān)閉倒計時功能的步驟實現(xiàn)比較關(guān)鍵,需要的朋友可以參考下2016-04-04詳解IOS開發(fā)之實現(xiàn)App消息推送(最新)
這篇文章主要介紹了詳解IOS開發(fā)之實現(xiàn)App消息推送(最新),具有一定的參考價值,有興趣的可以了解一下。2016-12-12