我的代码在同一个接口控制器上随机生成2个单独的图像。我需要检查图像是否相互匹配,但我不确定如何随机生成它们。我曾尝试写过如下语句:
if blueColour.setBackgroundImageNamed("colour\(randomImage).jpg") == mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg") {
println("they match")
} else {
println("they dont match")
}
但它似乎不起作用,我得到一个错误说"二元运算符' =='不能应用于两个Void操作数"
我的代码如下:
@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!
@IBOutlet var mainBackgroundColour: WKInterfaceGroup!
@IBOutlet var scoreLabel: WKInterfaceLabel!
var randomImage = UInt32()
var randomMainBackground = UInt32()
@IBAction func onePressedTest() {
if blueColour.setBackgroundImageNamed("colour\(randomImage).jpg") == mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg") {
println("they match")
} else {
println("they dont match")
}
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")
randomMainBackground = arc4random_uniform(4)
mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg")
}
新修订的代码2015年4月20日:
import WatchKit
import Foundation
protocol WKInterfaceComparableImage {
func getImage()->UIImage;
func equalsImage(comparableWKObject:WKInterfaceComparableImage)->Bool;
}
class WKInterfaceButtonComparable : WKInterfaceButton, WKInterfaceComparableImage {
private var image:UIImage?;
override func setBackgroundImage(image: UIImage?) {
self.image = image;
super.setBackgroundImage(image);
}
func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
return self.image === comparableWKObject.getImage();
}
func getImage() -> UIImage {
return image!;
}
}
class WKInterfaceGroupComparable : WKInterfaceButton, WKInterfaceComparableImage {
private var image:UIImage?;
override func setBackgroundImage(image: UIImage?) {
self.image = image;
super.setBackgroundImage(image);
}
func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
return self.image === comparableWKObject.getImage();
}
func getImage() -> UIImage {
return image!;
}
}
class ImageProvide {
private let MAX_RANDOM_NUMBER:UInt32 = 4
static let shared:ImageProvide = ImageProvide();
var images:[UIImage];
private init() {
images = [];
for i in 1...MAX_RANDOM_NUMBER {
//get image with the best way to you
images.append(UIImage(named: "colour\(i).jpg")!);
}
}
func getRandomImage()->UIImage {
let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER);
return getImage(id: Int(randomImage));
}
func getImage(#id:Int)->UIImage {
return images[id];
}
}
class InterfaceController: WKInterfaceController {
@IBOutlet var blueColour: WKInterfaceButtonComparable!
@IBOutlet var pinkColour: WKInterfaceButtonComparable!
@IBOutlet var greenColour: WKInterfaceButtonComparable!
@IBOutlet var yellowColour: WKInterfaceButtonComparable!
@IBOutlet var mainBackgroundColour: WKInterfaceGroupComparable!
@IBOutlet var scoreLabel: WKInterfaceLabel!
var randomImage = UInt32()
var randomMainBackground = UInt32()
var score:Int = 1
@IBAction func onePressedTest() {
if blueColour.equalsImage(mainBackgroundColour) || pinkColour.equalsImage(mainBackgroundColour) || greenColour.equalsImage(mainBackgroundColour) || yellowColour.equalsImage(mainBackgroundColour) {
println("they match")
scoreLabel.setText("\(score)")
score++
} else {
println("they dont match")
}
blueColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
pinkColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
greenColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
yellowColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
mainBackgroundColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
答案 0 :(得分:0)
我做了一个例子,我认为可以帮助您找到解决问题的方法,我创建了具有相似功能的课程自定义来帮助您的工作。我做得非常快,我没有办法为你测试,但你可以看到这个概念来帮助你创建一个解决方案。我希望通过我的榜样帮助你。
ComparableImage
是比较的关键,这个协议可以完成你的任务。
WKInterfaceButtonComparable
扩展WKInterfaceButton
,您可以保存当前图片进行比较,之后我对WKInterfaceGroupComparable
做了同样的事情。
ImageProvide
很重要,因为这会管理您的图片,您需要编写关于图像的逻辑代码,并使用此Singleton您可以预加载图像并发送到您的WK组件。我希望它也有帮助。
protocol WKInterfaceComparableImage {
func getImage()->UIImage;
func equalsImage(comparableWKObject:WKInterfaceComparableImage)->Bool;
}
class WKInterfaceButtonComparable : WKInterfaceButton, WKInterfaceComparableImage {
private var image:UIImage?;
override func setBackgroundImage(image: UIImage?) {
self.image = image;
super.setBackgroundImage(image);
}
func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
return self.image === comparableWKObject.getImage();
}
func getImage() -> UIImage {
return image!;
}
}
class WKInterfaceGroupComparable : WKInterfaceButton, WKInterfaceComparableImage {
private var image:UIImage?;
override func setBackgroundImage(image: UIImage?) {
self.image = image;
super.setBackgroundImage(image);
}
func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
return self.image === comparableWKObject.getImage();
}
func getImage() -> UIImage {
return image!;
}
}
class ImageProvide {
private let MAX_RANDOM_NUMBER:UInt32 = 4
static let shared:ImageProvide = ImageProvide();
var images:[UIImage];
private init() {
images = [];
for i in 1...MAX_RANDOM_NUMBER {
//get image with the best way to you
images.append(UIImage(named: "colour\(i).jpg")!);
}
}
func getRandomImage()->UIImage {
let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER);
return getImage(id: Int(randomImage));
}
func getImage(#id:Int)->UIImage {
return images[id];
}
}
class InterfaceController: WKInterfaceController {
@IBOutlet var blueColour: WKInterfaceButtonComparable!
@IBOutlet var pinkColour: WKInterfaceButtonComparable!
@IBOutlet var greenColour: WKInterfaceButtonComparable!
@IBOutlet var yellowColour: WKInterfaceButtonComparable!
@IBOutlet var mainBackgroundColour: WKInterfaceGroupComparable!
@IBOutlet var scoreLabel: WKInterfaceLabel!
var randomImage = UInt32()
var randomMainBackground = UInt32()
var score:Int = 1
@IBAction func onePressedTest() {
if blueColour.equalsImage(mainBackgroundColour) {
println("they match")
} else {
println("they dont match")
}
blueColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
pinkColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
greenColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
yellowColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
mainBackgroundColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
}
}