您的位置:首页 > 产品设计 > UI/UE

【代码笔记】点击任何处,显示出红色的UIView

2016-01-12 09:18 441 查看
一,效果图。



二,工程图。



三,代码。

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: