Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:52:08+00:00 2026-06-09T02:52:08+00:00

Hi I have a list of buttons which I am displaying using for loop.

  • 0

Hi I have a list of buttons which I am displaying using for loop. I want to animate random button can any one help me. Here is the code which I am trying, with this code only last button is animating continuously.

 -(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"];
       }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:52:09+00:00Added an answer on June 9, 2026 at 2:52 am

    This is your problem:

        myButton.tag = randomIndex;
       if(randomIndex == myButton.tag)
    

    You see, when you have a pointer to an instance (in this case, a UIButton named myButton), it holds only one instance at a time, which must be the “last button” that you describe. Instances in Objective-C (or any other language for that matter), do not magically morph into the object you expect them to be within the method they would be most convenient in. You need to actually assign myButton to a different instance, or reference that other instance instead.

    This is further complicated by the fact that you are setting, then immediately checking the same value (.tag), which is akin to saying “X is now 3. If X is 3, then…”. You need a proper reference to every button, and some place to store them all (say, an NSArray). That way, you can use the random index to retrieve the button from the array with objectAtIndex:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jQuery. I have a list of radio buttons which I am
I have a button which opens a context menu with a list of various
I have a list view item which has a button and displays properties on
I have a list of buttons with grey background, and when a button is
I have list view which contains 2 buttons and 2 textviews. I change the
I have a list view which contains a button in each row of the
I have an IList in my model. Which i am displaying as radio buttons.
I have an ASP.NET web form on which I'm displaying a list of database
enter image description herehai, i have a list of buttons in a view which
I've implemented a custom index section list by programmatically adding buttons using a loop.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.