gpt4 book ai didi

objective-c - 在代码中移动 UIButton

转载 作者:行者123 更新时间:2023-11-29 04:54:53 24 4
gpt4 key购买 nike

我有代码表示当单击按钮时,下面的按钮会向下移动。我想知道是否可以做到这样,当再次单击该按钮时,下面的按钮会移回其原始位置。

.h

@interface ViewController : UIViewController  {

IBOutlet UIButton *button;
}

-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;

@end

.m

- (void)buttonClicked:(id)sender {
CGRect frame = button.frame;
frame.origin.x = 65; // new x coordinate
frame.origin.y = 130; // new y coordinate
button.frame = frame;
}

最佳答案

您可以使用 BOOL 来跟踪状态并存储用于将按钮向后移动的 startRect

.h

@interface ViewController : UIViewController  {
BOOL buttonShifted;
CGRect startRect;
IBOutlet UIButton *button;
}

-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;

@end

.m

// make sure to set the original button frame when view loads
// ie startRect = button.frame;

- (void)buttonClicked:(id)sender {
if (buttonShifted) {
button.frame = startRect;
} else {
CGRect frame = button.frame;
frame.origin.x = 65; // new x coordinate
frame.origin.y = 130; // new y coordinate
button.frame = frame;
}
buttonShifted = !buttonShifted;
}

关于objective-c - 在代码中移动 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com