zoukankan      html  css  js  c++  java
  • iOS 悬浮可移动按钮

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(100, 100, 50, 50);
        btn.backgroundColor = [UIColor yellowColor];
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = 25;
        btn.tag = 10086;
        [self.view addSubview:btn];
        
        //添加手势
        //添加移动的手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(locationChange:)];
        pan.delaysTouchesBegan = YES;
        [btn addGestureRecognizer:pan];
        
    }
    
    - (void)locationChange:(UIPanGestureRecognizer *)recognizer
    {
        
        CGPoint translation = [recognizer translationInView:self.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.view];
        
        if(recognizer.state==UIGestureRecognizerStateEnded|| recognizer.state==UIGestureRecognizerStateCancelled) {
            
            if(centerX>ScreenWidth/2) {
                
                thecenter=ScreenWidth-50;
                
            }else{
                
                thecenter=50;
                
            }
            [UIView animateWithDuration:0.3 animations:^{
                
                recognizer.view.center=CGPointMake(thecenter,
                                                   
                                                   recognizer.view.center.y+ translation.y);
                
            }];
            
        }
        
    
    }
  • 相关阅读:
    刷题94—树(一)
    刷题93—动态规划(十)
    刷题92—动态规划(九)
    刷题91—动态规划(八)
    android Q build 变化
    ubuntu下解压rar文件
    Android PAI (PlayAutoInstall)预装APK 功能
    MTK Android O1平台预置apk
    预置第三方apk到MTK项目相关问题总结
    Android预置Apk方法
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/6141093.html
Copyright © 2011-2022 走看看