最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果

 更新時(shí)間:2021年09月13日 16:14:22   作者:scau_quan  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)應(yīng)用懸浮窗效果的具體代碼,供大家參考,具體內(nèi)容如下

需求

在一個(gè)app應(yīng)用的最頂部添加一個(gè)懸浮窗,就像ios系統(tǒng)AssistiveTouch 可以左右滑動(dòng),但是最終會停在左邊或右邊。

實(shí)現(xiàn)思路

在應(yīng)用的視圖的最頂層添加一個(gè)UIWindow,用這個(gè)UIWindow 充當(dāng)懸浮窗,給UIWindow添加移動(dòng)的手勢監(jiān)聽,讓懸浮窗隨著手指移動(dòng),釋放的時(shí)候,讓它以動(dòng)畫的方式靠邊

代碼

//懸浮窗測試
//創(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)的手勢
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)文章

最新評論

宣恩县| 泰来县| 章丘市| 美姑县| 抚州市| 阆中市| 阿荣旗| 翁源县| 仙游县| 太康县| 曲周县| 浮山县| 瑞金市| 临夏县| 轮台县| 龙胜| 延吉市| 枣阳市| 阆中市| 华坪县| 沾益县| 永寿县| 金川县| 南华县| 汶川县| 华容县| 旬邑县| 桂林市| 奎屯市| 会泽县| 金乡县| 勐海县| 平顶山市| 比如县| 广东省| 团风县| 彭阳县| 平度市| 深水埗区| 炉霍县| 泰来县|