gpt4 book ai didi

objective-c - 根据标签设置 UIImage

转载 作者:行者123 更新时间:2023-12-01 18:31:05 26 4
gpt4 key购买 nike

我正在编写一个循环遍历 LED UIImageView 数组的脚本,这些 LED UIImageView 以数字顺序设置并由标签选择。根据步骤(又名数字),它在 LED 图像上显示为打开或关闭。此方法的主要目标是执行当前步骤并显示“LED 开启”图像,同时将其减一并显示上一步的“LED 关闭”图像。因此,一次只能点亮一个 LED。

不幸的是,我只能让“LED on”图像正确显示。序列中的所有 LED 都会亮起,但它们永远不会熄灭。我的第一个猜测是我没有以正确的方式减去 NSInterger。但是,当我检查日志时,一切都在它应该在的地方。如果当前步骤为 2,则上一步为 1。不知道为什么这不起作用。谢谢!

sequencerLocation 和 previousLocation 都设置为属性。

- (void)clockLoop:(UInt8)seqClockPulse
{
//cast to an int to use in loop
NSInteger stepCount = sequencerSteps;

//increment sequencer on pulse in
sequencerLocation++;

if(sequencerLocation > stepCount)
{
sequencerLocation = 1;
}

//setup previous step location
previousLocation = (sequencerLocation - 1);

if (previousLocation == 0)
{
previousLocation = stepCount;
}

//change led color in led array
for (UIImageView *led in sequencerLEDArray)
{
if(led.tag == sequencerLocation)
{
UIImageView *previousLed = (UIImageView *)[led viewWithTag:previousLocation];
[previousLed setImage:[UIImage imageNamed:@"images/seq_LED_off.png"]];
NSLog(@"Previous: %d", previousLocation);

UIImageView *currentLed = (UIImageView *)[led viewWithTag:sequencerLocation];
[currentLed setImage:[UIImage imageNamed:@"images/seq_LED_on.png"]];
NSLog(@"Current: %d", sequencerLocation);
}
}

}

最佳答案

//change led color in led array
for (UIImageView *led in sequencerLEDArray)
{
if(led.tag == sequencerLocation)
{
// I THINK the problem is here
// UIImageView *previousLed = (UIImageView *)[led viewWithTag:previousLocation];
// TRY THIS instead
UIImageView *previousLed = [led.superview viewWithTag:previousLocation];
[previousLed setImage:[UIImage imageNamed:@"images/seq_LED_off.png"]];
NSLog(@"Previous: %d", previousLocation);

// HERE you don't need to search for the tag you already tested for it in your if statement
UIImageView *currentLed = (UIImageView *)[led viewWithTag:sequencerLocation];
[currentLed setImage:[UIImage imageNamed:@"images/seq_LED_on.png"]];
NSLog(@"Current: %d", sequencerLocation);
}
}

viewWithTag:
Discussion

This method searches the current view and all of its subviews for the specified view.



所以当你搜索 led通过它自己的标签,它正在返回自己,但是当你搜索它的兄弟时它没有找到它,这就是我建议 led.superview 的原因作为搜索标签的地方, parent 应该能够找到它的另一个 child 。

关于objective-c - 根据标签设置 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8737097/

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