gpt4 book ai didi

ios - 为什么 UIImageViews 的 NSArray 遵循某种模式?

转载 作者:行者123 更新时间:2023-11-29 12:21:56 25 4
gpt4 key购买 nike

我有一个包含 4 个 UIImageViews A、A2、A3 和 A4 的数组。我希望将它们拖到另一个名为 answerA 的 imageView 上。每次我将其中一个拖到答案上时,它应该将 answerA 上的图像更改为一个数字,该数字表明有多少人被拖到上面。例如。如果我在 answerA 上拖动 A,则 answerA 图像将更改为数字 1。如果我从数组中再拖动一个 A,则它将更改为数字 2。出于某种原因,我的数组遵循一种奇怪的模式。我拖动的第一个更改为数字 1,第二个什么都不做,第三个将更改为数字 2,第四个将其更改回数字 1。无论我以何种顺序拖动它们,它都将始终采用相同的模式.

这是我的 4 个 imageViews

数组
@property (strong, nonatomic) NSArray *letterA; //initialized in .h

self.letterA = @[A, A2, A3, A4];//initialized in .m

我检查碰撞的方法:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
int intersectionCount = 0;
for (UIImageView *letter in letterA) {
if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
letter.userInteractionEnabled = NO;
letter.hidden = YES;
intersectionCount++;
}
}

if (intersectionCount == 1) {
UIImage *Pic1 = [UIImage imageNamed:@"number1.png"];
[correctCounterA setImage:Pic1];
}
else if (intersectionCount == 2) {
UIImage *Pic2 = [UIImage imageNamed:@"number2.png"];
[correctCounterA setImage:Pic2];
}
else if (intersectionCount == 3) {
UIImage *Pic3 = [UIImage imageNamed:@"number3.png"];
[correctCounterA setImage:Pic3];
}
else if (intersectionCount == 4) {
UIImage *Pic4 = [UIImage imageNamed:@"number4.png"];
[correctCounterA setImage:Pic4];
}

注意:correctCounterA 是 answerA 之上的一个小 imageView,因此只有那个小部分会将图像更改为数字。

最佳答案

您需要修改 touchesEnded 如下所示。在这里,您发现拖动图像的方式有误。这对你有帮助吗?

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

for (UIImageView *letter in letterA) {

if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
letter.userInteractionEnabled = NO;
letter.hidden = YES;
intersectionCount++;
break;
}

}

if (intersectionCount == 1) {
UIImage *Pic1 = [UIImage imageNamed:@"number1.png"];
[correctCounterA setImage:Pic1];
}
else if (intersectionCount == 2) {
UIImage *Pic2 = [UIImage imageNamed:@"number2.png"];
[correctCounterA setImage:Pic2];
}
else if (intersectionCount == 3) {
UIImage *Pic3 = [UIImage imageNamed:@"number3.png"];
[correctCounterA setImage:Pic3];
}
else if (intersectionCount == 4) {
UIImage *Pic4 = [UIImage imageNamed:@"number4.png"];
[correctCounterA setImage:Pic4];
}
}

编辑:根据您的要求,您需要在.h 文件和viewDidLoad 中声明intersectionCount ,您需要按如下方式分配。

int intersectionCount = 0;

并像上面一样更改touchesEnded

关于ios - 为什么 UIImageViews 的 NSArray 遵循某种模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368970/

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