我目前正在开发一个应用程序,你可以分别滑动手机屏幕的两部分,每一半在你离开时生成一个随机的单词。
我已经为屏幕的上半部分创建了一个随机生成单词的单词列表。如何将同样的方法应用于屏幕的下半部分?我已经得到了我目前在视图控制器中拥有的代码的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)
}
}


发布于 2016-05-08 01:20:10
这就是你能做到的。如果你的背景保持不变,顶部和底部的颜色和星星总是一样的话,那就像图像一样。
添加一个imageView并将图像设置为背景图像
添加2 UIView的view1和view2作为顶部和底部视图。设置背景色为clearColor,标签分别设置为1和2
增加滑动手势2,它们都有相同的选择器。
将UILabel添加到每个视图中,即视图、label1和label2。
当滑动时,创建一个字符串来保存引号中的文本。基于视图的标记,相应地设置标签文本。
以下是示例实现:
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")
}
}

https://stackoverflow.com/questions/37094855
复制相似问题