2017-01-19 3 views
0

xcodeプロジェクトで新しいビューを作成しましたが、別のviewControllerからそのビューにボタンをリンクすることはできません。どのように私はそれをリンクすることができますか? ScrollViewを使用していて、いくつかのView Controllerが連携しています。scrollViewを使用しているときに、新しいViewControllerにボタンをアタッチするには

viewcontroller3 showing button

私は非常に.storyboardファイルに一緒にあなたの意見のすべてを保つ推薦viewcontroller3

import UIKit 
import AVFoundation 

enum MeditationState { 
    case on 
    case off 
} 

class ViewController3: UIViewController { 

    var player:AVAudioPlayer = AVAudioPlayer() 
    var player1:AVAudioPlayer = AVAudioPlayer() 
    var meditationState: MeditationState? 
    var replicatorLayer = CAReplicatorLayer() 
    var dot = CALayer() 

    func updateTimer(){ 

     seconds += 1 
     timerclock.text = "\(seconds)" 
    } 

    // Animation starts running 

    func animation2() { 

     // A layer that creates a specified number of copies of its sublayers (the source layer), each copy potentially having geometric, temporal, and color transformations applied to it. 
     replicatorLayer = CAReplicatorLayer() 

     // The layer’s bounds rectangle. Animatable. 
     replicatorLayer.bounds = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0) 

     // The radius to use when drawing rounded corners for the layer’s background. Animatable. 
     replicatorLayer.cornerRadius = 10.0 

     // The background color of the receiver. Animatable. 
     replicatorLayer.backgroundColor = UIColor(white: 0.0, alpha: 0.0).cgColor 

     // The layer’s position in its superlayer’s coordinate space. Animatable. 
     replicatorLayer.position = view.center 

     // calling this method creates an array for that property and adds the specified layer to it. 
     view.layer.addSublayer(replicatorLayer) 


     // connectng the animation to the content 

     // An object that manages image-based content and allows you to perform animations on that content 
     dot = CALayer() 

     // The layer’s bounds rectangle. Animatable. 
     dot.bounds = CGRect(x: 0.0, y: 0.0, width: 12.0, height: 12.0) 

     //The layer’s position in its superlayer’s coordinate space. Animatable. 
     dot.position = CGPoint(x: 150.0, y: 40.0) 

     //The background color of the receiver. Animatable. 
     dot.backgroundColor = UIColor(white: 0.2, alpha: 1.0).cgColor 

     // The color of the layer’s border. Animatable. 
     dot.borderColor = UIColor(white: 1.0, alpha: 1.0).cgColor 

     // The width of the layer’s border. Animatable. 
     dot.borderWidth = 1.0 

     //The radius to use when drawing rounded corners for the layer’s background. Animatable. 
     dot.cornerRadius = 5.0 


     //Appends the layer to the layer’s list of sublayers. 
     replicatorLayer.addSublayer(dot) 

     // number of copies of layer is instanceCount 

     let nrDots: Int = 1000 

     //The number of copies to create, including the source layers. 
     replicatorLayer.instanceCount = nrDots 

     // The basic type for floating-point scalar values in Core Graphics and related frameworks. 
     let angle = CGFloat(2*M_PI)/CGFloat(nrDots) 

