IOS開發學習實例之三---應用管理器

這是個非常簡單的小例子。主要涉及知識點如下:

1. 按照plist中的字典創建模型

2. 使用Xib創建appView中的子控件, 並顯示內容

3. 主控制器3步業務邏輯

1)懶加載創建appView數組

2)九宮格算法佈局控件

3)控制器中實現appView 中下載按鈕點擊的代理方法


實例樣子:



1. 按照plist中的字典創建模型

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface AppInfo : NSObject

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
//there is only a getter if 'readonly' is assigned
@property (nonatomic,strong,readonly) UIImage *image;

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

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


@end
這裏使用了KVC,直接從字典生成想要的對象

setValuesForKeysWithDictionary

#import "AppInfo.h"

@implementation AppInfo
{
    UIImage *_image;
}


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

    if(self = [super init]){
        // input data using kvc
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

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

- (UIImage *)image{
    if(!_image){
        _image = [UIImage imageNamed:self.icon];
    }
    return _image;
}

@end

懶加載創建appView數組

//Using lazy-load to create array
//override the getter of appViews
- (NSArray *)appViews{
    if (!_appViews) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];
        
        for (NSDictionary *dict in array) {
            //create model instance
            AppInfo *appInfo = [AppInfo appInfoWithDict:dict];
            //create own view
            AppView *appView = [AppView appView];
            appView.appInfo = appInfo;
            appView.delegate = self;
            
            //add own view to mutable array
            [arrayM addObject:appView];
        }
        _appViews = [arrayM copy];
    }
    return _appViews;
}

九宮格計算

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CGFloat margin = (kScreenW - kTotalCol * kAppViewW)/(kTotalCol + 1);
    
    for (int i=0; i < self.appViews.count; i++) {
        AppView *appView = self.appViews[i];
        
        //calcute the row and column number
        int col = i % kTotalCol;
        int row = i / kTotalCol;
        //calcute the center point of every views
        CGFloat centerX = (margin + kAppViewW * 0.5) + (margin + kAppViewW) * col;
        CGFloat centerY = (margin + kAppViewH * 0.5) + (margin + kAppViewH) * row;
        
        appView.center = CGPointMake(centerX, centerY);
        [self.view addSubview:appView];
        
    }
    
}

控制器中實現appView 中下載按鈕點擊的代理方法

#pragma mark - AppViewDelegate

- (void)downloadClickWithBtn:(UIButton *)btn{
    
    //get the name of app
    //Force convert
    AppView *appView = (AppView *)btn.superview;
    NSString *appName = appView.appInfo.name;
    
    //generate the busy indicator
    UIActivityIndicatorView *busy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    busy.frame = self.view.bounds;
    
    //busy.hidesWhenStopped = YES; //by default, it is YES
    //busy.backgroundColor = [UIColor greenColor];
    
    [busy startAnimating];
    [self.view addSubview:busy];
    
    // generate a lable for downloading above on the busy indicator
    UILabel *downloadLable = [[UILabel alloc] init];
    downloadLable.frame = CGRectMake(0, kScreenH * 0.5 + 10, kScreenW, 20);
    downloadLable.textColor = [UIColor whiteColor];
    downloadLable.textAlignment = NSTextAlignmentCenter;
    downloadLable.text = [NSString stringWithFormat:@"%@正在下載。。。", appName];
    downloadLable.font = [UIFont systemFontOfSize:15.0];
    downloadLable.backgroundColor = [UIColor blackColor];
    downloadLable.alpha = 0.5;
    
    [busy addSubview:downloadLable];
    
    //using GCD to
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [busy stopAnimating];
        
        //the alert when download is done
        UILabel *noteLable = [[UILabel alloc] init];
        noteLable.frame = CGRectMake(0, kScreenH * 0.5 + 20, kScreenW, 30);
        noteLable.textColor = [UIColor whiteColor];
        noteLable.textAlignment = NSTextAlignmentCenter;
        noteLable.text = [NSString stringWithFormat:@"%@下載完成", appName];
        noteLable.font = [UIFont systemFontOfSize:15.0];
        noteLable.backgroundColor = [UIColor blackColor];
        noteLable.alpha = 1;
        
        [self.view addSubview:noteLable];
        
        //animation by block
        [UIView animateWithDuration:2.0 animations:^{
            //do the animation
            noteLable.alpha = 0;
        } completion:^(BOOL finished) {
            //what we should do after the animation
            btn.enabled = NO;
            
            [btn setTitle:@"已下載" forState:UIControlStateDisabled];
            
            [noteLable removeFromSuperview];
        }];
    });
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章