应用管理第一部分
纯代码写出来

//
// ViewController.m
// 08-复习应用程序管理
//
// Created by lujun on 2021/5/20.
//
#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@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 arrayWithContentsOfFile:path];
}
return _appList;
}
- (void)viewDidLoad {
[super viewDidLoad];
int totalCol = 3;//3列
CGFloat viewW = 80,viewH=90;
CGFloat marginX = (kScreenW - totalCol * viewW) / (totalCol+1);
CGFloat marginY = 43.5;
CGFloat startY = 120;
for(int i = 0; i < self.appList.count; i++){
NSDictionary *dict = self.appList[i];
int col = i % totalCol;
int row = i / totalCol;
CGFloat x = marginX + (marginX + viewW) * col;
CGFloat y = marginY + (marginY + viewH) * row;
UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, startY + y, viewW, viewH)];
// appView.backgroundColor = [UIColor redColor];
[self.view addSubview:appView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewW, 50)];
// imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageNamed:dict[@"icon"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[appView addSubview:imageView];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, viewW, 20)];
// lbl.backgroundColor = [UIColor grayColor];
// lbl.text = @"test";
lbl.text = dict[@"name"];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:13.0];
[appView addSubview:lbl];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, 70, viewW-30, 20)];
// [btn setBackgroundColor:[UIColor blueColor]];
[btn setTitle:@"下载" forState:UIControlStateNormal];
[btn setTitle:@"下载1" forState:UIControlStateHighlighted];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
// btn.titleLabel.tintColor = [UIColor blackColor];
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
[appView addSubview:btn];
btn.tag = i;
[btn addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)downClick:(UIButton *)btn{
NSDictionary *dict = self.appList[btn.tag];
UILabel *lbl= [[UILabel alloc] initWithFrame:CGRectMake(100, kScreenH-200, 200, 50)];
lbl.backgroundColor = [UIColor lightGrayColor];
lbl.text = [NSString stringWithFormat:@"您下载了%@",dict[@"name"]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:13];
lbl.alpha = 1.0;
[self.view addSubview:lbl];
[UIView animateWithDuration:2.0 animations:^{
lbl.alpha = 0.0;
} completion:^(BOOL finished) {
[lbl removeFromSuperview];
}];
}
@end
第二部分 加入模型对象 和工厂实例方法
//
// ViewController.m
// 08-复习应用程序管理_2
//
// Created by lujun on 2021/5/21.
//
//
#import "ViewController.h"
#import "AppInfo.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property(nonatomic,strong)NSArray *appList;
@property(nonatomic,strong)AppInfo *appInfo;
@end
@implementation ViewController
- (NSArray *)appList{
if(!_appList){
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *dict in array) {
[mArray addObject:[AppInfo appInfoWithDict:dict]];
}
_appList = mArray;
}
return _appList;
}
- (void)viewDidLoad {
[super viewDidLoad];
int totalCol = 3;//3列
CGFloat viewW = 80,viewH=90;
CGFloat marginX = (kScreenW - totalCol * viewW) / (totalCol+1);
CGFloat marginY = 43.5;
CGFloat startY = 120;
for(int i = 0; i < self.appList.count; i++){
AppInfo *appInfo = self.appList[i];
int col = i % totalCol;
int row = i / totalCol;
CGFloat x = marginX + (marginX + viewW) * col;
CGFloat y = marginY + (marginY + viewH) * row;
UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, startY + y, viewW, viewH)];
[self.view addSubview:appView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewW, 50)];
imageView.image = [UIImage imageNamed:appInfo.icon];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[appView addSubview:imageView];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, viewW, 20)];
lbl.text = appInfo.name;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:13.0];
[appView addSubview:lbl];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, 70, viewW-30, 20)];
[btn setTitle:@"下载" forState:UIControlStateNormal];
[btn setTitle:@"下载1" forState:UIControlStateHighlighted];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
[appView addSubview:btn];
btn.tag = i;
[btn addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)downClick:(UIButton *)btn{
AppInfo *appInfo = self.appList[btn.tag];
UILabel *lbl= [[UILabel alloc] initWithFrame:CGRectMake(100, kScreenH-200, 200, 50)];
lbl.backgroundColor = [UIColor lightGrayColor];
lbl.text = [NSString stringWithFormat:@"您下载了%@",appInfo.name];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:13];
lbl.alpha = 1.0;
[self.view addSubview:lbl];
[UIView animateWithDuration:2.0 animations:^{
lbl.alpha = 0.0;
} completion:^(BOOL finished) {
[lbl removeFromSuperview];
}];
}
@end
//
// AppInfo.h
// 08-复习应用程序管理_2
//
// Created by lujun on 2021/5/21.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface AppInfo : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)appInfoWithDict:(NSDictionary *)dict;
@end
NS_ASSUME_NONNULL_END
具体实现
//
// AppInfo.m
// 08-复习应用程序管理_2
//
// Created by lujun on 2021/5/21.
//
#import "AppInfo.h"
@implementation AppInfo
- (instancetype)initWithDict:(NSDictionary *)dict{
self = [super init];
if (self) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)appInfoWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
第三部分 加入xib的实现,对象对象思想

