Objective c 在代码中移动UI按钮
我有一段代码,当点击一个按钮时,下面的按钮会向下移动。我想知道,当再次单击按钮时,下面的按钮是否可以移回其原始位置 .hObjective c 在代码中移动UI按钮,objective-c,ios,uibutton,Objective C,Ios,Uibutton,我有一段代码,当点击一个按钮时,下面的按钮会向下移动。我想知道,当再次单击按钮时,下面的按钮是否可以移回其原始位置 .h @interface ViewController : UIViewController { IBOutlet UIButton *button; } -(IBAction)buttonClicked:(id)sender; -(IBAction)buttonClicked1:(id)sender; @end .m - (void)buttonClicked:(id
@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
m
@interface ViewController : UIViewController {
BOOL buttonShifted;
CGRect startRect;
IBOutlet UIButton *button;
}
-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;
@end
// 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;
}