音楽トラックの再生の進行状況を表示し、トラック内でスクラブできるようにするカスタム スライダーを作成しました。
どちらも正常に機能しますが、ドラッグを停止してスライダーの位置を変更すると、わずかなラグ (およびジャンプした動き) が発生します - Apple Music アプリのスライダーはシームレスです。
_scrubberSlider = [[ScrubberSlider alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
_scrubberSlider.continuous = YES;
_scrubberSlider.maximumValue = 1.0f;
_scrubberSlider.minimumValue = 0.0f;
[_scrubberSlider addTarget:self action:@selector(handleSliderMove:) forControlEvents:UIControlEventValueChanged];
- (void) handleSliderMove:(UISlider*)sender
{
CGFloat currentPlayback = sender.value * [[_theMusicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration] floatValue];
_theMusicPlayer.currentPlaybackTime = currentPlayback;
}
-(void) handleTrackTime
{
if (!trackTimer)
{
NSNumber *playBackTime = [NSNumber numberWithFloat: _musicPlayer.currentPlaybackTime];
trackTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timeIntervalFinished:) userInfo:@{@"playbackTime" : playBackTime} repeats:YES];
}
}
-(void) timeIntervalFinished:(NSTimer*)sender
{
[self playbackTimeUpdated:_musicPlayer.currentPlaybackTime];
}
-(void) playbackTimeUpdated:(CGFloat)playbackTime
{
// Update time label
[self updatePosition];
}
-(void) updatePosition
{
if (!_scrubberSlider.isScrubbing)
{
CGFloat percent = _theMusicPlayer.currentPlaybackTime / [[_theMusicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration] floatValue];
_scrubberSlider.value = percent;
}
}
カスタムスライダー
@interface ScrubberSlider : UISlider
@property(nonatomic) BOOL isScrubbing;
@end
@implementation ScrubberSlider
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[super beginTrackingWithTouch:touch withEvent:event];
_isScrubbing = YES;
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[super endTrackingWithTouch:touch withEvent:event];
_isScrubbing = NO;
}
@end