2016-05-07 4 views
0

私は現在、あなたが左にスワイプしたときにランダムな単語を生成する各半分で電話画面の2つの半分をスワイプできるアプリケーションに取り組んでいます。どのように1つの画面に2つのランダムな単語ジェネレータを持っていますか?

ランダムに単語を生成する画面の上半分の単語リストを作成しました。画面の下半分に同じ方法を適用するにはどうすればよいですか?私は現在、ビューコントローラ

import UIKit 

class ViewController: UIViewController { 
    // put the labels and the buttons 
    @IBOutlet var InspirationalThought: UILabel! 
    @IBOutlet var Click: UIButton! 

    //what the label is going to show 

    var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle",  "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion","Ancient Greek", "Philosophers", "Fairy tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air", "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior","Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"] 

    var whichQuotestoChoose = 0 

    // what the button is going to show 
    var ButtonText = ["Inspiration", "Tell me more", "more inspirational"] 
    var whichButtonTexttoChoose = 0 

    @IBAction func ClickButtonClicked(sender: UIButton) { 
     chooseAQuote() 
     chooseTextonButton() 
    } 

    func chooseAQuote(){ 
     showTheQuote() 
     whichQuotestoChoose = Int(arc4random_uniform (84)) 
    } 

    func showTheQuote(){ 
     InspirationalThought.text = "\(quotes[whichQuotestoChoose])" 
    } 

    func chooseTextonButton(){ 
     Click.setTitle("\(ButtonText[whichButtonTexttoChoose])", forState:UIControlState.Normal) 
    } 
} 

enter image description here

enter image description here

+0

もそうandhasコードがそのために追加され、あなたが答えを探している画面:以下

はサンプル実装ですか?それは一度だけですかVC –

+0

最初の画面は私が現在持っているもので、コードはどこから来たのですか?はい、その唯一のVCです。第2の画面は、私もそれがストーリーボードのスクリーンショットのように見えるようにしたいです。 –

答えて

1

にこれは、あなたがそれを行うことができる方法である持っているコードの2枚の画像を持っています。 背景が同じで、天と地の色が常に星と同じ場合は、イメージとして取得します。

はImageViewのを追加し、上部と下部のビューとして2のUIViewのVIEW1とVIEW2を追加した背景画像

として画像を設定します。そこに背景色をclearColorとして設定し、タグをそれぞれ1と2に設定します。

両方のスワイプジェスチャーに同じセレクターを追加します。

それぞれにlabel1とlabel2を表示するUILabelを追加します。

スワップ時にテキストを引用符で囲む文字列を作成します。ビューのタグに基づいて、ラベルテキストを適切に設定します。

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var view1: UIView! 
@IBOutlet weak var view2: UIView! 
@IBOutlet weak var label1: UILabel! 
@IBOutlet weak var label2: UILabel! 

var displayString: String? 
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle",  "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion","Ancient Greek", "Philosophers", "Fairy tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air", "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior","Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"] 

var whichQuotestoChoose = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.viewSwipped(_:))) 
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    view1.addGestureRecognizer(swipeLeft) 

    let swipeLeft1 = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.viewSwipped(_:))) 
    swipeLeft1.direction = UISwipeGestureRecognizerDirection.Left 
    view2.addGestureRecognizer(swipeLeft1) 
} 

func viewSwipped(gesture: UIGestureRecognizer) { 
    self.chooseAQuote() 
    if let swippedView = gesture.view { 
     if swippedView.tag == 1 { 
      label1.leftToRightAnimation() 
      label1.text = displayString 
     } else { 
      label2.leftToRightAnimation() 
      label2.text = displayString 
     } 
    } 
} 

func chooseAQuote() { 
    displayString = quotes[whichQuotestoChoose] 
    whichQuotestoChoose = Int(arc4random_uniform (84)) 
} 

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


} 

extension UIView { 
    func leftToRightAnimation(duration: NSTimeInterval = 0.5, completionDelegate: AnyObject? = nil) { 
    // Create a CATransition object 
    let leftToRightTransition = CATransition() 

    // Set its callback delegate to the completionDelegate that was provided 
    if let delegate: AnyObject = completionDelegate { 
     leftToRightTransition.delegate = delegate 
    } 

    leftToRightTransition.type = kCATransitionPush 
    leftToRightTransition.subtype = kCATransitionFromRight 
    leftToRightTransition.duration = duration 
    leftToRightTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
    leftToRightTransition.fillMode = kCAFillModeRemoved 

    // Add the animation to the View's layer 
    self.layer.addAnimation(leftToRightTransition, forKey: "leftToRightTransition") 
    } 
} 

enter image description here

+0

ありがとうございます。上記の手順では、単語を左にスワイプしてから、ジェネレータの別のランダムな単語に置き換えることができます。ビューコントローラの元のコードの下にコードを実装していますか?画面の下半分の単語の2番目のリストのコードにどのように配置しますか? –

+0

いいえ、ラベルのテキストを変更するだけです。あなたがスワイプの言葉が変更されるのでどこでもスワイプはバイトンに設定されます。アニメーションを追加してその機能を追加することができます。 –

+0

ここで、コード例を自分のコードに配置しますか?どのようにして単語の最下位リストをコーディングするのですか? –

関連する問題