iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果的具體代碼,供大家參考,具體內(nèi)容如下
需求
在一個app應(yīng)用的最頂部添加一個懸浮窗,就像ios系統(tǒng)AssistiveTouch 可以左右滑動,但是最終會停在左邊或右邊。
實(shí)現(xiàn)思路
在應(yīng)用的視圖的最頂層添加一個UIWindow,用這個UIWindow 充當(dāng)懸浮窗,給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é)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS CAReplicatorLayer實(shí)現(xiàn)脈沖動畫效果
這篇文章主要介紹了iOS CAReplicatorLayer實(shí)現(xiàn)脈沖動畫效果 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析
UINavigation可以附著于導(dǎo)航控制器之中使用,也可以在controller中單獨(dú)使用,這里我們將來看iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析.2016-06-06
iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
這篇文章主要介紹了iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用的方法,實(shí)例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法
本篇文章主要介紹了iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
ios利用正則表達(dá)式判斷手機(jī)號碼格式是否正確的實(shí)例
下面小編就為大家分享一篇ios利用正則表達(dá)式判斷手機(jī)號碼格式是否正確的實(shí)例,具有很好的參考價值。希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11
詳解Swift中對C語言接口緩存的使用以及數(shù)組與字符串轉(zhuǎn)為指針類型的方法
這篇文章主要介紹了詳解Swift中對C語言接口緩存的使用以及數(shù)組與字符串轉(zhuǎn)為指針類型的方法的相關(guān)資料,這里提供簡單實(shí)例,代碼注釋介紹也清楚,需要的朋友可以參考下2017-07-07

