上QQ阅读APP看书,第一时间看更新
实例013 隐藏指定的UIView区域
实例说明
本实例的功能是使用UIView的属性hidden来隐藏指定的区域。当属性hidden的值为YES时隐藏UIView,当属性hidden的值为NO时显示UIView。当单击本实例中的“点击”按钮时,会实现文本隐藏和文本显示的转换。
具体实现
实例文件UIkitPrjHide.h的实现代码如下所示。
#import "SampleBaseController.h" @interface UIKitPrjHide : SampleBaseController { @private UILabel* label_; } @end
实例文件UIkitPrjHide.m的实现代码如下所示。
#import "UIKitPrjHide.h" @implementation UIKitPrjHide #pragma mark ----- Override Methods ----- // finalize - (void)dealloc { [label_ release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; label_ = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 320, 200 )]; label_.textAlignment = UITextAlignmentCenter; label_.backgroundColor = [UIColor blackColor]; label_.textColor = [UIColor whiteColor]; label_.text = @"I'm here! "; [self.view addSubview:label_]; UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake( 0, 0, 100, 40 ); CGPoint newPoint = self.view.center; newPoint.y = self.view.frame.size.height -70; button.center = newPoint; [button setTitle:@"点击" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControl EventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonDidPush { label_.hidden = ! label_.hidden; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [self.navigationController setNavigationBarHidden:NO animated:YES]; } @end
执行后的效果如图2-16所示,按下图2-16中的“点击”按钮后会隐藏文本,如图2-17所示。
图2-16 执行效果
图2-17 隐藏了文本