     // The transform matrix applied to the previous instance to produce the current instance. Animatable. 
     replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0.0, 0.0, 1.0) 

     // Type used to represent elapsed time in seconds. 
     let duration: CFTimeInterval = 10.0 

     // animation capabilities for a layer property. 

     // An object that provides basic, single-keyframe animation capabilities for a layer property. 
     let shrink = CABasicAnimation(keyPath: "transform.scale") 

     // Defines the value the receiver uses to start interpolation. 
     shrink.fromValue = 1.0 

     // Defines the value the receiver uses to end interpolation. 
     shrink.toValue = 0.1 

     // Specifies the basic duration of the animation, in seconds. 
     shrink.duration = duration 

     // Determines the number of times the animation will repeat. 
     shrink.repeatCount = Float.infinity 

     // Add the specified animation object to the layer’s render tree. 
     dot.add(shrink, forKey: "shrink") 

     // Specifies the delay, in seconds, between replicated copies. Animatable. 
     replicatorLayer.instanceDelay = duration/Double(nrDots) 

     // The transform applied to the layer’s contents. Animatable. 
     dot.transform = CATransform3DMakeScale(0.01, 0.01, 0.01) 
    } 

    // connecting the breathe in label 

    @IBOutlet weak var label: UILabel! 

    // instant delay 

    @IBOutlet weak var instantDelay: UIButton! 
    @IBAction func delayBtn(_ sender: Any) { 

     dot.removeAnimation(forKey: "shrink") 
     timer1.invalidate() 
     seconds = 0 
     timer2.invalidate() 
     timerclock.text = "\(seconds)" 
     time = 0 
     timerLabel.text = "Breathe in" 
     timerisOn = false 
     pauseBtn.isHidden = true 
     playBtn.isHidden = false 

     label.isHidden = true 
     replicatorLayer.isHidden = true 
     instantDelay.isHidden = true 
     instantDelay1.isHidden = false 
     slider.isHidden = false 
    } 

    // Delay 1 

    @IBOutlet weak var instantDelay1: UIButton! 
    @IBAction func delayBtn1(_ sender: Any) { 


     instantDelay1.isHidden = true 
     instantDelay.isHidden = false 
     label.isHidden = false 
     slider.isHidden = true 
    } 


    //Slider for changing animation speed 

    @IBOutlet weak var slider: UISlider! 
    @IBAction func slider(_ sender: Any) { 

    } 

    @IBAction func speed(_ sender: UISlider) { 
     view.layer.speed = sender.value 

    } 



    //Sound On button 

    @IBOutlet weak var soundOn: UIButton! 
    @IBAction func SoundOn(_ sender: Any) { 
     meditationState = .on 
    setTrackForPlayerWith(trackName: "Mute") 
     player.play() 
     soundoff.isHidden = false 
     soundOn.isHidden = true 
    } 

    //Sound Off button 

    @IBOutlet weak var soundoff: UIButton! 
    @IBAction func SoundOff(_ sender: Any) { 
     meditationState = .off 
     setTrackForPlayerWith(trackName: "Bigsur") 
     player.play() 
     soundoff.isHidden = true 
     soundOn.isHidden = false 
    } 

    //Timerclock at top of screen label 

    @IBOutlet weak var timerclock: UILabel! 

    // creating vars to set things 

    var animation = CFTimeInterval() 
    var timer1 = Timer() 
    var timer2 = Timer() 
    var time = 0 
    var seconds = 0 
    var timerisOn = false 

    // connecting breathe in label 

    @IBOutlet var question: UILabel! 

    var arrayOfStrings: [String] = [""] 

    // connecting timerclick and starting it 
    @IBOutlet var timerLabel: UILabel! 

    // changes the amount of time on the label of different labels 
    func increaseTimer() { 

     time += 1 

     switch time { 
     case 0 ... 7: 
      timerLabel.text = "Hold" 
     case 8 ... 10: 
      timerLabel.text = "Breathe Out" 
     case 11 ... 12: 
      timerLabel.text = "Breathe in" 
     default: 
      time = 0 
     } 

    } 

    // connecting the play button and vars 
    @IBOutlet weak var playBtn: UIButton! 
    @IBAction func play(sender: AnyObject) { 

     bell(trackName: "Bell") 
     player1.play() 

     timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController3.increaseTimer), userInfo: nil, repeats: true) 
     pauseBtn.isHidden = false 
     playBtn.isHidden = true 
     if timerisOn == false { 


     timer2 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true) 
      timerisOn = true 
     } 
     animation2() 
    } 


    // pausing the timer with the vars 

    @IBOutlet weak var pauseBtn: UIButton! 
    @IBAction func pause(sender: AnyObject) { 
     dot.removeAnimation(forKey: "shrink") 
     timer1.invalidate() 
     seconds = 0 
     timer2.invalidate() 
     timerclock.text = "\(seconds)" 
     time = 0 
     timerLabel.text = "Breathe in" 
     timerisOn = false 
     pauseBtn.isHidden = true 
     playBtn.isHidden = false 

     } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     meditationState = .on 
     setTrackForPlayerWith(trackName: "Bigsur") 
     player.play() 
     player.numberOfLoops = -1 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    time += 1 
     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient) 
      print("AVAudioSession Category Playback OK") 
      do { 
       try AVAudioSession.sharedInstance().setActive(true) 
       print("AVAudioSession is Active") 
      } catch let error as NSError { 
       print(error.localizedDescription) 
      } 
     } catch let error as NSError { 
      print(error.localizedDescription) 
     } 

    } 

     func setTrackForPlayerWith(trackName: String) { 
      do 
      { 
       let audioPath = Bundle.main.path(forResource: trackName, ofType: "mp3") 
       try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
      } 
      catch 
      { 
       //ERROR 
      } 
     } 


    func bell(trackName: String) { 
     do 
     { 
      let audioPath = Bundle.main.path(forResource: trackName, ofType: "mp3") 
      try player1 = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
     } 
     catch 
     { 
      //ERROR 
     } 
    } 

     override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



} 
+2

コードを投稿するか、ストーリーボードを示すスクリーンショットを少なくとも投稿する必要があります。あなたの現在の質問から、より多くの情報なしであなたを助けることは不可能です – Ollie

+0

私はスクリーンショットを追加しました。 –

答えて

0

からコードを、あなたは簡単に右クリックして、ボタンから直接ドラッグすることもでき、そのようビューに表示し、約2秒で接続します。別々の.xibファイルでそれらを接続するには、@IBActionボタンを押した関数を使用し、その中のxibからloadビューを使用する必要があります。

Good Luck!

+0

助けてくれてありがとう、私はそれを行く。 –

+0

ごめんなさい。 @IBActionボタンが押されたボタン内のxibからロードビューを使用するにはどうすればよいですか? –

+0

これは役立つかもしれません:http://stackoverflow.com/questions/24857986/load-a-uiview-from-nib-in-swift#26326006 – PyPiePi

関連する問題