MVC应用:简易版的应用管理

1.程序运行效果



2.程序项目结构



03,对应代码

//
//  YFController.m
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import "YFController.h"
#import "YFAppModel.h"
#import "YFAppView.h"
#import "YFCoverView.h"

//plist 文件名
#define kYFAppPlist @"app.plist"

//列数
#define kAppColumn 3
//应用图的宽
#define kAppViewWidth 80
//应用图的高
#define kAppViewHeight 90

@interface YFController ()<yfAppViewDelegate>

//所有的应用模型 集合
@property(nonatomic,strong)NSArray<YFAppModel *>  *dataArray;


@property(nonatomic,weak)YFCoverView *coverView;

@end

@implementation YFController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupAppView];
}
//设置 应用的 布局
-(void)setupAppView
{
    
    //计算 间隔
    CGFloat margin = (self.view.frame.size.width - kAppColumn *kAppViewWidth) / (kAppColumn +1);
    
    for (int i = 0; i < self.dataArray.count; i++) {
        //计算行索引
        NSInteger rowIndex = i / kAppColumn;
        //计算列索引
        NSInteger columnIndex = i % kAppColumn;
        //计算 图标的 x
        CGFloat appViewX = (columnIndex + 1) *margin + columnIndex * kAppViewWidth;
        //计算 图标的 y
        CGFloat appViewY = (rowIndex + 1) * margin + rowIndex * kAppViewHeight;
        //创建图标
        YFAppView *appView = [YFAppView appView];
        appView.deleagte = self;
        //设置图标的frame
        appView.frame = CGRectMake(appViewX, appViewY, kAppViewWidth, kAppViewHeight);
        
        [self.view addSubview:appView];
        
        appView.appModel = self.dataArray[i];
    }
}

/**
 *  懒加载数据
 *
 *  @return 数组
 */
-(NSArray<YFAppModel *> *)dataArray
{
    if (_dataArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:kYFAppPlist ofType:nil];
        NSArray *tempDataArray =  [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *dataArray = [NSMutableArray array];
        
        for (NSDictionary *dict in tempDataArray) {
            YFAppModel *appModel = [YFAppModel appModelWithDict:dict];
            [dataArray addObject:appModel];
        }
        _dataArray = dataArray;
    }
    
    return _dataArray;
}
/**
 *  懒加载覆盖界面
 *
 *  @return 覆盖界面
 */
-(YFCoverView *)coverView
{
    if (_coverView == nil) {
        YFCoverView *coverView = [[YFCoverView alloc]initWithFrame:self.view.frame];
        [self.view addSubview:coverView];
        _coverView = coverView;
    }
    return _coverView;
}
-(void)appView:(YFAppView *)appView downlBtn:(UIButton *)downBtn downloadApp:(YFAppModel *)appModel
{
    [UIView animateWithDuration:1 animations:^{
        self.coverView.appModel = appModel;
        self.coverView.alpha = 0.6;
    }completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1.0 animations:^{
            downBtn.enabled = NO;
            [self.coverView removeFromSuperview];
        }];
    }];
}
@end



//
//  YFAppModel.h
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//


#import <Foundation/Foundation.h>

@interface YFAppModel : NSObject

//注意事项
/**
 *  NSString 使用 copy 关键字 
    是因为   copy 能够对重新拷贝一份对象 所以两个指针指向的是不同的地址
            strong 是不能对对象重新进行拷贝 所以两个指针指向的是同一个地址
 */

/**
 *  属性  应用名称
 */
@property(nonatomic,copy)NSString *name;
/**
 *  属性  应用图标
 */
@property(nonatomic,copy)NSString *icon;

/**
 *  类方法  根据字典 初始化 对象模型
 *
 *  @param dict 字典
 *
 *  @return 模型
 */
/**
 *  instancetype  与  id  的区别
 
    1.instancetype 只能用作为 返回值参数  而且会明确当前的 对象是 当前类的对象
    2.id  是万能指针  既能当做  返回参数  也可以当做 普通参数  不明确指向对象
 */
+(instancetype)appModelWithDict:(NSDictionary *)dict;

-(instancetype)initWithDict:(NSDictionary *)dict;
@end






//
//  YFAppModel.m
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import "YFAppModel.h"

@implementation YFAppModel

+(instancetype)appModelWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict
{   
    if (self = [super init]) {
        //KVC 进行属性的赋值
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end

//
//  YFAppView.h
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YFAppModel,YFAppView;


@protocol yfAppViewDelegate <NSObject>

-(void)appView:(YFAppView *)appView downlBtn:(UIButton *) downBtn downloadApp:(YFAppModel *)appModel;

@end


@interface YFAppView : UIView


@property(nonatomic,strong)YFAppModel *appModel;

/**
 *  代理 
    代理的 @property 属性 之中 必须使用  weak  是为了防止 循环引用
 */

@property(nonatomic,weak)id<yfAppViewDelegate> deleagte;
/**
 *  类方法 创建视图
 *
 *  @return appView
 */
+(instancetype)appView;

@end




//
//  YFAppView.m
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import "YFAppView.h"
#import "YFAppModel.h"


@interface YFAppView()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLableView;

@end

@implementation YFAppView

+(instancetype)appView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

-(void)setAppModel:(YFAppModel *)appModel
{
    _appModel = appModel;
    
    self.iconImageView.image = [UIImage imageNamed:appModel.icon];
    self.nameLableView.text = appModel.name;
}
- (IBAction)downClick:(UIButton *)btn {
    if ([self.deleagte respondsToSelector:@selector(appView:downlBtn:downloadApp:)]) {
        [self.deleagte appView:self downlBtn:btn downloadApp:self.appModel];
    }
    
}
@end




//
//  YFCoverView.h
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YFAppModel;
@interface YFCoverView : UIView

@property(nonatomic,strong)YFAppModel *appModel;
@end



//
//  YFCoverView.m
//  01-应用管理
//
//  Created by YF on 16/3/1.
//  Copyright © 2016年 YF. All rights reserved.
//

#import "YFCoverView.h"
#import "YFAppModel.h"
@interface YFCoverView()

@property (nonatomic, weak) UILabel *nameLabel;

@end

@implementation YFCoverView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

-(void)setup
{
    // 设置颜色
    self.backgroundColor = [UIColor blackColor];
    // 设置alpha 值为0
    self.alpha = 0;
    [self addLabel];

}
//添加文本的 UILabel
-(void)addLabel
{
    // 添加一个label
    CGSize coverViewSize = self.bounds.size;
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, coverViewSize.width, 20)];
    self.nameLabel = nameLabel;
    // 设置中心点
    [nameLabel setCenter:CGPointMake(coverViewSize.width/2, coverViewSize.height/2)];
    // 设置对其方式
    nameLabel.textAlignment = NSTextAlignmentCenter;
    // 设置文本颜色
    nameLabel.textColor = [UIColor whiteColor];
    [self addSubview:nameLabel];
}

// 重写set方法
- (void)setAppModel:(YFAppModel *)appModel {
    // 给属性赋值
    _appModel = appModel;
    // 给控件赋值
    _nameLabel.text = [NSString stringWithFormat:@"%@,正在下载...",_appModel.name];
}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值