码迷,mamicode.com
首页 > 其他好文 > 详细

22.小项目:应用市场 版本5.0

时间:2015-08-03 22:31:02      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

------------- CZapp.h -------------

#import <Foundation/Foundation.h>


@interface CZApp : NSObject


@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *icon;

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

+ (instancetype) appWithDict:(NSDictionary *) dict;

+ (NSArray *) apps;


@end

------------- CZapp.m -------------

#import "CZApp.h"


@implementation CZApp

- (instancetype)initWithDict:(NSDictionary *)dict

{

    if (self = [super init])

    {

        self.name = dict[@"name"];

        self.icon = dict[@"icon"];

    }

    return self;

}

+ (instancetype)appWithDict:(NSDictionary *)dict

{

    return [[self alloc] initWithDict:dict];

}

+ (NSArray *) apps

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];

    NSArray *array = [NSArray arrayWithContentsOfFile:path];

    NSMutableArray *arrayM = [NSMutableArray array];

    for (NSDictionary *dict in array)

    {

        CZApp *app = [CZApp appWithDict:dict];

        [arrayM addObject:app];

    }

    return arrayM;

}

@end

------------- ViewController.m -------------

#import "ViewController.h"

#import "CZApp.h"


@interface ViewController ()

@property (nonatomic,strong) NSArray *apps;

@end


@implementation ViewController

- (NSArray *)apps

{

    if(_apps == nil){

        _apps = [CZApp apps];

    }

    return _apps;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    CGFloat appW = 100;

    CGFloat appH = 100;

    int numOfCol = 3;

    CGFloat marginX = (self.view.frame.size.width - appW * numOfCol) / (numOfCol + 1);

    CGFloat marginY = 20;

    for (int index = 0; index < self.apps.count; index++)

    {

        UIView *appView = [[UIView alloc] init];

        int col = index % numOfCol;

        CGFloat appX = marginX + (appW + marginX) * col;

        int row = index / numOfCol;

        CGFloat appY = 20 + marginY + (appH + marginY) * row;

        appView.frame = CGRectMake(appX, appY,appW,appH);

        [self.view addSubview:appView];

        [self addSubviewsForAppView:appView atIndex:index];

    }

}

- (void) addSubviewsForAppView:(UIView *) appView atIndex:(int) index

{

    CGFloat appW = appView.frame.size.width;

    CGFloat appH = appView.frame.size.height;

    CZApp *app = self.apps[index];

    UIImageView *imageView = [[UIImageView alloc] init];

    CGFloat iconW = 50;

    CGFloat iconH = 50;

    CGFloat iconX = (appW - iconW) / 2;

    CGFloat iconY = 0;

    imageView.frame = CGRectMake(iconX,iconY , iconW, iconH);

    [appView addSubview:imageView];

    imageView.image = [UIImage imageNamed:app.icon];

    UILabel *nameLabel = [[UILabel alloc] init];

    CGFloat nameX = 0;

    CGFloat nameY = CGRectGetMaxY(imageView.frame);

    CGFloat nameW = appW;

    CGFloat nameH = 20;

    nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);

    [appView addSubview:nameLabel];

    nameLabel.text = app.name;

    nameLabel.textAlignment = NSTextAlignmentCenter;

    nameLabel.font = [UIFont systemFontOfSize:14];

    UIButton *downloadBtn = [[UIButton alloc] init];

    CGFloat downloadX = iconX;

    CGFloat downloadY = CGRectGetMaxY(nameLabel.frame);

    CGFloat downloadW = iconW;

    CGFloat downloadH = appH - downloadY;

    downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);

    [appView addSubview:downloadBtn];

    [downloadBtn setTitle:@"下载" forState:UIControlStateNormal];

    [downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];

    [downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];

}

@end


22.小项目:应用市场 版本5.0

标签:

原文地址:http://www.cnblogs.com/lixiang2015/p/4700318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!