美文网首页
简单的MVC 之View封装

简单的MVC 之View封装

作者: 江R | 来源:发表于2017-01-10 15:36 被阅读0次
    屏幕快照 2017-01-10 下午2.57.34.png
    //CZAppInfoView.h
    
    #import
    
    @classCZAppInfo;
    
    @interfaceCZAppInfoView :UIView
    
    @property(nonatomic,strong)CZAppInfo*appInfo;
    
    + (instancetype)appInfoView;
    
    //- (void)setData;
    
    @end
    
    

    =============================================

    
    #import"CZAppInfo.h"
    
    @interfaceCZAppInfoView()
    
    @property(weak,nonatomic)IBOutletUIImageView*iconView;
    
    @property(weak,nonatomic)IBOutletUILabel*nameView;
    
    - (IBAction)downloadClick:(UIButton*)sender;
    
    @end
    
    @implementationCZAppInfoView
    
    + (instancetype)appInfoView
    
    {
    
    //从xib中加载subview
    
    NSBundle*bundle = [NSBundlemainBundle];
    
    //加载xib中得view
    
    CZAppInfoView*subView = [[bundleloadNibNamed:@"CZAppInfoView"owner:niloptions:nil]lastObject];
    
    //加载xml
    
    //CZAppInfoView *v = [[CZAppInfoView alloc] init];
    
    //v.frame
    
    returnsubView;
    
    }
    
    /**
    
    *重写属性的setter方法,给子控件赋值
    
    *
    
    *@param appInfo <#appInfo description#>
    
    */
    
    - (void)setAppInfo:(CZAppInfo*)appInfo
    
    {
    
    _appInfo= appInfo;
    
    self.nameView.text= appInfo.name;
    
    self.iconView.image= [UIImageimageNamed:appInfo.icon];
    
    }
    
    //- (void)setData
    
    //{
    
    //self.nameView.text = self.appInfo.name;
    
    //self.iconView.image = [UIImage imageNamed:self.appInfo.icon];
    
    //
    
    //}
    
    - (IBAction)downloadClick:(UIButton*)sender {
    
    //取消和用户的交互
    
    self.superview.userInteractionEnabled=NO;
    
    sender.enabled=NO;
    
    //提示正在下载
    
    UILabel*tipView = [[UILabelalloc]init];
    
    //self(自定义view).superview(控制器的根view)
    
    [self.superviewaddSubview:tipView];
    
    tipView.text= [NSStringstringWithFormat:@"正在下载:%@",self.appInfo.name];
    
    //frame
    
    CGFloattipW =200;
    
    CGFloattipH =25;
    
    CGFloattipX = (self.superview.frame.size.width- tipW) /2;
    
    CGFloattipY = (self.superview.frame.size.height- tipH) /2;
    
    tipView.frame=CGRectMake(tipX, tipY, tipW, tipH);
    
    tipView.backgroundColor= [UIColorgrayColor];
    
    tipView.textAlignment=NSTextAlignmentCenter;
    
    //透明度
    
    tipView.alpha=0;
    
    //圆角
    
    tipView.layer.cornerRadius=5;
    
    tipView.layer.masksToBounds=YES;//剪裁超过bounds的部分
    
    //动画效果
    
    [UIViewanimateWithDuration:1.0animations:^{
    
    tipView.alpha=0.9;
    
    }completion:^(BOOLfinished) {
    
    [UIViewanimateWithDuration:1.0delay:3.0options:UIViewAnimationOptionCurveLinearanimations:^{
    
    tipView.alpha=0;
    
    }completion:^(BOOLfinished) {
    
    //从父view中移除
    
    [tipViewremoveFromSuperview];
    
    //
    
    self.superview.userInteractionEnabled=YES;
    
    }];
    
    }];
    
    }
    
    @end
    
    

    下面是 控制器 加载View的代码,view的复用

    
    //NSLog(@"%@",self.appInfos);
    
    inttotalColumn =3;
    
    CGFloatsubViewW =100;
    
    CGFloatsubViewH =100;
    
    //子view的横向间距=(父view的宽度- 3 *子view的宽度) / 4
    
    CGFloatmarginX = (self.view.frame.size.width- totalColumn * subViewW) / (totalColumn +1);
    
    //子view的纵向间距=20
    
    CGFloatmarginY =20;
    
    //3动态生成9宫格的方块
    
    for(inti =0; i
    
    //动态生成view
    
    //UIView *subView = [[UIView alloc] init];
    
    //[self.view addSubview:subView];
    
    ////从xib中加载subview
    
    //NSBundle *bundle = [NSBundle mainBundle];
    
    ////加载xib中得view
    
    //CZAppInfoView *subView = [[bundle loadNibNamed:@"CZAppInfoView" owner:nil options:nil] lastObject];
    
    //封装自定义view
    
    CZAppInfoView*subView = [CZAppInfoViewappInfoView];
    
    [self.viewaddSubview:subView];
    
    //计算frame
    
    //当前子view的行号=当前遍历到得索引值/总列数
    
    introw = i / totalColumn;
    
    //当前子view的列号=当前遍历到得索引值%总列数
    
    intcolumn = i % totalColumn;
    
    //
    
    //子view横坐标的公式=子view的横向间距+列号* (子view的横向间距+子view的宽度)
    
    CGFloatsubViewX = marginX + column * (marginX + subViewW);
    
    //子view纵坐标的公式= 20 +行号* (子view的纵向间距+子view的高度)
    
    CGFloatsubViewY =30+ row * (marginY + subViewH);
    
    subView.frame=CGRectMake(subViewX, subViewY, subViewW, subViewH);
    
    //取得当前遍历到得数据
    
    CZAppInfo*appInfo =self.appInfos[i];
    
    subView.appInfo= appInfo;
    
    //给subview的子控件赋值
    
    ////方式1
    
    //UIImageView *iconView = subView.subviews[0];
    
    //iconView.image = [UIImage imageNamed:appInfo.icon];
    
    ////方式2
    
    //UILabel *nameView =(UILabel *)[subView viewWithTag:10];
    
    //nameView.text = appInfo.name;
    
    //subView.nameView.text = appInfo.name;
    
    //subView.iconView.image = [UIImage imageNamed:appInfo.icon];
    
    }
    

    相关文章

      网友评论

          本文标题:简单的MVC 之View封装

          本文链接:https://www.haomeiwen.com/subject/drgkbttx.html