2015-01-05 1 views
1

i learn swift. 나는 질문이있다.xcode의 맞춤 uislider

http://i57.tinypic.com/2ztfudi.jpg http://i57.tinypic.com/2ztfudi.jpg

정적 라벨 변경 : 버튼 - 다음과 같이 작성하는 방법, 거품 레이블은 항상을 그들이 + 슬라이더도 +1 (라벨도 +1 변경) 변경 누르면 또한, 슬라이더 원을 따르세요 슬라이더 값을 변경하는 경우 :

@IBAction func sliderValueChanged(sender: UISlider) { 
    var currentValue = Int(sender.value) 
    stepper.value = Double(currentValue) 
    labelku.text = "\(currentValue)" 
} 

정적 라벨 변경 스테퍼 값 변화가있는 경우 :

@IBAction func stepperValueChanged(sender: UIStepper) { 
    var stepperValue = Int(sender.value) 
    slider.value = Float(stepperValue) 
    labelku.text = Int(sender.value).description 
} 

편집 : 제가 넣어 각 ibaction의 버블 구문입니다. 따라서 currentValue 또는 stepperValue가 변경 될 때마다 거품이 위치를 변경합니다.

편집 : 여기에는 긴 코드가 있지만 짧을 수 있습니다. 너하기에 달렸다. 나는 더 긴 것을 좋아한다.

