iOS-汤姆猫项目总结
功能分析
點擊對應(yīng)的按鈕后,讓湯姆貓展現(xiàn)對應(yīng)的動畫
步驟分析
1、搭建UI界面
2、監(jiān)聽按鈕點擊
3、根據(jù)點擊的按鈕執(zhí)行對應(yīng)的動畫
知識點:
1、UIImageView幀動畫的使用
2、UIImage的2種加載方式
3、重復(fù)代碼的封裝抽取
4、文檔注釋的寫法
UIImageView幀動畫相關(guān)屬性和方法
@property(nonatomic,copy) NSArray *animationImages;
需要播放的序列幀圖片數(shù)組(里面都是UIImage對象,會按順序顯示里面的圖片)
@property(nonatomic) NSTimeInterval animationDuration;
幀動畫的持續(xù)時間
@property(nonatomic) NSInteger animationRepeatCount;
幀動畫的執(zhí)行次數(shù)(默認(rèn)是無限循環(huán))
- (void)startAnimating;
開始執(zhí)行幀動畫
- (void)stopAnimating;
停止執(zhí)行幀動畫
- (BOOL)isAnimating;
是否正在執(zhí)行幀動畫
UIImage的2種加載方式
方式一:有緩存(圖片所占用的內(nèi)存會一直停留在程序中)
+ (UIImage )imageNamed:(NSString )name;
name是圖片的文件名
方式二:無緩存(圖片所占用的內(nèi)存會在一些特定操作后被清除)
+ (UIImage )imageWithContentsOfFile:(NSString )path
- (id)initWithContentsOfFile:(NSString *)path;
path是圖片的全路徑
方式二對于內(nèi)存更優(yōu)化
重復(fù)代碼的封裝抽取:
1、當(dāng)一份代碼重復(fù)出現(xiàn)在程序的多處地方,就會造成程序又臭又長,當(dāng)這份代碼的結(jié)構(gòu)要修改時,每一處出現(xiàn)這份代碼的地方都得修改,導(dǎo)致程序的擴展性很差
2、因此,要將重復(fù)出現(xiàn)的代碼抽取到某個方法中,在需要這份代碼的地方調(diào)用方法即可
抽取代碼的思路
1.將相同的代碼放到一個方法中
2。將不同的值當(dāng)做方法參數(shù)傳進來
代碼簡摘:(不拖控件,使用純代碼大家界面)
#import "HMViewController.h"@interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *tom;@end@implementation HMViewController /**重構(gòu)代碼:1、將重復(fù)的代碼復(fù)制到新方法中2、根據(jù)需要調(diào)整方法關(guān)于圖像實例化UIImage UIImageViewimageName: 系統(tǒng)推薦使用,但是圖像實例化之后的釋放由系統(tǒng)負(fù)責(zé)如果要自己釋放圖片不能使用imageName方法!UIImage *image = [UIImage imageNamed:imageName];取而代之的方法:[UIImage imageWithContentsOfFile:<#(NSString *)#>]注意:一遇到ContentsOfFile則必須使用全路徑!!提示:如果放在Images.xcassets 中的圖片(存放經(jīng)常使用的圖片),不能使用imageWithContentsOfFile:臨時使用的大圖片放在Supporting Files*/ -(void)tomAnimationWithName:(NSString *)name count:(NSInteger)count{//判斷是否在動畫if([self.tom isAnimating]) return;//動畫圖片數(shù)組NSMutableArray *imageArray = [NSMutableArray array];int i;for (i = 0 ; i< count ; i++) {NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];//UIImage *image = [UIImage imageNamed:imageName];NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];UIImage *image = [UIImage imageWithContentsOfFile:path];[imageArray addObject:image];}//設(shè)置動畫數(shù)組self.tom.animationImages = imageArray;//重復(fù)一次self.tom.animationRepeatCount = 1;//動畫時長self.tom.animationDuration = self.tom.animationImages.count * 0.075;//開始動畫[self.tom startAnimating];// //點擊事件結(jié)束以后釋放數(shù)組 // self.tom.animationImages = nil;[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];}//currentTitle 可以去除按鈕當(dāng)前標(biāo)題文字 -(IBAction)tomAction:(UIButton *)button{[self tomAnimationWithName:button.currentTitle count:button.tag]; }- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end運行結(jié)果截圖:
總結(jié)
以上是生活随笔為你收集整理的iOS-汤姆猫项目总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线密码破解medusa
- 下一篇: cad拉伸怎么用_【cad比例缩放教程】