2017-11-24 9 views
-2

スライダの初期値(例:slider.value = 5)を与えてから移動していると、アニメーションが壊れてしまい、「親指」が現在の位置と位置の間でジャンプしているようです値5(しかし、私がそれを "スライド"しているときだけ)。私が最初のポジションを与えていないときは問題ありません。あなたは同じ/同様の問題に遭遇しましたか?SwiftのSliderの壊れたアニメーション

私のコードはここにある:

import UIKit 
import AVFoundation 

class Playercontroller: UIViewController { 

    // MARK: - User Interface Properties 

    var artworkView = UIImageView(image: #imageLiteral(resourceName: "rivuletInSpring")) 
    let infoLabel = UILabel() 
    let timeLabel = UILabel() 
    let startButton = UIButton(type: .system) 
    let stopButton = UIButton(type: .system) 
    let slider = UISlider() 
    var player: AVAudioPlayer = AVAudioPlayer() 
    var timer = Timer() 
    var time: Int = 5 
    var navigationTitle = "Rivulet in the Spring" 
    var soundName = "rivuletInSpringMelody" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.backgroundColor = .black 
     navigationItem.title = "\(navigationTitle)" // set to a value based on a pressed button 

     do 
     { 
      let audioPath = Bundle.main.path(forResource: "\(soundName)", ofType: "mp3") 
      try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
     } 
     catch 
     { 
      // finish! probably use a notification 
     } 

    } 

    override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 

     infoLabel.text = "Duration in mins: " 
     infoLabel.textColor = .white 
     infoLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 25.0) 

     timeLabel.text = String(Int(slider.value)) 
     timeLabel.textColor = .white 
     timeLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 25.0) 

     slider.maximumValue = 60 
     slider.minimumValue = 1 
     //slider.value = 5 
     slider.addTarget(self, action:#selector(sliderValueChanged), for: .valueChanged) 

     startButton.backgroundColor = .black 
     startButton.setTitle("Play", for: .normal) 
     startButton.tintColor = .orange 
     startButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 30.0) 
     startButton.layer.borderColor = UIColor.orange.cgColor 
     startButton.layer.borderWidth = 1 
     startButton.addTarget(self, action: #selector(play), for: .touchUpInside) 

     stopButton.backgroundColor = .black 
     stopButton.setTitle("Stop", for: .normal) 
     stopButton.tintColor = .orange 
     stopButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 30.0) 
     stopButton.layer.borderColor = UIColor.orange.cgColor 
     stopButton.layer.borderWidth = 1 
     stopButton.addTarget(self, action: #selector(stop), for: .touchUpInside) 

     view.addSubview(artworkView) 
     artworkView.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(infoLabel) 
     infoLabel.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(timeLabel) 
     timeLabel.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(slider) 
     slider.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(startButton) 
     startButton.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(stopButton) 
     stopButton.translatesAutoresizingMaskIntoConstraints = false 

     NSLayoutConstraint.activate([ 
      artworkView.widthAnchor.constraint(equalToConstant: 414), 
      artworkView.heightAnchor.constraint(equalTo: artworkView.widthAnchor, multiplier: 170.0/414.0), 
      artworkView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 0.0), 
      artworkView.leftAnchor.constraint(equalTo: view.leftAnchor), 
      artworkView.rightAnchor.constraint(equalTo: view.rightAnchor), 
      infoLabel.topAnchor.constraint(equalTo: artworkView.bottomAnchor, constant: 100.0), 
      infoLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -11.0), 
      timeLabel.topAnchor.constraint(equalTo: artworkView.bottomAnchor, constant: 100.0), 
      timeLabel.leadingAnchor.constraint(equalTo: infoLabel.trailingAnchor, constant: 7.0), 
      slider.topAnchor.constraint(equalTo: infoLabel.bottomAnchor, constant: 30.0), 
      slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40.0), 
      slider.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40.0), 
      startButton.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant: -20.0), 
      startButton.topAnchor.constraint(equalTo: slider.bottomAnchor, constant: 30.0), 
      startButton.widthAnchor.constraint(equalToConstant: 100.0), 
      startButton.heightAnchor.constraint(equalToConstant: 50.0), 
      stopButton.topAnchor.constraint(equalTo: slider.bottomAnchor, constant: 30.0), 
      stopButton.leadingAnchor.constraint(equalTo: view.centerXAnchor, constant: 20), 
      stopButton.widthAnchor.constraint(equalToConstant: 100.0), 
      stopButton.heightAnchor.constraint(equalToConstant: 50.0), 
      ]) 

    } 

    func sliderValueChanged(){ 
     timeLabel.text = String(Int(slider.value)) 
    } 

} 
+0

*オンサイトの*関連コード*外部サイトではありません – luk2302

答えて

1

あなたはviewWillLayoutSubviewsslider初期値を設定しています。その関数は、スライダの親指をドラッグしている間も含めて、何千回も実行されます。スライダーに初期値を与えたい場合は、オーバーライドの内部でviewDidLoadを選択するのが良いでしょう。実際に

、あなたはviewWillLayoutSubviewsを持っているコードのすべてがviewDidLoadまたはviewDidLoadによって呼び出されsetup関数に移動する必要があります。サブビューをviewWillLayoutSubviewsに追加することは望ましくありません。

+0

申し訳ありませんが、私もそれを試してみましたが、それは当時は機能しませんでした。しかし、初めて私はviewDidLoadでminとmaxを設定していました。ありがとう!私が尋ねることができる場合、なぜviewDidLoadにサブビューを追加する方が良いでしょうか? viewWillLayoutSubviewsは何千回も実行されるためですか? – 1johnnytheboy

+0

私の前のコメントをもう編集できないので、私は私の質問[ここ](https://stackoverflow.com/questions/13466189/should-i-initialize-my)への回答を見つけたと言いたいだけです。 -viewcontroller-on-viewdidload-or-viewwilllayoutsubviews) – 1johnnytheboy

関連する問題