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