我正在处理一个脚本,该脚本循环遍历一系列LED UIImageView,它们按数字顺序设置并由标签选择。根据步骤(又名号码),led图像上的显示为开或关。此方法的主要目标是采用当前步骤并在"上显示" LED。将图像减去1并显示" LED熄灭"上一步的图像。因此,一次只能点亮一个LED。
不幸的是,我只能打开" LED?图像显示正确。序列中的所有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);
}
}
}
答案 0 :(得分:4)
//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
此方法搜索指定视图的当前视图及其所有子视图。
所以,当你从它自己的标签中搜索led
时,它会自行返回,但是当你搜索它的兄弟时它没有找到它,这就是为什么我建议将led.superview
作为地方为了搜索您的标签,父母应该能够找到其他孩子。