2015-01-24 2 views
0

나는 votes/total votes에 자료가 있고 투표율을 보여주는 퍼센트 등급 막대를 추가하고 싶습니다. 즉 : 95 % 및 막대 95 %를 채 웁니다. 나는 이것에 대한 어떠한 신속한 지시도 찾을 수 없었다. (UISlider에게 시도하기를 제외하고).스위프트에있는 비율 막대기

예 :

Rating Bar (350/700 votes): 
[==========50%   ] 

Rating Bar (180/200 votes): 
[==========90%========== ] 

Rating Bar (213/709 votes): 
[====== 30%   ] 
+0

왜 'UIProgressView'위에 'UILabel'을 표시하지 않으시겠습니까? – rmaddy

답변

1

배경보기로 UIView를 사용합니다. 그런 다음 다른보기를 진행률 표시 줄과 같이 하위보기로 추가하십시오. 클래스로 그 구현 Let`s :

class ProgressView: UIView { 

    var progress: CGFloat = 0 
    var filledView: UIView 

    override init(frame: CGRect) { 
     filledView = UIView(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 0, height: frame.height)) 
     filledView.backgroundColor = Colors.fontColor 

     super.init(frame: frame) 
     addSubview(filledView) 
    } 

    required init(coder aDecoder: NSCoder) { // <-- You need to implement this 
     fatalError() 
    } 

    func setProgess(var progress: CGFloat) { 
     progress = min(max(progress, 0), 1) // Between 0 and 1 
     self.progress = progress 

     filledView.frame.size.width = self.frame.width * progress 
    } 
} 

지금 당신은 또한 당신이 원하는 경우 비율을 보여주기 위해 UILabel를 추가 할 수 있습니다.

+0

'self.filledView = PageItemController(). rating1'에서 치명적인 오류가 계속 발생합니다. http://it.imgur.com/t4wBkwg.png ... 내 'PageItemController'에서'self.rating1.frame = CGRectMake '를 설정했습니다. (100.0,100.0,100.0,100.0) ' – Onichan

+0

흠. 표준 init 항목을'init (coder aDecoder : NSCoder)'에 복사하지 않는 이유는 무엇입니까? –

+0

죄송합니다. 저는 프로그래밍을 처음 접했습니다. 표준 init 항목은 무엇을 의미합니까? 나는 스토리 보드에 만든'UIView'로'self.filledView'를 초기화해야한다고 생각했습니다. – Onichan