生活随笔
收集整理的這篇文章主要介紹了
IOS基础之仿酷狗音乐第1天
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
IOS基礎之仿酷狗音樂第1天
細節(jié)較多,涉及字典轉模型,tableView 的使用,模態(tài)框,自定義模態(tài),音視頻播放,全局PCH文件,xib加載,自定義 xib ,info。plist文件的加載,動畫的使用等
如果對OC不熟悉的人,照葫蘆畫瓢也不一定畫出來,到處報錯。天道酬勤,工夫和時間花到了,得心應手,就不怕報錯。
關鍵性的代碼如下,源碼在我的主頁下面。項目名稱:02-黑馬音樂第一天.zip
#import "HMMusicsViewController.h"
#import "HMMusic.h"
#import "HMMusicCell.h"
#import "HMPlayingViewController.h"@interface HMMusicsViewController
()
@property(nonatomic
,strong
)NSArray
*musics
;
@property(nonatomic
,strong
)HMPlayingViewController
*playingVc
;
@end
@implementation HMMusicsViewController
- (HMPlayingViewController
*)playingVc
{if(!_playingVc
){self.playingVc
= [[HMPlayingViewController alloc
] init
];}return _playingVc
;
}
-(NSArray
*)musics
{if(!_musics
){_musics
= [HMMusic musicWithFilename
:@"Musics.plist"];}return _musics
;
}
- (void)viewDidLoad
{[super viewDidLoad
];}
#pragma mark - Table view data source
- (NSInteger
)tableView
:(UITableView
*)tableView numberOfRowsInSection
:(NSInteger
)section
{return self.musics
.count
;
}
- (UITableViewCell
*)tableView
:(UITableView
*)tableView cellForRowAtIndexPath
:(NSIndexPath
*)indexPath
{HMMusicCell
*cell
=[HMMusicCell cellWithTableView
:tableView
];cell
.music
= self.musics
[indexPath
.row
];return cell
;
}
- (CGFloat
)tableView
:(UITableView
*)tableView heightForRowAtIndexPath
:(NSIndexPath
*)indexPath
{return 70;
}
- (void)tableView
:(UITableView
*)tableView didSelectRowAtIndexPath
:(NSIndexPath
*)indexPath
{[tableView deselectRowAtIndexPath
:indexPath animated
:YES
];[self.playingVc show
];
}
@end
#import "HMPlayingViewController.h"
@interface HMPlayingViewController
()
@end
@implementation HMPlayingViewController
- (void)viewDidLoad
{[super viewDidLoad
];
}
- (void)show
{UIWindow
*window
= [[UIApplication sharedApplication
].windows lastObject
];self.view
.frame
= window
.bounds
;[window addSubview
:self.view
];self.view
.y
= self.view
.height
;[UIView animateWithDuration
:0.4 animations
:^{self.view
.y
= 0;}];
}
@end
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface HMMusic
: NSObject
@property(nonatomic
,copy
)NSString
*name
;
@property(nonatomic
,copy
)NSString
*icon
;
@property(nonatomic
,copy
)NSString
*filename
;
@property(nonatomic
,copy
)NSString
*lrcname
;
@property(nonatomic
,copy
)NSString
*singer
;
@property(nonatomic
,copy
)NSString
*singerIcon
;
-(instancetype
)initWithDict
:(NSDictionary
*)dict
;
+(instancetype
)musicWithDict
:(NSDictionary
*)dict
;
+(NSArray
*)musicWithFilename
:(NSString
*)filename
;
@end
NS_ASSUME_NONNULL_END
#import "HMMusic.h"
@implementation HMMusic
- (instancetype
)initWithDict
:(NSDictionary
*)dict
{self = [super init
];if(self){[self setValuesForKeysWithDictionary
:dict
];}return self;
}
+ (instancetype
)musicWithDict
:(NSDictionary
*)dict
{return [[self alloc
] initWithDict
:dict
];
}
+(NSArray
*)musicWithFilename
:(NSString
*)filename
{NSString
*path
= [[NSBundle mainBundle
] pathForResource
:filename ofType
:nil
];NSArray
*array
= [NSArray arrayWithContentsOfFile
:path
];NSMutableArray
*mArray
= [NSMutableArray array
];for(NSDictionary
*dict
in array
){[mArray addObject
:[self musicWithDict
:dict
]];}return mArray
;
}
@end
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class HMMusic
;
@interface HMMusicCell
: UITableViewCell
@property(nonatomic
,strong
)HMMusic
*music
;
+(instancetype
)cellWithTableView
:(UITableView
*)tableView
;
@end
NS_ASSUME_NONNULL_END
#import "HMMusicCell.h"
#import "HMMusic.h"
#import "Colours.h"
#import "UIImage+MJ.h"
@implementation HMMusicCell
+ (instancetype
)cellWithTableView
:(UITableView
*)tableView
{static NSString
*ID
=@"music";HMMusicCell
*cell
= [tableView dequeueReusableCellWithIdentifier
:ID
];if(cell
==nil
){cell
= [[HMMusicCell alloc
] initWithStyle
:UITableViewCellStyleSubtitle reuseIdentifier
:ID
];}return cell
;
}
- (void)setMusic
:(HMMusic
*)music
{_music
= music
;self.textLabel
.text
= music
.name
;self.detailTextLabel
.text
= music
.singer
;self.imageView
.image
= [UIImage circleImageWithName
:music
.singerIcon borderWidth
:2 borderColor
:[UIColor pinkColor
]];
}
@end
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結
以上是生活随笔為你收集整理的IOS基础之仿酷狗音乐第1天的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。