日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios添加全局悬浮按钮_iOS开发悬浮按钮

發布時間:2025/3/20 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios添加全局悬浮按钮_iOS开发悬浮按钮 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

釋放雙眼,帶上耳機,聽聽看~!

#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(strong,nonatomic)UIWindow *window;

@property(strong,nonatomic)UIButton *button;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self performSelector:@selector(createButton) withObject:nil afterDelay:0];

}

#pragma mark - 創建懸浮的按鈕

- (void)createButton{

_window = [UIApplication sharedApplication].windows[0];

_button = [UIButton buttonWithType:UIButtonTypeCustom];

[_button setTitle:@"按鈕" forState:UIControlStateNormal];

_button.frame = CGRectMake(kSize_width - 70, kSize_height - 150, 60, 60);

_button.titleLabel.font = [UIFont systemFontOfSize:13.0f];

[_button setBackgroundColor:[UIColor orangeColor]];

_button.layer.cornerRadius = 30;

_button.layer.masksToBounds = YES;

[_button addTarget:self action:@selector(resignButton) forControlEvents:UIControlEventTouchUpInside];

//? ? _window = [[UIWindow alloc]initWithFrame:CGRectMake(kSize_width - 70, kSize_height - 150, 50, 50)];

//? ? _window.windowLevel = UIWindowLevelAlert+1;

//? ? _window.backgroundColor = [UIColor greenColor];

//? ? _window.layer.cornerRadius = 25;

//? ? _window.layer.masksToBounds = YES;

[_window addSubview:_button];

//? ? [_window makeKeyAndVisible];//顯示window

//放一個拖動手勢,用來改變控件的位置

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];

[_button addGestureRecognizer:pan];

}

- (void)resignButton{

}

//手勢事件 -- 改變位置

-(void)changePostion:(UIPanGestureRecognizer *)pan{

CGPoint point = [pan translationInView:_button];

CGFloat width = [UIScreen mainScreen].bounds.size.width;

CGFloat height = [UIScreen mainScreen].bounds.size.height;

CGRect originalFrame = _button.frame;

if (originalFrame.origin.x >= 0 && originalFrame.origin.x+originalFrame.size.width <= width) {

originalFrame.origin.x += point.x;

}if (originalFrame.origin.y >= 0 && originalFrame.origin.y+originalFrame.size.height <= height) {

originalFrame.origin.y += point.y;

}

_button.frame = originalFrame;

[pan setTranslation:CGPointZero inView:_button];

if (pan.state == UIGestureRecognizerStateBegan) {

_button.enabled = NO;

}else if (pan.state == UIGestureRecognizerStateChanged){

} else {

CGRect frame = _button.frame;

//是否越界

BOOL isOver = NO;

if (frame.origin.x < 0) {

frame.origin.x = 0;

isOver = YES;

} else if (frame.origin.x+frame.size.width > width) {

frame.origin.x = width - frame.size.width;

isOver = YES;

}if (frame.origin.y < 0) {

frame.origin.y = 0;

isOver = YES;

} else if (frame.origin.y+frame.size.height > height) {

frame.origin.y = height - frame.size.height;

isOver = YES;

}if (isOver) {

[UIView animateWithDuration:0.3 animations:^{

_button.frame = frame;

}];

}

_button.enabled = YES;

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 100;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

return [[UITableViewCell alloc] init];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

總結

以上是生活随笔為你收集整理的ios添加全局悬浮按钮_iOS开发悬浮按钮的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。