美文网首页iOS小项目iOS DeveloperiOS OC 学习手册
iOS AssistiveTouch效果-跟随手指移动的butt

iOS AssistiveTouch效果-跟随手指移动的butt

作者: 孤独雪域 | 来源:发表于2016-10-21 21:30 被阅读1544次

    这是一个悬浮的按钮,类似于手机的AssistiveTouch效果,可以全工程悬浮的

    http://code.cocoachina.com/view/135532

    话不多说,上代码,复制黏贴即可

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,50,50)];

    btn.backgroundColor=[UIColor orangeColor];

    btn.layer.cornerRadius=25;

    btn.center=self.window.rootViewController.view.center;

    [self.window.rootViewController.view addSubview:btn];

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]

    initWithTarget:self

    action:@selector(handlePan:)];

    [btn addGestureRecognizer:panGestureRecognizer];

    - (void) handlePan:(UIPanGestureRecognizer*) recognizer

    {

    CGPoint translation = [recognizer translationInView:self.window.rootViewController.view];

    CGFloat centerX=recognizer.view.center.x+ translation.x;

    CGFloat thecenter=0;

    recognizer.view.center=CGPointMake(centerX,

    recognizer.view.center.y+ translation.y);

    [recognizer setTranslation:CGPointZero inView:self.window.rootViewController.view];

    if(recognizer.state==UIGestureRecognizerStateEnded|| recognizer.state==UIGestureRecognizerStateCancelled) {

    if(centerX>ScreenWidth/2) {

    thecenter=ScreenWidth-50/2;

    }else{

    thecenter=50/2;

    }

    [UIView animateWithDuration:0.3 animations:^{

    recognizer.view.center=CGPointMake(thecenter,

    recognizer.view.center.y+ translation.y);

    }];

    }

    }

    相关文章

      网友评论

      • Hanser0503:就不能好好排排版?看第一眼就不想看了
        Hanser0503:@人民重重 噗...那你就继续看这种随意排版的文章..
        春暖花已开:@尚雷勋 同意🤝
        14b3a6ac00ba:能实现你的需求就好了,复制到 Xcode 里面,自己去试试看,不就完了。不想看了,还来评论?
      • 神游子:👍👍👍

      本文标题:iOS AssistiveTouch效果-跟随手指移动的butt

      本文链接:https://www.haomeiwen.com/subject/xepcuttx.html