亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

iOS實現(xiàn)懸浮按鈕

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

前言

開發(fā)中會遇到有懸浮按鈕功能。

效果

iOS實現(xiàn)懸浮按鈕

上代碼

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) {//向右側(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'//懸浮按鈕@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)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: IOS
相關文章:
主站蜘蛛池模板: 久久悠 | 一a一片一级一片啪啪 | 视频一区色眯眯视频在线 | 亚洲综合色视频在线观看 | 亚洲精品资源网在线观看 | 自偷自偷自亚洲永久 | 男人v天堂 | 精品欧美一区二区三区免费观看 | 国产精品亚洲精品影院 | 欧洲欧美成人免费大片 | 成人亚洲国产 | 一级视频在线免费观看 | 日本卡一卡2卡3卡4精品卡无人区 | 男女免费爽爽爽在线视频 | 中文字幕久久亚洲一区 | 国产一区二区三区免费在线视频 | 亚洲国产精品久久精品成人 | 欧洲一级视频 | 久久久久久91精品色婷婷 | 欧美videos另类齐全 | 狼伊千合综网中文 | 天天黄色片 | 欧美成人精品免费播放 | 另类专区另类专区亚洲 | 欧美第一页草草影院浮力 | 国产午夜精品久久久久小说 | 欧美一级免费观看 | 九九热爱视频精品视频高清 | 欧美日本综合一区二区三区 | 精品三级国产一区二区三区四区 | 欧美一级高清片免费一级 | 亚洲久久在线观看 | 欧美精品专区55页 | 亚洲精品综合久久中文字幕 | 99国产在线观看 | 国产资源在线免费观看 | 国产成人aa在线观看视频 | 亚洲国产综合人成综合网站00 | 亚洲欧洲国产成人综合一本 | 亚洲欧美视频一区二区 | 国产亚洲人成网站在线观看 |