iOS實現(xiàn)應(yīng)用懸浮窗效果
本文實例為大家分享了iOS實現(xiàn)應(yīng)用懸浮窗效果的具體代碼,供大家參考,具體內(nèi)容如下
需求
在一個app應(yīng)用的最頂部添加一個懸浮窗,就像ios系統(tǒng)AssistiveTouch 可以左右滑動,但是最終會停在左邊或右邊。
實現(xiàn)思路
在應(yīng)用的視圖的最頂層添加一個UIWindow,用這個UIWindow 充當懸浮窗,給UIWindow添加移動的手勢監(jiān)聽,讓懸浮窗隨著手指移動,釋放的時候,讓它以動畫的方式靠邊
代碼
//懸浮窗測試 //創(chuàng)建一個懸浮窗口 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];
//添加移動的手勢 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); }]; } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Objective-C實現(xiàn)自定義的半透明導(dǎo)航
這篇文章主要為大家詳細介紹了Objective-C實現(xiàn)自定義的半透明導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2016-05-05iOS實現(xiàn)列表與網(wǎng)格兩種視圖的相互切換
相信大家應(yīng)該也都發(fā)現(xiàn)了,在現(xiàn)在很多的電商app中,都會有列表視圖和網(wǎng)格兩種視圖的相互切換。例如京東和淘寶。這樣更利于提高用戶的體驗度,所以這篇文章小編就是大家分享下利用iOS實現(xiàn)列表與網(wǎng)格兩種視圖相互切換的方法,文中介紹的很詳細,感興趣的下面來一起看看吧。2016-10-10iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié)
Xcode環(huán)境中包含CoreMotion框架,能夠幫助我們調(diào)用硬件設(shè)備的加速度傳感器和陀螺儀等感應(yīng)器,下面比較詳細地整理了iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié),需要的朋友可以參考下:2016-07-07IOS UIImagePickerController從拍照、圖庫、相冊獲取圖片
這篇文章主要介紹了IOS UIImagePickerController從拍照、圖庫、相冊獲取圖片的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09iOS使用pageViewController實現(xiàn)多視圖滑動切換
這篇文章主要為大家詳細介紹了iOS使用pageViewController實現(xiàn)多視圖滑動切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06