iOS学习之个人笔记(应用管理)
2015-11-29 12:03
513 查看
最终效果:
步骤:
1)创建xib展示每个应用
2)由于用到字典转模型,所以根据plist文件创建模型类(模型里的属性根据plist里的键值对创建)然后通过懒加载方式从plist文件里获取数据到模型中,然后把模型数据返回给数组。
3)设置应用属性,获取数据到模型中创建view,解析模型数据然后设置给view
4)最后把view添加到界面上。
步骤:
1)创建xib展示每个应用
2)由于用到字典转模型,所以根据plist文件创建模型类(模型里的属性根据plist里的键值对创建)然后通过懒加载方式从plist文件里获取数据到模型中,然后把模型数据返回给数组。
3)设置应用属性,获取数据到模型中创建view,解析模型数据然后设置给view
4)最后把view添加到界面上。
//这个是模型的.h文件 #import <Foundation/Foundation.h> @interface YZApp : NSObject @property (nonatomic,copy) NSString *name; @property (nonatomic,copy) NSString *icon; -(instancetype) initWithDict: (NSDictionary *) dict; +(instancetype)appWithDict: (NSDictionary *) dict; @end
//这是模型的.m文件 #import "YZApp.h" @implementation YZApp //封装字典转模型方法 -(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]; } @end
//这是controller类 #import "ViewController.h" #import "YZApp.h" #import "YZAppView.h" @interface ViewController () @property(nonatomic,strong)NSArray *apps; @end @implementation ViewController #pragma mark 重写apps的get方法(懒加载) -(NSArray *)apps{ if (_apps == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; NSArray *arrDict = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *model = [NSMutableArray array]; for (NSDictionary *dict in arrDict) { //通过字典转模型方法把数据赋值给模型 YZApp *app = [[YZApp alloc] initWithDict:dict]; [model addObject:app]; } _apps = model; } return _apps; } - (void)viewDidLoad { [super viewDidLoad]; //设置应用属性 CGFloat viewWidth = self.view.frame.size.width; CGFloat appW = 75; CGFloat appH = 90; //第一行距离顶端的距离 CGFloat marginTop =30; //每行应用数 int num = 3; CGFloat marginX =(viewWidth - num * appW)/(num + 1); CGFloat marginY = marginX; for (int i=0; i<self.apps.count; i++) { //获取数据 YZApp *appModel = self.apps[i]; //通过appView方法创建view YZAppView *appView = [YZAppView appView]; int colldx = i % num; int rowldx =i / num; CGFloat appX = marginX + colldx*(appW+marginX); CGFloat appY = marginTop +rowldx*(appH+marginY); appView.frame = CGRectMake(appX, appY, appW, appH); //通过重写模型属性的set方法把模型数据设置给自定义的view appView.appModel = appModel; [self.view addSubview:appView]; } } @end
//这是appView类由于要引用xib里的控件,所以创建appView类来进行操作 #import <UIKit/UIKit.h> @class App; @interface AppView : UIView @property (nonatomic,strong)App *appModel; +(instancetype) appView; @end
#import "AppView.h" #import "App.h" @interface AppView() @property (weak, nonatomic) IBOutlet UIImageView *imgViewIcon; @property (weak, nonatomic) IBOutlet UILabel *lblName; @property (weak, nonatomic) IBOutlet UIButton *btnDownload; - (IBAction)btnDownloadClick:(UIButton *)sender; @end @implementation AppView //重写appModel属性的set方法 -(void)setAppModel:(App *)appModel{ _appModel = appModel; //解析模型数据,把模型数据赋值给UIView中的子控件 self.imgViewIcon.image = [UIImage imageNamed:appModel.icon]; self.lblName.text = appModel.name; } +(instancetype)appView{ //1)通过xib创建每个应用UIView //获取应用根目录 NSBundle *rootBundle =[NSBundle mainBundle]; //在应用根目录下取得xib文件 AppView *appView = [[rootBundle loadNibNamed:@"AppView" owner:nil options:nil] lastObject]; return appView; } - (IBAction)btnDownloadClick:(UIButton *)sender { //1)禁用当前点击按钮 sender.enabled = NO; //2)弹出消息框 UILabel *lblMsg = [[UILabel alloc] init]; //设置Label属性 lblMsg.text = @"正在下载..."; lblMsg.backgroundColor = [UIColor blackColor]; CGFloat lblW = 200; CGFloat lblH = 30; CGFloat lblX = (self.superview.frame.size.width - lblW)/2; CGFloat lblY = (self.superview.frame.size.height - lblH)/2; lblMsg.textColor = [UIColor redColor]; lblMsg.textAlignment = NSTextAlignmentCenter; lblMsg.frame = CGRectMake(lblX, lblY, lblW, lblH); //设置透明度 lblMsg.alpha = 0; //设置圆角 lblMsg.layer.cornerRadius = 10; lblMsg.layer.masksToBounds = YES; //开启动画执行1.5秒 [UIView animateWithDuration:0.6 animations:^{ lblDownload.alpha = 0.6; } completion:^(BOOL finished) { if (finished) { //延迟(delay)1.0秒,然后执行动画1.5秒,执行动画 [UIView animateWithDuration:0.7 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{ lblDownload.alpha = 0; } completion:^(BOOL finished) { if (finished) { //如果动画执行完毕,把这个lbl从view中移除 [lblDownload removeFromSuperview]; } }]; } }]; [self.superview addSubview:lblMsg]; } @end
应用管理素材
相关文章推荐
- iOS开发实战tips--让Xcode的控制台支持NSArray和NSDictionary的中文输出
- iOS开发:bitcode介绍和使用cocoapods出现“target overrides the `OTHER_LDFLAGS`……”的解决方案
- iOS进阶之runtime(2)--KVO简单模拟实现
- iOS编程习惯
- (NO.00004)iOS实现打砖块游戏(十):砖块!更多的砖块!
- (NO.00004)iOS实现打砖块游戏(十):砖块!更多的砖块!
- (NO.00004)iOS实现打砖块游戏(十):砖块!更多的砖块!
- (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
- (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
- (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
- (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
- (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
- (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
- (NO.00004)iOS实现打砖块游戏(七):关卡类的实现
- (NO.00004)iOS实现打砖块游戏(七):关卡类的实现
- (NO.00004)iOS实现打砖块游戏(七):关卡类的实现
- (NO.00004)iOS实现打砖块游戏(六):反弹棒类
- (NO.00004)iOS实现打砖块游戏(六):反弹棒类
- (NO.00004)iOS实现打砖块游戏(六):反弹棒类
- iOS MRC手动内存管理 心得体会