在方法中保留变量
这应该很容易。
我有一个方法可以上下移动视图,同时我可以上下移动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用静态变量在方法调用之间保留其内容。但是,初始化程序必须是常量,因此您无法调用方法来初始化它。您可以做的是使初始值无效,并在设置变量之前对此进行测试。
我不确定你的意思是什么。如果您只想设置中心的 x 坐标,则需要获取当前中心,更改 x 坐标,然后保存新点。
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.
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.