您的位置:首页 > 移动开发 > IOS开发

e项目总结一活动悬浮框的实现

2016-05-31 09:13 489 查看
            当时项目正值收尾,公司近期要举办一个投票的推广活动,然后就在首页上加了一个活动悬浮框,点进去后 调得是web端的活动页面 ,接口返回参数为1的时候让悬浮框出现,返回参数为0 的时候,让其消失 ,这是为了避免苹果审核时间过长而造成app上的进度不同 ,由于苹果的唯一识别码已经废弃 所以为了防止刷票 又在 进入活动界面的时候做了个登录提醒,因为账号是绑定手机号的,如果对方有那么多的手机号的话 刷票就由他去吧,也想过采用uuid + keychain 的方式 也就是把uuid由keychain储存,但他的局限性也只是能保证卸载重装的时候 
 uuid不变 但是刷机和升级系统的时候还是会改变 想想以前自己就是个刷机狂魔,so就将其舍去了。

           悬浮框是放在视图的window上的,当然悬浮框写成个代理……

         mwindow.assistiveDelegate =self;

        self.window = [[UIApplication
sharedApplication] keyWindow];

        [self.window
addSubview:mwindow];

    又为了悬浮框加了个拖拽功能 效果类似于苹果自带的AssistiveTouch 所以它需要改变位置 以及添加手势

一个拖拽,一个轻拍

  

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(locationChange:)];

        pan.delaysTouchesBegan =
YES;

        [self
addGestureRecognizer:pan];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(click:)];

        [self
addGestureRecognizer:tap];

    }

    

    self.timer = [NSTimer
scheduledTimerWithTimeInterval:0.1

                                                  target:self
selector:@selector(changePos)

                                                userInfo:nil
repeats:YES];

//改变位置

-(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];

        _viewAnima.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);

                }];

            }

        }

    }

}
//为了让活动的题目在小的悬浮框上显示出来 就将字体文字做了成跑马灯的效果

- (void) changePos

{

    CGPoint curPos =
_customLab.center;

   // NSLog(@"%f",self.customLab.center.x);

    //
当curPos的x坐标已经超过了屏幕的宽度

    if(curPos.x <  -100 )

    {

        CGFloat jianJu =
_customLab.frame.size.width/2;

       
// 控制蝴蝶再次从屏幕左侧开始移动

        _customLab.center =
CGPointMake(self.frame.size.width + jianJu, 20);

        

    }

    else

    {

        //
通过修改iv的center属性来改变iv控件的位置

        _customLab.center =
CGPointMake(curPos.x - 5, 20);

    }

    //其实整个移动都是————靠iv.center来去设置的

}

又为了只让悬浮框在首界面出现 so就让他在 viewWillAppear:(BOOL)animated上创建,在viewDidDisappear:(BOOL)animated 上remove掉就可以了 这一点要注意下 如果首界面进入下个界面是push出去的话 因为共用导航条 所以有时候在下界面的时候会不走viewDidDisappear:(BOOL)animated的方法
这样如果你频繁调用的话 就会让首界面重复创建悬浮框 这样的话 你就需要在首界面遵守 UINavigationControllerDelegate

然后在协议上加以限制

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

   
//每次当navigation中的界面切换,设为空。本次赋值只在程序初始化时执行一次

    static UIViewController *lastController =
nil;

    

    //若上个view不为空

    if (lastController !=
nil)

    {

        //若该实例实现了viewWillDisappear方法,则调用

        if ([lastController
respondsToSelector:@selector(viewWillDisappear:)])

        {

            [lastController viewWillDisappear:animated];

        }

    }

    

    //将当前要显示的view设置为lastController,在下次view切换调用本方法时,会执行viewWillDisappear

    lastController = viewController;

    

    [viewController viewWillAppear:animated];

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息