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


控制器:代码

<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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值