亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

iOS實現懸浮按鈕

瀏覽:4日期:2022-09-17 08:04:07

前言

開發中會遇到有懸浮按鈕功能。

效果

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;@endNS_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
相關文章:
主站蜘蛛池模板: 91在线丝袜 | 12306播播影院午夜 | 综合色婷婷 | 国产精品成人va在线观看 | 日韩经典欧美精品一区 | 50岁老女人毛片一级亚洲 | 亚洲欧美日韩高清在线看 | 久久精品视频在线观看榴莲视频 | 亚洲福利国产精品17p | 国产片毛片 | 国产伦一区二区三区四区久久 | 综合久久 五十路 二区 | 中文国产成人精品久久水 | 久久成人国产精品青青 | 一级片网 | 欧美伦理片在线 | 成人午夜又粗又硬有大 | 国产不卡视频在线 | 福利在线一区二区 | 1024你懂的国产日韩欧美 | www.国产精品| 欧美一级毛片免费播放aa | 一级特黄aa大片免费播放视频 | 东北普通话清晰对白 | 国内精品不卡一区二区三区 | 五月婷婷六月丁香 | 日本老年人精品久久中文字幕 | 久久毛片免费看一区二区三区 | 精品国产一区二区三区久久影院 | 国产又黄又爽又色视频观看免费 | 成人在线视频网站 | 久热在线播放中文字幕 | 久久性视频 | 东莞a级毛片 | 国产成人精品亚洲2020 | 午夜性刺激免费视频 | 亚洲欧美中日韩中文字幕 | 三上悠亚vr | 五月婷婷综合色 | 国产视频三区 | 久久精品a亚洲国产v高清不卡 |