您如何在一个屏幕上有两个随机单词生成器 [英] How do you have two random word generators on one screen

查看:135
本文介绍了您如何在一个屏幕上有两个随机单词生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,在该应用程序中,您可以分别滑动手机的两半,向左滑动时,每一半都会产生一个随机单词.

I am currently working on an app where you can swipe two halves of the phone screen separately with each half generating a random word when you swipe left.

我为屏幕的上半部分创建了一个单词列表,这些单词列表随机生成单词.如何在屏幕的下半部分应用相同的方法?我在视图控制器中有2张当前代码的图像

I have created a list of words for the top half of the screen that randomly generate words. How would I apply this same method for the bottom half of the screen? I have got 2 images of the code that I currently have in view controller

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)
    }
}

推荐答案

这就是您的操作方法. 如果您的背景保持不变,并且顶部和底部的颜色始终与星形相同,则将其作为图像获取.

This is how you can do it. If your background remains the same, with top and bottom color always same with star then get it as a image.

添加一个imageView并将其设置为背景图像

Add a imageView and set the image as background image

添加2个UIView的view1和view2作为顶视图和底视图.在那里设置背景色为clearColor并将标签分别设置为1和2

Add 2 UIView's view1 and view2 as top and bottom view. Set there background color as clearColor and tag as 1 and 2 respectively

使用相同的选择器添加两个手势2.

Add swipe gesture 2 both of them with same selector.

将UILabel添加到每个视图标签1和标签2.

Add UILabel to each namely view label1 and label2.

创建一个字符串,以便在滑动时保留引号中的文本.根据视图的标签,相应地设置标签文本.

Create a string to hold the text from quotes when swipped. Based on the tag of the view, set the label text accordingly.

下面是示例实现:

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")
    }
}

这篇关于您如何在一个屏幕上有两个随机单词生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