zoukankan      html  css  js  c++  java
  • AppManager(demo16.03.15)

    //
    //  ViewController.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    
    
    //1 get the data
    //2 draw the view
    @interface ViewController ()
    
    //
    @property (nonatomic,strong) NSArray *appList;
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            _appList = [[NSArray alloc]initWithContentsOfFile:path];
        }
        return _appList;
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        int totalCol = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
        CGFloat marginY =10;
        CGFloat startY = 20;
        //int count = self.appList.count;
        for(int i=0;i<self.appList.count;i++){
            int row =i/totalCol;
            int col = i%totalCol;
            
            CGFloat x = marginX +(marginX+viewW)*col;
            CGFloat y =startY+ marginY +(marginY+viewH)*row;
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, y, viewW, viewH)];
            //view.backgroundColor = [UIColor redColor];
            [self.view addSubview:view];
            
            //image
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, viewW, 50)];
            //imageView.backgroundColor = [UIColor grayColor];
            UIImage *image = [UIImage imageNamed:self.appList[i][@"icon"]];
            imageView.image = image;
            //按照比例现实图像
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [view addSubview: imageView];
            
            //description
            UILabel *descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,imageView.bounds.size.height, viewW, 20)];
            //descriptionLabel.backgroundColor = [UIColor blackColor];
            descriptionLabel.text = self.appList[i][@"name"];
            descriptionLabel.font =[UIFont systemFontOfSize:12.0];
            descriptionLabel.textAlignment = NSTextAlignmentCenter;
            [view addSubview:descriptionLabel];
            
            //button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 70, viewW, 20);
            [button setTitle:@"下载" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14.0];
            //button.backgroundColor = [UIColor purpleColor];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
            [view addSubview:button];
            
        }
        
    }
    
    @end
    

    添加按钮事件和动画

    button.tag = i;
            
            [button addTarget:self action:@selector(downloadClick:) forControlEvents:UIControlEventTouchUpInside];
    

      

    -(void)downloadClick:(UIButton *)button{
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        NSDictionary *dict = self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",dict[@"name"]];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 1.0;
        [self.view addSubview:lable];
        
        //动画效果
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:1.0];
        //lable.alpha = 1.0;
        //[UIView commitAnimations];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=0.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
        
    }
    

    优化:添加AppInfo class

    //
    //  AppInfo.h
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface AppInfo : NSObject
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,copy) NSString *icon;
    
    -(id)initWithDict:(NSDictionary *)dict;
    @end
    
    //----------------------------
    
    //
    //  AppInfo.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfo.h"
    
    @implementation AppInfo
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            self.name = dict[@"name"];
            self.icon = dict[@"icon"];
        }
        return self;
    }
    
    
    @end
    
    //
    //  ViewController.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "AppInfo.h"
    
    //1 get the data
    //2 draw the view
    @interface ViewController ()
    
    //
    @property (nonatomic,strong) NSArray *appList;
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                AppInfo *info = [[AppInfo alloc]initWithDict:dict];
                [arrayM addObject:info];
            }
            
            _appList = arrayM;
        }
        return _appList;
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        int totalCol = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
        CGFloat marginY =10;
        CGFloat startY = 20;
        //int count = self.appList.count;
        for(int i=0;i<self.appList.count;i++){
            int row =i/totalCol;
            int col = i%totalCol;
            
            CGFloat x = marginX +(marginX+viewW)*col;
            CGFloat y =startY+ marginY +(marginY+viewH)*row;
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, y, viewW, viewH)];
            //view.backgroundColor = [UIColor redColor];
            [self.view addSubview:view];
            
            AppInfo *info = self.appList[i];
            
            //image
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, viewW, 50)];
            //imageView.backgroundColor = [UIColor grayColor];
            UIImage *image = [UIImage imageNamed:info.icon];
            imageView.image = image;
            //按照比例现实图像
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [view addSubview: imageView];
            
            //description
            UILabel *descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,imageView.bounds.size.height, viewW, 20)];
            //descriptionLabel.backgroundColor = [UIColor blackColor];
            descriptionLabel.text = info.name;
            descriptionLabel.font =[UIFont systemFontOfSize:12.0];
            descriptionLabel.textAlignment = NSTextAlignmentCenter;
            [view addSubview:descriptionLabel];
            
            //button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 70, viewW, 20);
            [button setTitle:@"下载" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14.0];
            //button.backgroundColor = [UIColor purpleColor];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
            [view addSubview:button];
            button.tag = i;
            
            [button addTarget:self action:@selector(downloadClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    -(void)downloadClick:(UIButton *)button{
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        AppInfo *info = self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",info.name];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 1.0;
        [self.view addSubview:lable];
        
        //动画效果
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:1.0];
        //lable.alpha = 1.0;
        //[UIView commitAnimations];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=0.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
        
    }
    
    @end
    

    继续优化model

    //
    //  AppInfo.h
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @interface AppInfo : NSObject
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,copy) NSString *icon;
    //只生成getter方法,同时没有成员变量
    @property (nonatomic,strong,readonly) UIImage *image;
    
    -(id)initWithDict:(NSDictionary *)dict;
    @end
    
    //-----
    //
    //  AppInfo.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfo.h"
    
    @interface AppInfo(){
        UIImage *_image;
    }
    @end
    
    @implementation AppInfo
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            self.name = dict[@"name"];
            self.icon = dict[@"icon"];
        }
        return self;
    }
    
    -(UIImage *)image{
        if(!_image){
            _image = [UIImage imageNamed:self.icon];
        }
        return _image;
    }
    
    
    @end
    
    //
    //  ViewController.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "AppInfo.h"
    
    //1 get the data
    //2 draw the view
    @interface ViewController ()
    
    //
    @property (nonatomic,strong) NSArray *appList;
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                AppInfo *info = [[AppInfo alloc]initWithDict:dict];
                [arrayM addObject:info];
            }
            
            _appList = arrayM;
        }
        return _appList;
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        int totalCol = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
        CGFloat marginY =10;
        CGFloat startY = 20;
        //int count = self.appList.count;
        for(int i=0;i<self.appList.count;i++){
            int row =i/totalCol;
            int col = i%totalCol;
            
            CGFloat x = marginX +(marginX+viewW)*col;
            CGFloat y =startY+ marginY +(marginY+viewH)*row;
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, y, viewW, viewH)];
            //view.backgroundColor = [UIColor redColor];
            [self.view addSubview:view];
            
            AppInfo *info = self.appList[i];
            
            //image
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, viewW, 50)];
            //imageView.backgroundColor = [UIColor grayColor];
            //UIImage *image = [UIImage imageNamed:info.icon];
            imageView.image = info.image;
            //按照比例现实图像
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [view addSubview: imageView];
            
            //description
            UILabel *descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,imageView.bounds.size.height, viewW, 20)];
            //descriptionLabel.backgroundColor = [UIColor blackColor];
            descriptionLabel.text = info.name;
            descriptionLabel.font =[UIFont systemFontOfSize:12.0];
            descriptionLabel.textAlignment = NSTextAlignmentCenter;
            [view addSubview:descriptionLabel];
            
            //button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 70, viewW, 20);
            [button setTitle:@"下载" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14.0];
            //button.backgroundColor = [UIColor purpleColor];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
            [view addSubview:button];
            button.tag = i;
            
            [button addTarget:self action:@selector(downloadClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    -(void)downloadClick:(UIButton *)button{
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        AppInfo *info = self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",info.name];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 1.0;
        [self.view addSubview:lable];
        
        //动画效果
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:1.0];
        //lable.alpha = 1.0;
        //[UIView commitAnimations];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=0.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
        
    }
    
    @end
    

    加入工厂方法

    //
    //  AppInfo.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfo.h"
    
    @interface AppInfo(){
        UIImage *_image;
    }
    @end
    
    @implementation AppInfo
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            self.name = dict[@"name"];
            self.icon = dict[@"icon"];
        }
        return self;
    }
    
    -(UIImage *)image{
        if(!_image){
            _image = [UIImage imageNamed:self.icon];
        }
        return _image;
    }
    
    //工厂方法
    +(instancetype)AppInfoInitWithDict:(NSDictionary *)dict{
        return [[self alloc]initWithDict:dict];
    }
    
    
    @end
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                //AppInfo *info = [AppInfo AppInfoInitWithDict:dict];
                [arrayM addObject:[AppInfo AppInfoInitWithDict:dict]];
            }
            
            _appList = arrayM;
        }
        return _appList;
    }
    

    新建一个class继承UIView

    配合使用XIB

    //
    //  AppInfoUIView.h
    //  AppManager
    //
    //  Created by xin on 15-3-17.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface AppInfoUIView : UIView
    @property (weak, nonatomic) IBOutlet UIImageView *iconImage;
    @property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
    @property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
    
    @end
    
    
    //
    //  AppInfoUIView.m
    //  AppManager
    //
    //  Created by xin on 15-3-17.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfoUIView.h"
    
    @implementation AppInfoUIView
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    
    //
    //  ViewController.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "AppInfo.h"
    #import "AppInfoUIView.h"
    //1 get the data
    //2 draw the view
    @interface ViewController ()
    
    //
    @property (nonatomic,strong) NSArray *appList;
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                //AppInfo *info = [AppInfo AppInfoInitWithDict:dict];
                [arrayM addObject:[AppInfo AppInfoInitWithDict:dict]];
            }
            
            _appList = arrayM;
        }
        return _appList;
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        
        int totalCol = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
        CGFloat marginY =10;
        CGFloat startY = 20;
        //int count = self.appList.count;
        for(int i=0;i<self.appList.count;i++){
            int row =i/totalCol;
            int col = i%totalCol;
            
            CGFloat x = marginX +(marginX+viewW)*col;
            CGFloat y =startY+ marginY +(marginY+viewH)*row;
            
            //UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, y, viewW, viewH)];
            //view.backgroundColor = [UIColor redColor];
            NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"AppInfoView" owner:nil options:nil];
            //UIView *view = [arr firstObject];
            AppInfoUIView *view = [arr firstObject];
            view.frame =CGRectMake(x, y, viewW, viewH);
            
            [self.view addSubview:view];
            
            AppInfo *info = self.appList[i];
            
            //image
            //UIImageView *imageView = (UIImageView *)[view viewWithTag:1];//view.subviews[0];//[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, viewW, 50)];
            //imageView.backgroundColor = [UIColor grayColor];
            //UIImage *image = [UIImage imageNamed:info.icon];
            //imageView.image = info.image;
            view.iconImage.image = info.image;
            //按照比例现实图像
            //imageView.contentMode = UIViewContentModeScaleAspectFit;
            //[view addSubview: imageView];
            
            //description
            //UILabel *descriptionLabel =(UILabel *)[view viewWithTag:2];//view.subviews[1]; //[[UILabel alloc]initWithFrame:CGRectMake(0,imageView.bounds.size.height, viewW, 20)];
            //descriptionLabel.backgroundColor = [UIColor blackColor];
            //descriptionLabel.text = info.name;
            view.descriptionLabel.text = info.name;
            //descriptionLabel.font =[UIFont systemFontOfSize:12.0];
            //descriptionLabel.textAlignment = NSTextAlignmentCenter;
            //[view addSubview:descriptionLabel];
            
            //button
            //UIButton *button =(UIButton *)[view viewWithTag:3];// view.subviews[2];//[UIButton buttonWithType:UIButtonTypeCustom];
            //button.frame = CGRectMake(0, 70, viewW, 20);
            //[button setTitle:@"下载" forState:UIControlStateNormal];
            //button.titleLabel.font = [UIFont systemFontOfSize:14.0];
            //button.backgroundColor = [UIColor purpleColor];
            //[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
            //[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
            //[view addSubview:button];
            view.downloadBtn.tag = i;
            
            [view.downloadBtn addTarget:self action:@selector(downloadClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    -(void)downloadClick:(UIButton *)button{
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        AppInfo *info = self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",info.name];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 1.0;
        [self.view addSubview:lable];
        
        //动画效果
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:1.0];
        //lable.alpha = 1.0;
        //[UIView commitAnimations];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=0.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
        
    }
    
    @end
    

    最后:

    //
    //  AppInfo.h
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @interface AppInfo : NSObject
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,copy) NSString *icon;
    //只生成getter方法,同时没有成员变量
    @property (nonatomic,strong,readonly) UIImage *image;
    
    -(id)initWithDict:(NSDictionary *)dict;
    +(instancetype)AppInfoInitWithDict:(NSDictionary *)dict;
    @end
    
    
    //
    //  AppInfo.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfo.h"
    
    @interface AppInfo(){
        UIImage *_image;
    }
    @end
    
    @implementation AppInfo
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            self.name = dict[@"name"];
            self.icon = dict[@"icon"];
        }
        return self;
    }
    
    -(UIImage *)image{
        if(!_image){
            _image = [UIImage imageNamed:self.icon];
        }
        return _image;
    }
    
    //工厂方法
    +(instancetype)AppInfoInitWithDict:(NSDictionary *)dict{
        return [[self alloc]initWithDict:dict];
    }
    
    
    @end
    

      

    //
    //  AppInfoUIView.h
    //  AppManager
    //
    //  Created by xin on 15-3-17.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "AppInfo.h"
    @interface AppInfoUIView : UIView
    
    +(instancetype)getXibView;
    +(instancetype)getXibViewWithDict:(AppInfo *)appInfo;
    @end
    
    //
    //  AppInfoUIView.m
    //  AppManager
    //
    //  Created by xin on 15-3-17.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfoUIView.h"
    @interface AppInfoUIView()
    @property (weak, nonatomic) IBOutlet UIImageView *iconImage;
    @property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
    @property (strong,nonatomic) AppInfo *appInfo;
    @end
    @implementation AppInfoUIView
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    +(instancetype)getXibView{
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"AppInfoView" owner:nil options:nil];
        return [arr firstObject];
    }
    +(instancetype)getXibViewWithDict:(AppInfo *)appInfo{
        AppInfoUIView *view = [self getXibView];
        view.appInfo = appInfo;
        return view;
    }
    
    -(void)setAppInfo:(AppInfo *)appInfo{
        _appInfo = appInfo;
        self.iconImage.image = appInfo.image;
        self.descriptionLabel.text = appInfo.name;
    }
    - (IBAction)downloadBtn:(id)sender {
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        AppInfo *info = self.appInfo;//self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",info.name];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 1.0;
        [self.superview addSubview:lable];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=0.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
    
    }
    
    @end
    

      

    //
    //  ViewController.m
    //  AppManager
    //
    //  Created by xin on 15-3-16.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "AppInfo.h"
    #import "AppInfoUIView.h"
    //1 get the data
    //2 draw the view
    @interface ViewController ()
    
    @property (nonatomic,strong) NSArray *appList;
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                [arrayM addObject:[AppInfo AppInfoInitWithDict:dict]];
            }
            
            _appList = arrayM;
        }
        return _appList;
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        
        int totalCol = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
        CGFloat marginY =10;
        CGFloat startY = 20;
        //int count = self.appList.count;
        for(int i=0;i<self.appList.count;i++){
            int row =i/totalCol;
            int col = i%totalCol;
            
            CGFloat x = marginX +(marginX+viewW)*col;
            CGFloat y =startY+ marginY +(marginY+viewH)*row;
            
            AppInfoUIView *view = [AppInfoUIView getXibViewWithDict:self.appList[i]];
            view.frame =CGRectMake(x, y, viewW, viewH);
            
            [self.view addSubview:view];
            }
        
    }
    
    @end
    

      

  • 相关阅读:
    Java中两个Long对象如何比较是否相等
    微信小程序data数组push和remove问题
    微信小程序let和var以及const有什么区别
    小程序开发之弹出框
    Windows系统下如何卸载干净mysql
    jQuery序列化表单
    ssm框架使用详解&配置两个数据源
    nginx在一个服务器上配置两个项目,并通过两个不同的域名访问
    nginx跨域设置&文件上传大小限制
    Navicat11连接oracle数据库
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4342936.html
Copyright © 2011-2022 走看看