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

初学IOSUI基础第二天demo1,九宫格纯代码版本,有GIF,有源文件

2016-07-20 10:56 513 查看


控制器:代码

<pre name="code" class="objc">

#import "ViewController.h"
#import "AMShop.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *nineView;
/** 添加按钮*/
@property (strong, nonatomic) UIButton *add;
/** 删除按钮*/
@property (strong, nonatomic) UIButton *remove;
/** lable*/
@property (weak, nonatomic) IBOutlet UILabel *Label;
/** shops*/
@property (strong, nonatomic) NSArray *shops;

@end
@implementation ViewController
#pragma mark 重写shops方法
-(NSArray*)shops{

if (_shops==nil) {
//获得包地址
NSBundle *bundle=[NSBundle mainBundle];
//打包地址
NSString *file=[bundle pathForResource:@"shops" ofType:@"plist"];
//获取资源
self.shops=[NSArray arrayWithContentsOfFile:file];
}
return _shops;
}
/**
手动添加按钮调用方法
*/
- (UIButton*)newBtu:(NSString*)name :(NSString*)name_highlighted :(NSString*) name_disabled :(int)tagNum :(CGRect)fram{
//1.新建对象
UIButton *btn=[[UIButton alloc] init];
[btn setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:name_highlighted] forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:name_disabled] forState:UIControlStateDisabled];
btn.tag=tagNum;
btn.frame=fram;
[btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

return btn;
}

- (void)viewDidLoad {
[super viewDidLoad];
//hud设置为隐藏
self.Label.hidden=YES;
//0.设置整个View为灰色
self.view.backgroundColor=[UIColor grayColor];
//打印屏幕大小
//NSLog(@"%@",NSStringFromCGRect(self.view.frame));

//1.添加按钮
self.add=[self newBtu:@"add" :@"add_highlighted" :@"add_disabled" :10 : CGRectMake(50,50,50,50)];
//2.删除按钮
self.remove=[self newBtu:@"remove" :@"remove_highlighted" :@"remove_disabled" :11 :CGRectMake(275,50,50,50)];

}

-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)btn:(UIButton*)r{
//获得索引值
NSUInteger count=self.nineView.subviews.count;
NSLog(@"%lu",count);
//1.添加文字数组

NSLog(@"%@",self.shops);
//2.新建小九宫格
UIView *nine=[[UIView alloc]init];

//tag10为添加按钮
if (r.tag==10) {
//3.手动添加图片和文字的View
//3.1图片
UIImageView *image =[[UIImageView alloc]init];
image.frame=CGRectMake(10, 10, 70, 70);

//4.2文字
UILabel *lable =[[UILabel alloc]init];
lable.frame=CGRectMake(18, 80, 70, 20);
if (count<6) {
//使用模型加载
NSDictionary *dir=self.shops[count];
AMShop *shop=[AMShop shopWithDict:dir];
image.image=[UIImage imageNamed:shop.icon];
lable.text=shop.name;
}
//判断商品是否满
if (count==5) {
//关闭按钮
self.add.enabled=NO;
//显示HUD(label)
self.Label.text=@"商品已满";
self.Label.hidden=NO;
[self performSelector:@selector(hideHud) withObject:nil afterDelay:1.0];

}
self.remove.enabled=YES;

//5.手动创建小九宫格View并且把图片文字添加进去
if (count<3) {
//nine.backgroundColor=[UIColor grayColor];
nine.frame=CGRectMake(2+(count*102), 0, 100, 100);
[nine addSubview:image];
[nine addSubview:lable];
[self.nineView addSubview:nine];

}else if(count>=3 && count<6){
int temp=count%3;
// nine.backgroundColor=[UIColor grayColor];
nine.frame=CGRectMake(2+(temp*102), 0+100, 100, 100);
[nine addSubview:image];
[nine addSubview:lable];
[self.nineView addSubview:nine];

}else{
NSLog(@"error");
}
//删除按钮
}else if(r.tag==11){
//判断是否没有元素
if(count==1){
self.remove.enabled=NO;
//显示HUD(label)
self.Label.text=@"商品已清空";
self.Label.hidden=NO;
[self performSelector:@selector(hideHud) withObject:nil afterDelay:1.0];
}
self.add.enabled=YES;
//删除最后一个
[[self.nineView.subviews lastObject] removeFromSuperview];
}

}
-(void)hideHud{
self.Label.hidden=YES;
}

@end


文件下载:
http://download.csdn.net/detail/z2340868/9581481
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息