@IBAction func sliderValueChanged(sender: UISlider) { 
    var currentValue = Int(sender.value) 
    stepper.value = Double(currentValue) 
    labelku.text = "\(currentValue)" 
    if(currentValue == 0 || currentValue == 1 || currentValue == 2 || currentValue == 3 || currentValue == 4 || currentValue == 5 || currentValue == 6){ 
     self.catImageView.hidden = false 
     self.catImageView2.hidden = true 
     self.catImageView3.hidden = true 
     let catImages1 = (1...6).map { UIImage(named: "ra_tr_\($0)")! } 
     self.catImageView.animationImages = catImages1 
     self.catImageView.animationDuration = 1.0 
     self.catImageView.startAnimating() 
    }else if(currentValue == 7 || currentValue == 8){ 
     self.catImageView.stopAnimating() 
     self.catImageView.hidden = true 
     self.catImageView2.hidden = false 
     self.catImageView3.hidden = true 
     let catImages2 = (1...6).map { UIImage(named: "ra_av_\($0)")! } 
     self.catImageView2.animationImages = catImages2 
     self.catImageView2.animationDuration = 1.0 
     self.catImageView2.startAnimating() 
    }else if(currentValue == 9 || currentValue == 10){ 
     self.catImageView2.stopAnimating() 
     self.catImageView.hidden = true 
     self.catImageView2.hidden = true 
     self.catImageView3.hidden = false 
     let catImages3 = (1...12).map { UIImage(named: "ra_ve_\($0)")! } 
     self.catImageView3.animationImages = catImages3 
     self.catImageView3.animationDuration = 1.0 
     self.catImageView3.startAnimating() 
    } 
    if(currentValue == 0){ 
     nomor0.hidden = false 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 1){ 
     nomor0.hidden = true 
     nomor1.hidden = false 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 2){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = false 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 3){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = false 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 4){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = false 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 5){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = false 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 6){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = false 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 7){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = false 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 8){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = false 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(currentValue == 9){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = false 
     nomor10.hidden = true 
    }else if(currentValue == 10){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = false 
    } 
} 
@IBAction func stepperValueChanged(sender: UIStepper) { 
    var stepperValue = Int(sender.value) 
    //println(Int(sender.value)) 
    slider.value = Float(stepperValue) 
    labelku.text = Int(sender.value).description 
    if(stepperValue == 0 || stepperValue == 1 || stepperValue == 2 || stepperValue == 3 || stepperValue == 4 || stepperValue == 5 || stepperValue == 6){ 
     self.catImageView.hidden = false 
     self.catImageView2.hidden = true 
     self.catImageView3.hidden = true 
     let catImages1 = (1...6).map { UIImage(named: "ra_tr_\($0)")! } 
     self.catImageView.animationImages = catImages1 
     self.catImageView.animationDuration = 1.0 
     self.catImageView.startAnimating() 
    }else if(stepperValue == 7 || stepperValue == 8){ 
     self.catImageView.stopAnimating() 
     self.catImageView.hidden = true 
     self.catImageView2.hidden = false 
     self.catImageView3.hidden = true 
     let catImages2 = (1...6).map { UIImage(named: "ra_av_\($0)")! } 
     self.catImageView2.animationImages = catImages2 
     self.catImageView2.animationDuration = 1.0 
     self.catImageView2.startAnimating() 
    }else if(stepperValue == 9 || stepperValue == 10){ 
     self.catImageView2.stopAnimating() 
     self.catImageView.hidden = true 
     self.catImageView2.hidden = true 
     self.catImageView3.hidden = false 
     let catImages3 = (1...12).map { UIImage(named: "ra_ve_\($0)")! } 
     self.catImageView3.animationImages = catImages3 
     self.catImageView3.animationDuration = 1.0 
     self.catImageView3.startAnimating() 
    } 
    if(stepperValue == 0){ 
     nomor0.hidden = false 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 1){ 
     nomor0.hidden = true 
     nomor1.hidden = false 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 2){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = false 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 3){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = false 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 4){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = false 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 5){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = false 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 6){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = false 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 7){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = false 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 8){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = false 
     nomor9.hidden = true 
     nomor10.hidden = true 
    }else if(stepperValue == 9){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = false 
     nomor10.hidden = true 
    }else if(stepperValue == 10){ 
     nomor0.hidden = true 
     nomor1.hidden = true 
     nomor2.hidden = true 
     nomor3.hidden = true 
     nomor4.hidden = true 
     nomor5.hidden = true 
     nomor6.hidden = true 
     nomor7.hidden = true 
     nomor8.hidden = true 
     nomor9.hidden = true 
     nomor10.hidden = false 
    } 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 
    stepper.wraps = true 
    stepper.autorepeat = true 
    stepper.minimumValue = 0 
    slider.minimumValue = 0 
    stepper.maximumValue = 10 
    slider.maximumValue = 10 
    stepper.stepValue = 1 
    stepper.value = 0 
    slider.value = 0 
    questionText.text = question.description 

    self.catImageView.animationDuration = 1.0 
    self.catImageView.animationImages = getCatAnimation(labelku.tag) 
    self.catImageView.startAnimating() 

    answerLabel.layer.borderWidth = 1.0 
    answerLabel.layer.cornerRadius = 5.2 
    answerLabel.layer.borderColor = ColorLightGreyForQuestion.CGColor 
    answerLabel.text = placeHolderText 

    self.answerLabel.backgroundColor = UIColor(patternImage: UIImage(named: "buble.png")!) 
    answerLabel.textColor = UIColor.whiteColor() 
} 

희망이 다른 사람을 도울 수 있기를 바란다.

안부.

+0

방금 ​​배우는 경우 매우 복잡한 시각 효과로 시작하면 안됩니다. – holex

+0

@ 홀렉스, 귀하의 의견에 감사드립니다. 실제로 이것은 매우 복잡한 시각 효과가 아닙니다. 그러나 지금 나는 이미 그것을했다. – user1858725

+0

@hemang, 감사합니다. – user1858725

답변

0

그림을 코드로 바꾸는 앱인 PaintCode을 사용해보세요. 그것은 당신이하려고하는 종류의 일에 완벽합니다. Swift (또는 Objective-C)로 내보낼 수 있습니다.

Here은 PaintCode를 설명하는 가장 좋은 비디오입니다. PaintCode는 무료로 시도 할 수 있습니다 (저장된 문서를 열 수 없으므로 상당히 제한적 임).

편집

11 분에서 만든이 코드, 46초 :

![func drawSlider(#value: CGFloat, textValue: String, leftText: String, rightText: String) { 
    //// General Declarations 
    let context = UIGraphicsGetCurrentContext() 

    //// Color Declarations 
    let color = UIColor(red: 0.170, green: 0.565, blue: 0.571, alpha: 1.000) 
    let color2 = UIColor(red: 0.120, green: 0.635, blue: 0.521, alpha: 1.000) 

    //// Variable Declarations 
    let barWidth: CGFloat = value * 20 
    let buttonPosition: CGFloat = 20 + value * 20 
    let bubblePosition: CGFloat = value * 20 + 1 

    //// Rectangle Drawing 
    let rectanglePath = UIBezierPath(rect: CGRectMake(31, 96, 200, 4)) 
    UIColor.grayColor().setFill() 
    rectanglePath.fill() 


    //// Rectangle 2 Drawing 
    let rectangle2Path = UIBezierPath(rect: CGRectMake(31, 96, barWidth, 4)) 
    color.setFill() 
    rectangle2Path.fill() 


    //// Oval Drawing 
    var ovalPath = UIBezierPath(ovalInRect: CGRectMake(buttonPosition, 88, 20, 19)) 
    color.setFill() 
    ovalPath.fill() 


    //// Group 
    CGContextSaveGState(context) 
    CGContextTranslateCTM(context, bubblePosition, 19) 



    //// Oval 2 Drawing 
    var oval2Path = UIBezierPath() 
    oval2Path.moveToPoint(CGPointMake(35.42, 56.15)) 
    oval2Path.addCurveToPoint(CGPointMake(57, 28.5), controlPoint1: CGPointMake(47.81, 53.06), controlPoint2: CGPointMake(57, 41.85)) 
    oval2Path.addCurveToPoint(CGPointMake(28.5, 0), controlPoint1: CGPointMake(57, 12.76), controlPoint2: CGPointMake(44.24, 0)) 
    oval2Path.addCurveToPoint(CGPointMake(0, 28.5), controlPoint1: CGPointMake(12.76, 0), controlPoint2: CGPointMake(0, 12.76)) 
    oval2Path.addCurveToPoint(CGPointMake(23.94, 56.64), controlPoint1: CGPointMake(0, 42.69), controlPoint2: CGPointMake(10.37, 54.45)) 
    oval2Path.addCurveToPoint(CGPointMake(29, 65), controlPoint1: CGPointMake(25.42, 56.88), controlPoint2: CGPointMake(27.45, 65)) 
    oval2Path.addCurveToPoint(CGPointMake(35.42, 56.15), controlPoint1: CGPointMake(31.39, 65), controlPoint2: CGPointMake(33.2, 56.71)) 
    color2.setFill() 
    oval2Path.fill() 
    color.setStroke() 
    oval2Path.lineWidth = 2 
    oval2Path.stroke() 


    //// Text Drawing 
    let textRect = CGRectMake(9, 10, 40, 39) 
    let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
    textStyle.alignment = NSTextAlignment.Center 

    let textFontAttributes = \[NSFontAttributeName: UIFont(name: ".HelveticaNeueDeskInterface-Regular", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle\] 

    let textTextHeight: CGFloat = NSString(string: textValue).boundingRectWithSize(CGSizeMake(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.height 
    CGContextSaveGState(context) 
    CGContextClipToRect(context, textRect); 
    NSString(string: textValue).drawInRect(CGRectMake(textRect.minX, textRect.minY + (textRect.height - textTextHeight)/2, textRect.width, textTextHeight), withAttributes: textFontAttributes) 
    CGContextRestoreGState(context) 



    CGContextRestoreGState(context) 


    //// Text 2 Drawing 
    let text2Rect = CGRectMake(19, 113, 48, 29) 
    let text2Style = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
    text2Style.alignment = NSTextAlignment.Center 

    let text2FontAttributes = \[NSFontAttributeName: UIFont(name: ".HelveticaNeueDeskInterface-Regular", size: 14)!, NSForegroundColorAttributeName: UIColor.grayColor(), NSParagraphStyleAttributeName: text2Style\] 

    let text2TextHeight: CGFloat = NSString(string: leftText).boundingRectWithSize(CGSizeMake(text2Rect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: text2FontAttributes, context: nil).size.height 
    CGContextSaveGState(context) 
    CGContextClipToRect(context, text2Rect); 
    NSString(string: leftText).drawInRect(CGRectMake(text2Rect.minX, text2Rect.minY + (text2Rect.height - text2TextHeight)/2, text2Rect.width, text2TextHeight), withAttributes: text2FontAttributes) 
    CGContextRestoreGState(context) 


    //// Text 3 Drawing 
    let text3Rect = CGRectMake(200, 113, 48, 29) 
    let text3Style = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
    text3Style.alignment = NSTextAlignment.Center 

    let text3FontAttributes = \[NSFontAttributeName: UIFont(name: ".HelveticaNeueDeskInterface-Regular", size: 14)!, NSForegroundColorAttributeName: UIColor.grayColor(), NSParagraphStyleAttributeName: text3Style\] 

    let text3TextHeight: CGFloat = NSString(string: rightText).boundingRectWithSize(CGSizeMake(text3Rect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: text3FontAttributes, context: nil).size.height 
    CGContextSaveGState(context) 
    CGContextClipToRect(context, text3Rect); 
    NSString(string: rightText).drawInRect(CGRectMake(text3Rect.minX, text3Rect.minY + (text3Rect.height - text3TextHeight)/2, text3Rect.width, text3TextHeight), withAttributes: text3FontAttributes) 
    CGContextRestoreGState(context) 
}] 

이 코드는 완전히 안된 ...

Preview in PaintCode

는 네 개의 매개 변수가 있습니다;

  1. 슬라이더의 값 (기대 0-10)
  2. 기포
  3. 왼쪽 텍스트로 넣을 텍스트 값
  4. 오른쪽 텍스트

코드 제한 - 고정 폭 (200px)입니다. 그러나 이것은 바뀔 수 있습니다.

다음 단계 :

  1. 이 속성을 그리기 방법
  2. 사용자 정의 추가에서 4 개 개의 변수를
  3. 전화 위의 코드를 표현하기 위해 만들기에 UIView를 확장하여 사용자 지정보기 만들기 귀하의 UIViewController 또는 무엇이든보기

H 참고 용으로 PaintCode file입니다.

+0

paincode는 훌륭하지만 자습서를 본 후에는 여전히 혼란 스럽습니다. – user1858725

+0

나는 이런 종류의 프로그램 (Photoshop 등)으로 * 놀고 싶은 사람이다. PainCode를 "얻으려면"어느 정도 시간이 걸렸습니다. 튜토리얼이 최선이 아니라고 동의합니다. [이 동영상] (https://www.youtube.com/watch?v=tZVrc8Y67AI)을 본 적이 있는지 궁금합니다. –

+0

어쩌면 그것은 어리석은 것처럼 보입니다. 나는 내 자신의 질문에 대한 아이디어를 가지고 있습니다. 나는 질문 하단에 편집 한 내용을 넣었습니다. 감사합니다 린든. – user1858725