NJHu / iOSProject

iOS project of collected some demos for iOS App, use Objective-C

Home Page:https://github.com/NJHu/iOSProject

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

类似高德百度地图内部的上拉滑动上滑拖出的抽屉拖拽效果

maple-y opened this issue · comments

可以接入一个类似高德百度地图内部的上拉滑动上滑拖出的抽屉拖拽效果,自己写了一个简单的,但是在移动的view和tableview滚动的第一响应切换时出现卡顿。有什么方式可以完美的解决这个问题吗

commented

复制以下代码即可实现左右的抽屉效果- 参考下哈

@interface ViewController ()

@property (weak, nonatomic) UIView *leftView;
@property (weak, nonatomic) UIView *rightView;
@property (weak, nonatomic) UIView *mainView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    // Do any additional setup after loading the view, typically from a nib.
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
    
    [self.view addGestureRecognizer:panGR];
}

#define offYMax 80.0
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
#define yxScale offYMax/screenW
#define autoMaxOffX 275.0
#define autoMinOffX -250.0

- (void)panGR:(UIPanGestureRecognizer *)panGR
{
    CGPoint transP = [panGR translationInView:self.view];
    
    self.mainView.frame = [self frameWithOffPoint:transP];
    self.rightView.hidden = (self.mainView.frame.origin.x > 0);
    
    [panGR setTranslation:CGPointZero inView:self.view];
    
    
    CGPoint transP2 = CGPointZero;
    if(panGR.state == UIGestureRecognizerStateEnded)
    {
        if(self.mainView.frame.origin.x > screenW/2)
        {
            transP2 = CGPointMake(autoMaxOffX - self.mainView.frame.origin.x, 0);
        }
        else if(CGRectGetMaxX(self.mainView.frame) < screenW/2)
        {
            transP2 = CGPointMake(autoMinOffX - self.mainView.frame.origin.x, 0);
        }
        else
        {
            transP2 = CGPointMake(0 - self.mainView.frame.origin.x, 0);
        }
        [UIView animateWithDuration:0.25 animations:^{
            self.mainView.frame = [self frameWithOffPoint:transP2];
        }];
        
    }
}


- (CGRect)frameWithOffPoint:(CGPoint)offPoint
{
    CGRect frame = self.mainView.frame;
    
    CGFloat preX = frame.origin.x;
    CGFloat preW = frame.size.width;
    CGFloat preH = frame.size.height;
    
    CGFloat nowX = preX + offPoint.x;
    CGFloat nowY = yxScale * (nowX>0?nowX:(-nowX));
    CGFloat nowH = screenH - 2 * nowY;
    
    CGFloat hScale = nowH / preH;
    CGFloat nowW = preW * hScale;
    
    frame.origin.x = nowX;
    frame.origin.y = nowY;
    frame.size.width = nowW;
    frame.size.height = nowH;
    
    return frame;
}
- (void)setupViews
{
    UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
    leftView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:leftView];
    self.leftView = leftView;
    
    
    UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
    rightView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:rightView];
    self.rightView = rightView;
    
    
    UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
    mainView.backgroundColor = [UIColor redColor];
    [self.view addSubview:mainView];
    self.mainView = mainView;
    
}

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

@end