ios 底部拖拉菜单

这个简易 的底部拖拉菜单没什么工作量,这里氷分析了,直接上代码,距离属性没剥离出来,不过很简单,各位大神用到的时候,自己扩展吧



代码如下:

@interface ViewController ()

@property(nonatomic,strong) UIView* bottomView;

@property(nonatomic, strong) NSLayoutConstraint *bottomHeightCons;

@property(nonatomic,assign) CGPoint startPos;

@property(nonatomic,assign) CGPoint endPos;

@property(nonatomic,assign) CGPoint originPos;

@property(nonatomic,assign) BOOL bIsDirectionDowm;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initView];
    
}

-(void) initView{

    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100,
                                                               self.view.frame.size.width, 300)];
    self.bottomView.backgroundColor = [UIColor greenColor];
    self.bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(100, self.bottomView.frame.size.height - 80, 160, 40)];
    label.text = @"底部隐藏菜单";
    [self.bottomView addSubview:label];
    
    
    [self.view addSubview:self.bottomView];
    
    self.originPos = self.bottomView.center;
    
    self.bIsDirectionDowm = false;
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    self.startPos = [touch locationInView:self.view];
    

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    UITouch *touch = [touches anyObject];
    CGPoint curP = [touch locationInView:self.view];

    
    if(curP.y - self.startPos.y <= 0 && curP.y - self.startPos.y >= -200){      //向上滑
        
        if(self.bIsDirectionDowm) return;
        self.bottomView.center = CGPointMake(self.bottomView.center.x, self.originPos.y + ( curP.y - self.startPos.y));
        
    }else if(curP.y - self.startPos.y > 0 && curP.y - self.startPos.y <= 100){ //向下滑动
    
        if(!self.bIsDirectionDowm) return;
        self.bottomView.center = CGPointMake(self.bottomView.center.x, self.originPos.y - 100 + ( curP.y - self.startPos.y));
    }
    
}

//当手指离开屏幕时调用
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    self.endPos = [touch locationInView:self.view];

    if(self.endPos.y - self.startPos.y <= 0 && self.endPos.y - self.startPos.y >= -100){  //向上滑动
    
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.bottomView.center = CGPointMake(self.bottomView.center.x, self.view.frame.size.height - self.bottomView.frame.size.height / 2);
            self.bIsDirectionDowm = true;
            
        }];
        
    }
    
    else if(self.endPos.y - self.startPos.y >= 0 && self.endPos.y - self.startPos.y <= 100){
    
        
        [UIView animateWithDuration:0.1 animations:^{
            
            self.bottomView.center = CGPointMake(self.bottomView.center.x, self.originPos.y);
            self.bIsDirectionDowm = false;
            
        }];
        
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



猜你喜欢

转载自blog.csdn.net/d06110902002/article/details/78027500