欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现应用悬浮窗效果

程序员文章站 2022-03-22 11:14:10
本文实例为大家分享了ios实现应用悬浮窗效果的具体代码,供大家参考,具体内容如下需求在一个app应用的最顶部添加一个悬浮窗,就像ios系统assistivetouch 可以左右滑动,但是最终会停在左边...

本文实例为大家分享了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 悬浮窗