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);
                
            }];
            
        }
        
    
    }
  • 相关阅读:
    2021年欺骗式防御技术将迎来爆发
    可以简化决策过程的10个大数据源
    “AI+”改变世界!不同领域的5大人工智能趋势
    预测2021: 区块链领域新景观
    后量子时代的密码学
    考试
    进度总结8
    进度总结7
    进度总结6
    进度总结5
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/6141093.html
Copyright © 2011-2022 走看看