UI基础3

图片浏览器

从NSBuilder中读取文件
加载图片:


UI基础3_第1张图片
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pageLabel;
@property (weak, nonatomic) IBOutlet UIButton *preButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;

@end

@implementation ViewController{
    NSArray *_arr;
    NSInteger _pageCount;
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    _pageCount = 0;
    [self showUI];
}
-(void)showUI{
     NSInteger pageTotal = _arr.count;
    
    _preButton.enabled = _pageCount != 0;
    _nextButton.enabled = _pageCount != pageTotal -1;
    
    //展示页数
    NSString *page = [NSString stringWithFormat:@"%zd / %zd",_pageCount + 1,pageTotal];
    _pageLabel.text = page;
    
    //展示图片
    NSDictionary *dict = _arr[_pageCount];
    NSString *icon = dict[@"icon"];
    NSInteger count = [dict[@"count"] intValue];
   
    NSMutableArray *arrM = [NSMutableArray array];
    for (int i = 1; i <= count; i++) {
        NSString *name = [icon stringByAppendingFormat:@"%03d.png",i];
//       UIImage *img = [UIImage imageNamed:name];
        NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
        UIImage *img = [UIImage imageWithContentsOfFile:path];
        [arrM addObject:img];
    }
    UIImage *img = [UIImage animatedImageWithImages:arrM duration:count*0.06];
    _imgView.image = img;
    
    //描述
     NSString *desc = dict[@"desc"];
    _descLabel.text = desc;
}

-(void)loadData{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"images" withExtension:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfURL:url];
    _arr = arr;
}


- (IBAction)clickPreButton:(id)sender {
    _pageCount--;
    [self showUI];
}
- (IBAction)clickNextButton:(id)sender {
    _pageCount++;
    [self showUI];
}

@end
应用管理
UI基础3_第2张图片
#import "Model.h"
#import "AppView.h"
@interface ViewController ()

@end

@implementation ViewController{
    NSArray *_arrlist;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
}

-(void)setupUI{
    for (int i = 0; i < _arrlist.count; i++) {
        [self addView:_arrlist[i] :i];
    }
}
-(void)addView:(Model *)model :(NSInteger)num{
    //设置图片信息
   UINib *nib =  [UINib nibWithNibName:@"AppView" bundle:nil];
    AppView *v = [nib instantiateWithOwner:nil options:nil].lastObject;
    v.imgView.image = [UIImage imageNamed:model.icon];
    v.nameLabel.text = model.name;
    //设置位置
    NSInteger width = (self.view.bounds.size.width - v.bounds.size.width * 3)/4;
    NSInteger row = num/3;
    NSInteger col = num%3;
    
    CGFloat x = width + col *(width + v.frame.size.width);
    CGFloat y = width + row *(width + v.frame.size.height);
    v.frame = CGRectMake(x, y, v.frame.size.width,v.frame.size.height);
    
    [self.view addSubview:v];
}

-(void)loadData{
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"apps" withExtension:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfURL:url];
    NSMutableArray *arrM = [NSMutableArray array];
    for (int i = 0; i < arr.count; i++) {
        Model *model = [Model modelWithDict:arr[i]];
        [arrM addObject:model];
    }
    _arrlist = arrM;
}
@end

你可能感兴趣的:(UI基础3)