一,效果圖。二,工程圖。三,代碼。RootViewController.h#import //頭文件#import "MoreView.h"@interface RootViewController : UIViewController{ //是否點擊 BOOL isSwitch; ...
一,效果圖。
二,工程圖。
三,代碼。
RootViewController.h
#import <UIKit/UIKit.h>
//頭文件
#import "MoreView.h"
@interface RootViewController : UIViewController
{
//是否點擊
BOOL isSwitch;
//紅色UIView界面
MoreView *moreView;
}
@end
RootViewController.m
//點擊任何處,顯示出紅色的UIView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isSwitch) {
[moreView removeFromSuperview];
isSwitch=NO;
}else{
moreView=[[MoreView alloc]initWithFrame:CGRectMake(10, 100, 200, 50)];
[self.view addSubview:moreView];
isSwitch=YES;
}
}
MoreView.h
#import <UIKit/UIKit.h>
@interface MoreView : UIView
@end
MoreView.m
#import "MoreView.h"
@implementation MoreView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//設計背景色為紅色
self.backgroundColor=[UIColor redColor];
}
return self;
}
@end