在方法中保留变量

发布于 2024-11-24 19:07:20 字数 1554 浏览 1 评论 0原文

这应该很容易。

我有一个方法可以上下移动视图,同时我可以上下移动 UIButton...然后当该方法再次运行时将按钮移回其原始位置。

我以为我可以使用 float originalCenterX = topSubmitButton.center.x 和 float originalCenterY = topSubmitButton.center.y 获得按钮的原始位置,但当然,当第二次点击该方法时,这些会被按钮的中心覆盖。

如何在方法的多次迭代中保留变量?

-(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];

}

如果你能告诉我如何将视图的中心放在 CGPoint 的 x 值中,那就太好了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一枫情书 2024-12-01 19:07:20

您可以使用静态变量在方法调用之间保留其内容。但是,初始化程序必须是常量,因此您无法调用方法来初始化它。您可以做的是使初始值无效,并在设置变量之前对此进行测试。

-(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;
    }
    ...

如果你能告诉我如何将视图的中心放在 CGPoint 的 x 值中,那就太好了。

我不确定你的意思是什么。如果您只想设置中心的 x 坐标,则需要获取当前中心,更改 x 坐标,然后保存新点。

CGPoint temp = topSubmitButton.center;
temp.x = newXValue;
topSubmitButton.center = temp;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文