2015-05-27 35 views
0

我有用於播放視頻的UISlider,我想在我的UISlider中放置紅色的「中斷指示」線,以便在休息時間時直觀地顯示用戶如下圖所示。我的滑塊有一組.duration屬性,而且我有一個包含時間戳的陣列,其中包含視頻暫停休息所需的時間。我仍然得到了iOS的縈繞,所以我不知道如何去描繪UISlider的線條。我希望它看起來與此類似:使用For循環在UISlider上繪製線條

enter image description here

在我的研究,我在蘋果文檔閱讀UISlider幸運的是提供滑塊的方法座標基於一個浮點值。這是完美的,因爲現在我可以確定滑塊的位置,我可以根據數組中的時間戳繪製黃線,對吧?

所以,我創建了一個for循環來調用該方法,並且(嘗試)劃一條線。

畫線部分是我遇到的問題。我一直在閱讀Apple網站上的文檔和其他問題,但我似乎無法弄清楚繪圖邏輯。它繪製得很好,問題在於它的座標都是錯誤的。它繪製並重疊了其大部分行的特定位置,即我的視圖的左上角。這是我試圖做:

更新的代碼在與相關答案@Bannings(非常感謝你)。

- (void)breakStarted:(NSNotification *)notification { 

     self.lines = [NSMutableArray new]; 

     for (int i = 0; i < myArray.count; i++) { 
      long long nanoSeconds = [[myArray.adMarkerTimes objectAtIndex:i] floatValue]; 
      float minutes = nanoSeconds/60000000000; 

      [self sliderThumbCenter:self.scrubberSliderView forValue:minutes]; 

      NSLog(@"Minute Readings: %f", minutes); 
     } 
    } 

    - (float)sliderThumbCenter:(UISlider *)slider forValue:(float)value { 

    CGRect trackRect = [slider trackRectForBounds:slider.bounds]; 
    CGRect thumbRect = [slider thumbRectForBounds:slider.bounds trackRect:trackRect value:value]; 
    CGFloat centerThumb = CGRectGetMidX(thumbRect); 

    NSLog(@"Center Thumb/Line Placements on slider are: %f", centerThumb); 

    [self.lines addObject:@(centerThumb)]; // Added the rect values to an array which we will loop through to draw the lines over the slider 

    [self setNeedsDisplay]; 

    return centerThumb; 
} 
- (void)drawRect:(CGRect)rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(context, 2); 

    for (NSNumber *x in self.lines) { 
     CGContextMoveToPoint(context, x.floatValue, 0); 
     CGContextAddLineToPoint(context, x.floatValue, rect.size.height); 
     CGContextStrokePath(context); 
    } 
} 

我忘了補充:我手動將CMTimeValue轉換爲循環中的秒數。那是myArray.adMarkerTimes。也許我做錯了...

+0

「這裏就是我迷路了,我不知道如何使這些值匹配和借鑑汲取什麼樣的匹配了一下在任何情況下,你不能得出???在'sliderThumbCenter'中,所以你完全不清楚你的想法 – matt

回答

3

你可以覆蓋UISlider的drawRect

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(context, 2); 


    CGContextMoveToPoint(context, 20, 0); 
    CGContextAddLineToPoint(context, 20, rect.size.height); 

    CGContextStrokePath(context); 
} 

或將UIView插入到UISlider。

- (UIView *)lineViewForRect:(CGRect)rect { 
    UIView *lineView = [[UIView alloc] initWithFrame:rect]; 
    lineView.backgroundColor = [UIColor redColor]; 
    return lineView; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [slider addSubview:[self lineViewForRect:CGRectMake(20, 0, 2, slider.bounds.size.height)]]; 
} 

您還可以設置lineView.layer.zPosition = 1

前:

enter image description here

後:

enter image description here


編輯: 您可以將行存儲在數組中,並在上下文中繪製每行。
這似乎是這樣的:

// YourSlider.m 
- (void)addLineToX:(CGFloat)x { 
    [self.lines addObject:@(x)]; 

    // This will cause drawRect to be called 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(context, 2); 

    for (NSNumber *x in self.lines) { 
     CGContextMoveToPoint(context, x.floatValue, 0); 
     CGContextAddLineToPoint(context, x.floatValue, rect.size.height); 
    } 

    CGContextStrokePath(context); 
} 
+0

你先生,我已經幫了我很大的忙,哈哈,謝謝你,我正在使用你的第一個解決方案,並且第一次紅線出現!我遇到的唯一問題是將其與'sliderThumbCenter'方法相關聯,這是我調用'[self drawRect:thumbRect];'的地方。嘗試使用centerThumb,但它是CGFloat。如果沒有意義,我可以在OP中更新我的代碼。 – John

+0

@John你不應該直接調用這個方法(drawRect)!我編輯過,請告訴我,如果這項工作 – Bannings

+0

非常感謝您的洞察力和解釋,我真的很感激。由於某種原因,這次它將所有的線條放在彼此之上......我用我的新代碼更新了我的OP。再次感謝你的幫助! – John