您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關iOS如何實現應用懸浮窗效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在一個app應用的最頂部添加一個懸浮窗,就像ios系統AssistiveTouch 可以左右滑動,但是最終會停在左邊或右邊。
在應用的視圖的最頂層添加一個UIWindow,用這個UIWindow 充當懸浮窗,給UIWindow添加移動的手勢監聽,讓懸浮窗隨著手指移動,釋放的時候,讓它以動畫的方式靠邊
//懸浮窗測試 //創建一個懸浮窗口 mwindow = [[AssistiveTouch alloc]initWithFrame:CGRectMake(100, 200, 40, 40) imageName:@"1.png"]; //ios9 window要設置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); }]; } } } }
關于“iOS如何實現應用懸浮窗效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。