在 Swift Apple Watch 中按下按钮时生成新图像
Generate a new image on a button press in Swift AppleWatch
好的,我有 4 张图片,它们的命名顺序为 'colour1.jpg'、colour2.jpg、'colour3.jpg'、'colour4.jpg'。这些图像中的每一个都将充当按钮背景图像。
想法是屏幕上有 4 个按钮,每次按下其中一个按钮时,每个按钮上的图像都会随机变化。
我正在使用 arc4random_uniform 随机化图像。但目前它不起作用,如果用户按下其中一个按钮,则会选择一个随机图像,但所有 4 个按钮都是相同的。然后它永远不会从那个图像改变。 IE。如果您按下同一个按钮或另一个按钮,则什么也不会发生。它只是保留在以前的图像上。
我是 swift 的新手,非常感谢您对我的耐心等待,但我真的很想知道我哪里出错了。
class InterfaceController: WKInterfaceController {
@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!
var randomImageA1 = arc4random_uniform(4)
var randomImageA2 = arc4random_uniform(4)
var randomImageA3 = arc4random_uniform(4)
var randomImageA4 = arc4random_uniform(4)
var randomImageB1 = arc4random_uniform(4)
var randomImageB2 = arc4random_uniform(4)
var randomImageB3 = arc4random_uniform(4)
var randomImageB4 = arc4random_uniform(4)
var randomImageC1 = arc4random_uniform(4)
var randomImageC2 = arc4random_uniform(4)
var randomImageC3 = arc4random_uniform(4)
var randomImageC4 = arc4random_uniform(4)
var randomImageD1 = arc4random_uniform(4)
var randomImageD2 = arc4random_uniform(4)
var randomImageD3 = arc4random_uniform(4)
var randomImageD4 = arc4random_uniform(4)
@IBAction func onePressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageA1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageA2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageA3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageA4).jpg")
}
@IBAction func twoPressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageB1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageB2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageB3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageB4).jpg")
}
@IBAction func threePressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageC1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageC2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageC3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageC4).jpg")
}
@IBAction func fourPressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageD1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageD2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageD3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageD4).jpg")
}
您只拨打了 randomImage = arc4random_uniform(5)
一次。如果该函数为您随机选择一个图像,那么您需要在每次调用 setBackgroundImage()
之前调用它
随机化的成功还取决于您如何在 randomImage = arc4random_uniform(5)
中随机化图像。很难说在没有看到你的随机性实现的情况下是否总是 return 相同的图像。
示例:
@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!
var randomImage
@IBAction func onePressed() {
randomImage = arc4random_uniform(4)
blueColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
pinkColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
greenColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
yellowColour.setBackgroundImageNamed("colour\(randomImage).jpg")
}
好的,我有 4 张图片,它们的命名顺序为 'colour1.jpg'、colour2.jpg、'colour3.jpg'、'colour4.jpg'。这些图像中的每一个都将充当按钮背景图像。
想法是屏幕上有 4 个按钮,每次按下其中一个按钮时,每个按钮上的图像都会随机变化。
我正在使用 arc4random_uniform 随机化图像。但目前它不起作用,如果用户按下其中一个按钮,则会选择一个随机图像,但所有 4 个按钮都是相同的。然后它永远不会从那个图像改变。 IE。如果您按下同一个按钮或另一个按钮,则什么也不会发生。它只是保留在以前的图像上。
我是 swift 的新手,非常感谢您对我的耐心等待,但我真的很想知道我哪里出错了。
class InterfaceController: WKInterfaceController {
@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!
var randomImageA1 = arc4random_uniform(4)
var randomImageA2 = arc4random_uniform(4)
var randomImageA3 = arc4random_uniform(4)
var randomImageA4 = arc4random_uniform(4)
var randomImageB1 = arc4random_uniform(4)
var randomImageB2 = arc4random_uniform(4)
var randomImageB3 = arc4random_uniform(4)
var randomImageB4 = arc4random_uniform(4)
var randomImageC1 = arc4random_uniform(4)
var randomImageC2 = arc4random_uniform(4)
var randomImageC3 = arc4random_uniform(4)
var randomImageC4 = arc4random_uniform(4)
var randomImageD1 = arc4random_uniform(4)
var randomImageD2 = arc4random_uniform(4)
var randomImageD3 = arc4random_uniform(4)
var randomImageD4 = arc4random_uniform(4)
@IBAction func onePressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageA1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageA2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageA3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageA4).jpg")
}
@IBAction func twoPressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageB1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageB2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageB3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageB4).jpg")
}
@IBAction func threePressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageC1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageC2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageC3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageC4).jpg")
}
@IBAction func fourPressed() {
blueColour.setBackgroundImageNamed("colour\(randomImageD1).jpg")
pinkColour.setBackgroundImageNamed("colour\(randomImageD2).jpg")
greenColour.setBackgroundImageNamed("colour\(randomImageD3).jpg")
yellowColour.setBackgroundImageNamed("colour\(randomImageD4).jpg")
}
您只拨打了 randomImage = arc4random_uniform(5)
一次。如果该函数为您随机选择一个图像,那么您需要在每次调用 setBackgroundImage()
随机化的成功还取决于您如何在 randomImage = arc4random_uniform(5)
中随机化图像。很难说在没有看到你的随机性实现的情况下是否总是 return 相同的图像。
示例:
@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!
var randomImage
@IBAction func onePressed() {
randomImage = arc4random_uniform(4)
blueColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
pinkColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
greenColour.setBackgroundImageNamed("colour\(randomImage).jpg")
randomImage = arc4random_uniform(4)
yellowColour.setBackgroundImageNamed("colour\(randomImage).jpg")
}