iOS實(shí)現(xiàn)全局懸浮按鈕
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)全局懸浮按鈕的具體代碼,供大家參考,具體內(nèi)容如下
現(xiàn)在有很多app都做這個(gè)全局按鈕

如上面兩張圖的效果,完成一個(gè)全局懸浮的按鈕,而且不會(huì)劃出屏幕外
既然是全局,那寫在AppDelegate中
UIWindow是一種特殊的UIView,它相當(dāng)于一塊畫框,而UIView相當(dāng)于里面的畫布。通常在一個(gè)app中只會(huì)有一個(gè)UIWindow。
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIButton *button; @end
AppDelegate.m
先button懶加載
- (UIButton*)button {
? ? if (!_button) {
? ? ? ? _button = [UIButton buttonWithType:UIButtonTypeCustom];
? ? ? ? _button.frame = CGRectMake(258, 450, 60, 60);//初始在屏幕上的位置
? ? ? ? [_button setImage:[UIImage imageNamed:@"bcl_btn_whole"] forState:UIControlStateNormal];
? ? }
? ? return _button;
}然后將其加在window上,設(shè)置手勢(shì)
-(void)createButton{
? ? if (!_button) {
? ? ? ? _window = [[UIApplication sharedApplication] keyWindow];
? ? ? ? _window.backgroundColor = [UIColor whiteColor];
? ? ? ? [_window addSubview:self.button];
? ? ? ? UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self action:@selector(locationChange:)];
? ? ? ? pan.delaysTouchesBegan = YES;
? ? ? ? [_button addGestureRecognizer:pan];
? ? }
}這個(gè)呢是為了開機(jī)啟動(dòng)兩秒后創(chuàng)建全局button
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? [self performSelector:@selector(createButton) withObject:nil afterDelay:2];
}最關(guān)鍵的就是設(shè)置button不要?jiǎng)澇銎聊煌?br />以下四個(gè)else if分別為屏幕的上下左右
設(shè)置一個(gè)標(biāo)記值isOVer
如果超出屏幕范圍,糾正回來
-(void)locationChange:(UIPanGestureRecognizer*)p{
? ? CGFloat HEIGHT=_button.frame.size.height;
? ? CGFloat WIDTH=_button.frame.size.width;
? ? BOOL isOver = NO;
? ? CGPoint panPoint = [p locationInView:[UIApplication sharedApplication].windows[0]];
? ? CGRect frame = CGRectMake(panPoint.x, panPoint.y, HEIGHT, WIDTH);
? ? NSLog(@"%f--panPoint.x-%f-panPoint.y-", panPoint.x, panPoint.y);
? ? if(p.state == UIGestureRecognizerStateChanged){
? ? ? ? _button.center = CGPointMake(panPoint.x, panPoint.y);
? ? }
? ? else if(p.state == UIGestureRecognizerStateEnded){
? ? ? ? if (panPoint.x + WIDTH > KScreenWidth) {
? ? ? ? ? ? frame.origin.x = KScreenWidth - WIDTH;
? ? ? ? ? ? isOver = YES;
? ? ? ? } else if (panPoint.y + HEIGHT > KScreenHeight) {
? ? ? ? ? ? frame.origin.y = KScreenHeight - HEIGHT;
? ? ? ? ? ? isOver = YES;
? ? ? ? } else if(panPoint.x - WIDTH / 2< 0) {
? ? ? ? ? ? frame.origin.x = 0;
? ? ? ? ? ? isOver = YES;
? ? ? ? } else if(panPoint.y - HEIGHT / 2 < 0) {
? ? ? ? ? ? frame.origin.y = 0;
? ? ? ? ? ? isOver = YES;
? ? ? ? }
? ? ? ? if (isOver) {
? ? ? ? ? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? ? ? ? ? self.button.frame = frame;
? ? ? ? ? ? }];
? ? ? ? }以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
- 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- iOS開發(fā)中UISwitch按鈕的使用方法簡(jiǎn)介
- 詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法
- 詳解iOS-按鈕單選與多選邏輯處理
- iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
- iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法
相關(guān)文章
iOS中PNChart與UITableView的聯(lián)動(dòng)示例詳解
PNChart是個(gè)界面很漂亮的圖表第三方庫(kù),UITableView則不用過多介紹了,各位iOS開發(fā)者們都知道,下面這篇文章主要給大家介紹了關(guān)于iOS中PNChart與UITableView的聯(lián)動(dòng)的相關(guān)資料,需要的朋友可以參考下2018-07-07
iOS緩存文件大小顯示功能和一鍵清理功能的實(shí)現(xiàn)方法
緩存占用了系統(tǒng)的大量空間,如何實(shí)時(shí)動(dòng)態(tài)的顯示緩存的大小,使用戶清晰的了解緩存的積累情況,有效的進(jìn)行一鍵清理呢?下面小編通過本文給大家介紹iOS緩存文件大小顯示功能和一鍵清理功能的實(shí)現(xiàn)方法,一起看看吧2016-10-10
關(guān)于iOS GangSDK的使用 為App快速集成社群公會(huì)模塊
這篇文章主要介紹了iOS GangSDK的使用為App快速集成社群公會(huì)模塊功能的實(shí)現(xiàn)過程。2017-11-11
IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03

