gpt4 book ai didi

swift - 如何检查 2 张图像是否相互匹配 - Swift Apple Watch

转载 作者:行者123 更新时间:2023-11-28 09:09:39 25 4
gpt4 key购买 nike

我的代码在同一个界面 Controller 上随机生成 2 个单独的图像。我需要检查图像是否相互匹配,但我不确定如何去做,因为它们是随机生成的。我试过编写 if 语句,例如:

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

最佳答案

我举了一个我认为可以帮助您找到解决问题方法的示例,我创建了具有相当能力的类自定义来帮助您的工作。我做的很快,我没有办法给你测试,但你可以看到这个概念来帮助你创建一个解决方案。我希望我的示例对您有所帮助。

ComparableImage 是比较的关键,这个协议(protocol)可以完成你的任务。

WKInterfaceButtonComparable extends 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())
}
}

关于swift - 如何检查 2 张图像是否相互匹配 - Swift Apple Watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com