zoukankan      html  css  js  c++  java
  • iOS 自由拖动的滑块

    完全自由拖动的滑块

    floatViewExample

      

    代码实现:

    @implementation FloatView
    
    #pragma mark - lazyload
    
    - (UIImageView *)imageView {
        if (_imageView == nil) {
            _imageView = [[UIImageView alloc]init];
            _imageView.image = [UIImage imageNamed:@"telephone"];
            _imageView.frame = self.bounds;
        }
        return _imageView;
    }
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        
        UITouch *touch = [touches anyObject];
        //CGPoint pt = [touch locationInView:self.superview];
        CGRect frame = self.frame;
        //在自身视图中的位置
        CGPoint pt = [touch locationInView:self];
        CGPoint anchorPoint = CGPointMake(pt.x/self.bounds.size.width, pt.y/self.bounds.size.height);
        //这是设置的是position也就是center的位置百分比
        //也就是鼠标所在的位置
        self.layer.anchorPoint = anchorPoint;
        self.frame = frame;
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint pt = [touch locationInView:self.superview];
        
        self.layer.position = pt;
        
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [self limitRangeOfView];
    }
    
    - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self limitRangeOfView];
    }
    
    
    
    - (void)limitRangeOfView {
        [UIView animateWithDuration:0.3 animations:^{
            
            CGFloat padding = 1;
            CGRect frame = self.frame;
            if (frame.origin.x < padding) {
                frame.origin.x = padding;
            }
            if (frame.origin.y < padding) {
                frame.origin.y = padding;
            }
            if (frame.origin.x > (kScreenWidth-frame.size.height-padding)) {
                frame.origin.x = kScreenWidth-frame.size.width-padding;
            }
            if (frame.origin.y > (kScreenHeight-frame.size.height-padding)) {
                frame.origin.y = kScreenHeight-frame.size.height-padding;
            }
            
            self.frame = frame;
        }];
    }
    
    @end
    

      

    github 地址:floatViewExample

  • 相关阅读:
    简述Mesos API–files
    docker-compose常用命令
    Linux命令行--使用linux环境变量(转)
    docker:从 tomcat 容器连接到 mysql 容器
    开发环境、生产环境、测试环境的基本理解和区别(转)
    Linux命令行–更多bash shell命令(转)
    docker启动Mysql(转)
    Linux命令行–基本的bash shell命令
    浅谈 man 命令的日常使用
    Linux命令行–走进shell
  • 原文地址:https://www.cnblogs.com/saytome/p/6973169.html
Copyright © 2011-2022 走看看