//
// AppInfoView.h
// 08-复习应用程序管理加入xib_3
//
// Created by lujun on 2021/5/21.
//
#import <UIKit/UIKit.h>
@class AppInfo;
NS_ASSUME_NONNULL_BEGIN
@interface AppInfoView : UIView
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *lblText;
@property(nonatomic,strong)AppInfo *appInfo;
+(instancetype)appInfoViewWithAppInfo:(AppInfo *)appInfo;
+(instancetype)appInfoView;
@end
NS_ASSUME_NONNULL_END
//
// AppInfoView.m
// 08-复习应用程序管理加入xib_3
//
// Created by lujun on 2021/5/21.
#import "AppInfoView.h"
#import "AppInfo.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@implementation AppInfoView
- (void)setAppInfo:(AppInfo *)appInfo{
_appInfo = appInfo;
self.imageViewIcon.image = appInfo.image;
self.lblText.text = appInfo.name;
}
+ (instancetype)appInfoView{
NSArray *arr=[[NSBundle mainBundle] loadNibNamed:@"AppInfoView" owner:nil options:nil];
AppInfoView *appView = arr.firstObject;
return appView;
}
+ (instancetype)appInfoViewWithAppInfo:(AppInfo *)appInfo{
AppInfoView *view=[self appInfoView];
view.appInfo = appInfo;
return view;
}
- (IBAction)downLoadclick:(id)sender {
UILabel *lbl= [[UILabel alloc] initWithFrame:CGRectMake(100, kScreenH-200, 200, 50)];
lbl.backgroundColor = [UIColor lightGrayColor];
lbl.text = [NSString stringWithFormat:@"您下载了%@",self.appInfo.name];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:13];
lbl.alpha = 1.0;
[self.superview addSubview:lbl];
[UIView animateWithDuration:2.0 animations:^{
lbl.alpha = 0.0;
} completion:^(BOOL finished) {
[lbl removeFromSuperview];
}];
}
@end
//
// ViewController.m
// 08-复习应用程序管理加入xib_3
//
// Created by lujun on 2021/5/21.
//
#import "ViewController.h"
#import "AppInfo.h"
#import "AppInfoView.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property(nonatomic,strong)NSArray *appList;
@property(nonatomic,strong)AppInfo *appInfo;
@end
@implementation ViewController
- (NSArray *)appList{
if(!_appList){
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *dict in array) {
[mArray addObject:[AppInfo appInfoWithDict:dict]];
}
_appList = mArray;
}
return _appList;
}
- (void)viewDidLoad {
[super viewDidLoad];
int totalCol = 3;//3列
CGFloat viewW = 80,viewH=90;
CGFloat marginX = (kScreenW - totalCol * viewW) / (totalCol+1);
CGFloat marginY = 43.5;
CGFloat startY = 120;
for(int i = 0; i < self.appList.count; i++){
int col = i % totalCol;
int row = i / totalCol;
CGFloat x = marginX + (marginX + viewW) * col;
CGFloat y = marginY + (marginY + viewH) * row;
AppInfoView *appView = [AppInfoView appInfoViewWithAppInfo:self.appList[i]];
appView.frame = CGRectMake(x, startY + y, viewW, viewH);
[self.view addSubview:appView];
}
}
@end
//
// AppInfo.h
// 08-复习应用程序管理_2
//
// Created by lujun on 2021/5/21.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface AppInfo : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)appInfoWithDict:(NSDictionary *)dict;
@property(nonatomic,strong)UIImage *image;
@end
NS_ASSUME_NONNULL_END
//
// AppInfo.m
// 08-复习应用程序管理_2
//
// Created by lujun on 2021/5/21.
//
#import "AppInfo.h"
@implementation AppInfo
- (instancetype)initWithDict:(NSDictionary *)dict{
self = [super init];
if (self) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)appInfoWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
- (UIImage *)image{
if(!_image){
_image = [UIImage imageNamed:self.icon];
}
return _image;
}
@end
网友评论