嗨,我有一个使用 for 循环显示的按钮列表。我想为随机按钮设置动画,任何人都可以帮助我。这是我正在尝试的代码,此代码只有最后一个按钮连续动画。
-(void)arrangeFavouriteWords
{
[buttonsArray removeAllObjects];
buttonsArray = [[NSMutableArray alloc]init];
for(int i=0;i<count1;i++)
{
WordObject *favObj = [databaseArray objectAtIndex:i];
float height = [MainViewController calculateHeightOfTextFromWidth:favObj.wordName :fontValue : buttonWidth :UILineBreakModeCharacterWrap];
myButton = [[MyCustomButton alloc]initWithIdValue:favObj.wordName];
myButton.backgroundColor = [UIColor clearColor];
myButton.frame = CGRectMake(0,yCoordinate,buttonWidth,height);
myButton.tag = i;
[myButton.titleLabel setFont:fontValue];
[myButton setTitleColor:color forState:UIControlStateNormal];
[myButton setTitle:favObj.wordName forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(wordClicked:) forControlEvents:UIControlEventTouchUpInside];
myButton.contentHorizontalAlignment = NO;
[buttonsArray addObject:myButton];
[displayView addSubview:myButton];
[myButton release];
yCoordinate = yCoordinate + height;
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer
{
int randomIndex = arc4random() % [buttonsArray count];
printf("\n the random index is :%d",randomIndex);
myButton.tag = randomIndex;
if(randomIndex == myButton.tag)
{
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
theAnimation.duration=1.0;
//theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.25f];
theAnimation.toValue=[NSNumber numberWithFloat:1.0f];
[myButton.layer addAnimation:theAnimation forKey:@"transform.scale"];
}
}