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 6382329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:30:30+00:00 2026-05-25T02:30:30+00:00

I am adding a button to a UIView and creating an animation. How do

  • 0

I am adding a button to a UIView and creating an animation. How do I remove the button when the animation stops? Here is my code…

if (myImageCounter < MAX_IMAGES)
{
    // Set Counter

    myImageCounter++;

    // Add Image

    OBShapedButton *button = [OBShapedButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"images/Happy.gif"];
    int width = [self randomNumberBetween:MAX_WIDTH and:MIN_WIDTH];
    int height = width;

    // Set button properties
    button.adjustsImageWhenHighlighted = NO;
    button.adjustsImageWhenDisabled = NO;
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, width, height);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    // Add button to view
    [self.view addSubview:button];

    // Animate image

    int adjustedMaxX = MAX_X - height;
    int adjustedMaxY = MAX_Y - width;
    int x0 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y0 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x1 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y1 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x2 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y2 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x3 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y3 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];

    // Create animage
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 1;

    // Create path
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, x0, y0);
    CGPathAddCurveToPoint(curvedPath, NULL, x1, y1, x2, y2, x3, y3);

    // Set path
    pathAnimation.path = curvedPath;

    // Draw path
    [myView drawPath:curvedPath];

    // Run animation
    [button.layer addAnimation:pathAnimation forKey:nil];

    // Release path
    CGPathRelease(curvedPath);  

    // Increment image count
    [self setImage:(myImageCounter + 1)];
}

I ended up doing:

button.tag = uniqueID;
[pathAnimation setValue:[NSString stringWithFormat:@"%d", uniqueID] forKey:@"name"];

Then:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString* name = [theAnimation valueForKey:@"name"];
[self removeImage:[myView viewWithTag:[name intValue]]];
[name release];
}
  • 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-05-25T02:30:31+00:00Added an answer on May 25, 2026 at 2:30 am

    You could just use a simple tag, like this….

    Creating the button

    #define  kButtonTag  1
    
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton setFrame:CGRectMake(50.0, 50.0, 220.0, 80.0)];
    [myButton setTag:kButtonTag];
    [myButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    

    Removing the button

    [[self.view viewWithTag:kButtonTag] removeFromSuperview];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that works great for adding a button to the toolbar: NSArray*
I'm creating some UILabels in my UIView , filling them with data, adding them
I am creating a button pro-grammatically in my TableView Header cell and adding TouchUpInside
I'm trying to modify the fridge magnets example by adding a button that will
In an Android ListActivity, I am adding a button as a footer in a
I've got an existing Access MDB. I'm adding a command button to an existing
Adding the row one by one in the button click.. (i tried but its
i am adding more than one link button during runtime but they all have
I am working on adding a custom accessory view, (a button) to a UITableViewCell,
I'm adding a row like this, when a button is pressed. It must be

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.