UITapGestureRecognizer 和UIPanGestureRecognizer的使用,触摸和滑动

/**

 *  点击了进度条背景

 */

- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {

    CGPoint point = [sender locationInView:sender.view];

    

    // 切换歌曲的当前播放时间

    self.player.currentTime = (point.x / sender.view.width) * self.player.duration;

    

    [self updateCurrentTime];

}

////////////滑动

- (IBAction)panSlider:(UIPanGestureRecognizer *)sender {

    // 获得挪动的距离

    CGPoint t = [sender translationInView:sender.view];

    [sender setTranslation:CGPointZero inView:sender.view];

    

    // 控制滑块和进度条的frame

    CGFloat sliderMaxX = self.view.width - self.slider.width;

    self.slider.x += t.x;

    if (self.slider.x < 0) {

        self.slider.x = 0;

    } else if (self.slider.x > sliderMaxX) {

        self.slider.x = sliderMaxX;

    }

    

    self.progressView.width = self.slider.center.x;

    

    // 设置时间值

    double progress = self.slider.x / sliderMaxX;

    NSTimeInterval time = self.player.duration * progress;

    [self.slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];

    

    // 显示半透明指示器的文字

    [self.currentTimeView setTitle:self.slider.currentTitle forState:UIControlStateNormal];

    self.currentTimeView.x = self.slider.x;

    

    if (sender.state == UIGestureRecognizerStateBegan) {

        NSLog(@"UIGestureRecognizerStateBegan");

        // 停止定时器

        [self removeCurrentTimeTimer];

        

        // 显示半透明指示器

        self.currentTimeView.hidden = NO;

        self.currentTimeView.y = self.currentTimeView.superview.height - 10 - self.currentTimeView.height;

    } else if (sender.state == UIGestureRecognizerStateEnded) { // 手松开

        // 设置播放器的时间

        self.player.currentTime = time;

        

        NSLog(@"UIGestureRecognizerStateEnded");

        // 开始定时器

        [self addCurrentTimeTimer];

        

        // 隐藏半透明指示器

        self.currentTimeView.hidden = YES;

    }

    

}

posted on 2015-10-14 15:39  彪biao  阅读(942)  评论(0编辑  收藏  举报

导航