初学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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Gifski:一个跨平台的高质量 GIF 编码器
- ip地址基础知识
- 模仿动画的放大缩小容器
- VBS基础编程教程 (第1篇)
- VBS基础编程教程 (第3篇)
- 路由器基础精析
- 九宫格的含义 横竖斜三个数相加都等于15
- VBS基础编程教程 (第4篇)
- VBS基础编程教程 (第5篇)
- VBS基础编程教程 (第6篇)
- VBS编程教程 (第2篇)
- jQuery 1.9.1源码分析系列(十五)动画处理之缓动动画核心Tween
- AJAX初级教程之初识AJAX