iMoive拖动选择框

2018-04-19  本文已影响0人  风动青春

- (void)loadView {

    [super loadView];

    preBgView = [[UIView alloc] initWithFrame:CGRectMake(10, 70, Screen_Width-20, 90)];

    preBgView.backgroundColor = UIColorFromRGB(0x888888);

    [self.view addSubview:preBgView];

    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];

    [preBgView addGestureRecognizer:panGesture];

    sliderBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, preBgView.frame.size.width, preBgView.frame.size.height)];

    sliderBgView.layer.borderWidth = 5;

    sliderBgView.layer.borderColor = UIColorFromRGB(999999).CGColor;

    sliderBgView.userInteractionEnabled = YES;

    [preBgView addSubview:sliderBgView];

    leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, sliderBgView.frame.size.height)];

    leftBtn.backgroundColor = [UIColor redColor];

    [sliderBgView addSubview:leftBtn];

    UIPanGestureRecognizer *leftPanGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleLeftPanGesture:)];

    [leftBtn addGestureRecognizer:leftPanGesture];

    rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(sliderBgView.frame.size.width-20, 0, 20, sliderBgView.frame.size.height)];

    rightBtn.backgroundColor = [UIColor yellowColor];

    [sliderBgView addSubview:rightBtn];

    UIPanGestureRecognizer *rightPanGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleRightPanGesture:)];

    [rightBtn addGestureRecognizer:rightPanGesture];

}

#pragma mark - Gesture

-(void)handlePanGesture:(UIPanGestureRecognizer *)sender {

    //得到拖过程中的xy坐标

    CGPoint translation = [(UIPanGestureRecognizer *)sender locationInView:preBgView];

    if(sender.state==UIGestureRecognizerStateChanged) {

        // 根据拖动位置,快进视频预览进度

    }

}

-(void)handleLeftPanGesture:(UIPanGestureRecognizer *)sender {

    //得到拖过程中的xy坐标

    CGPoint translation = [(UIPanGestureRecognizer *)sender locationInView:preBgView];

    if (translation.x < 0 || sliderBgView.frame.size.width+sliderBgView.frame.origin.x < translation.x+rightBtn.frame.size.width*2 ) {

        return;

    }

    CGFloat intX = translation.x - sliderBgView.frame.origin.x;

    if(sender.state==UIGestureRecognizerStateChanged)

    {

        CGRect rect = sliderBgView.frame;

        rect.origin.x = translation.x;

        rect.size.width = sliderBgView.frame.size.width - intX;

        sliderBgView.frame = rect;

        rect = rightBtn.frame;

        rect.origin.x = sliderBgView.frame.size.width-rightBtn.frame.size.width;

        rightBtn.frame = rect;

    }

}

-(void)handleRightPanGesture:(UIPanGestureRecognizer *)sender {

    //得到拖过程中的xy坐标

    CGPoint translation = [(UIPanGestureRecognizer *)sender locationInView:preBgView];

    if (translation.x+rightBtn.frame.size.width >= preBgView.frame.size.width) {

        return;

    }

    CGFloat intX = sliderBgView.frame.size.width+sliderBgView.frame.origin.x - translation.x;

    if(sender.state==UIGestureRecognizerStateChanged)

    {

        CGRect rect = sliderBgView.frame;

        rect.size.width = sliderBgView.frame.size.width - intX;

        sliderBgView.frame = rect;

        rect = rightBtn.frame;

        rect.origin.x = sliderBgView.frame.size.width-rightBtn.frame.size.width;

        rightBtn.frame = rect;

    }

}

上一篇 下一篇

猜你喜欢

热点阅读