iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果的具體代碼,供大家參考,具體內(nèi)容如下
需求
在一個(gè)app應(yīng)用的最頂部添加一個(gè)懸浮窗,就像ios系統(tǒng)AssistiveTouch 可以左右滑動(dòng),但是最終會(huì)停在左邊或右邊。
實(shí)現(xiàn)思路
在應(yīng)用的視圖的最頂層添加一個(gè)UIWindow,用這個(gè)UIWindow 充當(dāng)懸浮窗,給UIWindow添加移動(dòng)的手勢(shì)監(jiān)聽(tīng),讓懸浮窗隨著手指移動(dòng),釋放的時(shí)候,讓它以動(dòng)畫的方式靠邊
代碼
//懸浮窗測(cè)試 //創(chuàng)建一個(gè)懸浮窗口 mwindow = [[AssistiveTouch alloc]initWithFrame:CGRectMake(100, 200, 40, 40) imageName:@"1.png"]; //ios9 window要設(shè)置rootview 不然崩潰 UIViewController *controller = [[UIViewController alloc] init]; mwindow.rootViewController = controller; //展示懸浮窗。。 [self.window makeKeyAndVisible];
//添加移動(dòng)的手勢(shì) UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(locationChange:)]; pan.delaysTouchesBegan = YES; [self addGestureRecognizer:pan];
//改變位置 -(void)locationChange:(UIPanGestureRecognizer*)p { //[[UIApplication sharedApplication] keyWindow] CGPoint panPoint = [p locationInView:[[UIApplication sharedApplication] keyWindow]]; if(p.state == UIGestureRecognizerStateBegan) { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeColor) object:nil]; _imageView.alpha = 0.8; } else if (p.state == UIGestureRecognizerStateEnded) { [self performSelector:@selector(changeColor) withObject:nil afterDelay:4.0]; } if(p.state == UIGestureRecognizerStateChanged) { self.center = CGPointMake(panPoint.x, panPoint.y); } else if(p.state == UIGestureRecognizerStateEnded) { if(panPoint.x <= kScreenWidth/2) { if(panPoint.y <= 40+HEIGHT/2 && panPoint.x >= 20+WIDTH/2) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(panPoint.x, HEIGHT/2); }]; } else if(panPoint.y >= kScreenHeight-HEIGHT/2-40 && panPoint.x >= 20+WIDTH/2) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(panPoint.x, kScreenHeight-HEIGHT/2); }]; } else if (panPoint.x < WIDTH/2+15 && panPoint.y > kScreenHeight-HEIGHT/2) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(WIDTH/2, kScreenHeight-HEIGHT/2); }]; } else { CGFloat pointy = panPoint.y < HEIGHT/2 ? HEIGHT/2 :panPoint.y; [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(WIDTH/2, pointy); }]; } } else if(panPoint.x > kScreenWidth/2) { if(panPoint.y <= 40+HEIGHT/2 && panPoint.x < kScreenWidth-WIDTH/2-20 ) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(panPoint.x, HEIGHT/2); }]; } else if(panPoint.y >= kScreenHeight-40-HEIGHT/2 && panPoint.x < kScreenWidth-WIDTH/2-20) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(panPoint.x, 480-HEIGHT/2); }]; } else if (panPoint.x > kScreenWidth-WIDTH/2-15 && panPoint.y < HEIGHT/2) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(kScreenWidth-WIDTH/2, HEIGHT/2); }]; } else { CGFloat pointy = panPoint.y > kScreenHeight-HEIGHT/2 ? kScreenHeight-HEIGHT/2 :panPoint.y; [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(320-WIDTH/2, pointy); }]; } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于iOS 11不能定位問(wèn)題的解決方法
最近在開(kāi)發(fā)中遇到一個(gè)問(wèn)題,后來(lái)發(fā)現(xiàn)是蘋果新增一個(gè)權(quán)限,自己可查,下面這篇文章主要給大家介紹了關(guān)于iOS 11不能定位問(wèn)題的解決方法,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11ios利用正則表達(dá)式判斷手機(jī)號(hào)碼格式是否正確的實(shí)例
下面小編就為大家分享一篇ios利用正則表達(dá)式判斷手機(jī)號(hào)碼格式是否正確的實(shí)例,具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Objective-C實(shí)現(xiàn)自定義的半透明導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Objective-C實(shí)現(xiàn)自定義的半透明導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2016-05-05iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖的相互切換
相信大家應(yīng)該也都發(fā)現(xiàn)了,在現(xiàn)在很多的電商app中,都會(huì)有列表視圖和網(wǎng)格兩種視圖的相互切換。例如京東和淘寶。這樣更利于提高用戶的體驗(yàn)度,所以這篇文章小編就是大家分享下利用iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖相互切換的方法,文中介紹的很詳細(xì),感興趣的下面來(lái)一起看看吧。2016-10-10iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié)
Xcode環(huán)境中包含CoreMotion框架,能夠幫助我們調(diào)用硬件設(shè)備的加速度傳感器和陀螺儀等感應(yīng)器,下面比較詳細(xì)地整理了iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié),需要的朋友可以參考下:2016-07-07iOS判斷用戶是否打開(kāi)APP通知開(kāi)關(guān)
這篇文章主要為大家詳細(xì)介紹了iOS判斷用戶是否打開(kāi)APP通知開(kāi)關(guān)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04iOS通過(guò)block在兩個(gè)頁(yè)面間傳值的方法
不知道大家有沒(méi)有發(fā)現(xiàn),在實(shí)際開(kāi)發(fā)中使用block的地方特別多,block比delegate和notification有著更簡(jiǎn)潔的優(yōu)勢(shì),下面這篇文章我們來(lái)簡(jiǎn)單了解一下block在兩個(gè)頁(yè)面之間的傳值。有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11iOS簡(jiǎn)單抽屜效果的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了iOS簡(jiǎn)單抽屜效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08IOS UIImagePickerController從拍照、圖庫(kù)、相冊(cè)獲取圖片
這篇文章主要介紹了IOS UIImagePickerController從拍照、圖庫(kù)、相冊(cè)獲取圖片的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06