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

  • Home
  • SEARCH
  • 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 6154675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:18:07+00:00 2026-05-23T20:18:07+00:00

This should be easy. I’ve got a method to move a view up and

  • 0

This should be easy.

I’ve got a method to move a view up and down and while doing so I move a UIButton over and down… then move the button back to its original position when the method runs again.

I thought I could get the original position of the button with float originalCenterX = topSubmitButton.center.x and float originalCenterY = topSubmitButton.center.y but of course those are overwritten with the center of the button when the method is hit for the second time.

How does one preserve a variable over multiple iterations of a method?

-(IBAction)scrollForComment { 

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; 

CGRect rect = self.view.frame;

NSLog(@"button center x = %f y = %f",topSubmitButton.center.x,topSubmitButton.center.y);
float originalCenterX = topSubmitButton.center.x;
float originalCenterY = topSubmitButton.center.y;

if (commentViewUp) {
    rect.origin.y = self.view.frame.origin.y + 80;// move down view by 80 pixels
    commentViewUp = NO;

    CGPoint newCenter = CGPointMake( 57.0f , 73.0f); // better to calculate this if you are going to rotate to landscape

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5f];
    topSubmitButton.center = newCenter;
    [UIView commitAnimations];

} else { // toggling the view's location
    rect.origin.y = self.view.frame.origin.y - 80;// move view back up 80 pixels
    commentViewUp = YES;


    CGPoint newCenter = CGPointMake(160 , 74.0f + topSubmitButton.center.y);// would like to calculate center
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5f];
    topSubmitButton.center = newCenter;
    [UIView commitAnimations];
  }

self.view.frame = rect;

[UIView commitAnimations];

}

Bonus if you can tell me how to put the center of a view in a CGPoint’s x value.

  • 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-23T20:18:08+00:00Added an answer on May 23, 2026 at 8:18 pm

    You can use a static variable to preserve its contents between method calls. However, the initializer must be constant, so you cannot call a method to initialize it. What you can do is make the initial value invalid, and test for this before setting the variable.

    -(IBAction)scrollForComment {
        static float originalCenterX = −1, originalCenterY = −1; // Assuming −1 would be an invalid value
        if(originalCenterX == −1 && originalCenterY == −1) {
            CGPoint temp = topSubmitButton.center;
            originalCenterX = temp.x;
            originalCenterY = temp.y;
        }
        ...
    

    Bonus if you can tell me how to put the center of a view in a CGPoint’s x value.

    I am not sure what you meant by this. If you want to be able to set only the x coordinate of the center, you will need to get the current center, change the x coordinate, and save the new point.

    CGPoint temp = topSubmitButton.center;
    temp.x = newXValue;
    topSubmitButton.center = temp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be easy but its got me stumped. I've got a button on
I think this should be easy, but it's evading me. I've got a many-to-many
This should be and easy one for the LINQ gurus out there. I'm doing
This should be easy for Haskell pros.. I've got a Maybe value, > let
This should be easy: When calling a didSelectRowAtIndexPath, I run a complex method, that
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
This should be easy, just curious. I know httpd is the HTTP daemon, just
This should be easy (at least no one else seems to be having a
OK, I'm feeling like this should be easy but am obviously missing something fundamental

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.