你的代码有效,我用过。查看
.h 文件下面的代码:
#import <UIKit/UIKit.h>
@interface StackoverflowTestViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *cautionView;
- (IBAction)btnToggleClick:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btnToggle;
@end
.m 文件:
#import "StackoverflowTestViewController.h"
@interface StackoverflowTestViewController ()
@end
@implementation StackoverflowTestViewController
bool isShown = false;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnToggleClick:(id)sender {
if (!isShown) {
_cautionView.frame = CGRectMake(130, 20, 0, 0);
[UIView animateWithDuration:0.25 animations:^{
_cautionView.frame = CGRectMake(130, 30, 100, 200);
}];
isShown = true;
} else {
[UIView animateWithDuration:0.25 animations:^{
_cautionView.frame = CGRectMake(130, 30, 0, 0);
}];
isShown = false;
}
}
@end