簡單說一下什么是UIView,直白的說就是在iPhone中班利,你看到的、摸到的都是UIView榨呆。比如:按鈕罗标、圖片、文字等积蜻。UIView是所有控件的父控件闯割,他擁有許多的子控件,接下來通過一張圖片來描述一下它的繼承體系結(jié)構(gòu)竿拆。
*UIView常見的屬性和方法
UIView常見的屬性有很多宙拉,這里只介紹一下最常用的四個(gè)
@property (nonatomic) NSInteger tag; //用于表示控件的ID,父控件可以通過tag找到對應(yīng)的字控件丙笋,定義控件的唯一標(biāo)識
@property (nonatomic) CGRect bounds谢澈;//用于表示控件所在矩形框的位置和尺寸(以自己左上角為坐標(biāo)原點(diǎn))
@property (nonatomic) CGRect center;//控件中心位置(以父控件左上角為坐標(biāo)原點(diǎn))
@property (nonatomic) CGRect frame;//控件所在矩形框在父類控件中 ?的位置和尺寸(以父控件左上角為坐標(biāo)原點(diǎn))
UIView常見的方法
- (void) addSubView: (UIView *) view; //添加子控件
- (void) removeFromSuperview; //從父類控件中移除
- (nullable _kindof UIView *) viewwithTag: (NSInteger)tag;// 根據(jù)tag標(biāo)識找到對應(yīng)的控件
UIView - Lable
常見屬性(table)
@property(nullable, nonatomic,copy) ? NSString ? ? ? ? ? *text; ? ? ? ? ? ?// default is nil
@property(null_resettable, nonatomic,strong) UIFont ? ? ?*font; ? ? ? ? ? ?// default is nil (system font 17 plain)
@property(null_resettable, nonatomic,strong) UIColor ? ? *textColor; ? ? ? // default is nil (text draws black)
@property(nullable, nonatomic,strong) UIColor ? ? ? ? ? ?*shadowColor; ? ? // default is nil (no shadow)
@property(nonatomic) ? ? ? ?CGSize ? ? ? ? ? ? shadowOffset; ? ?// default is CGSizeMake(0, -1) -- a top shadow
@property(nonatomic) ? ? ? ?NSTextAlignment ? ?textAlignment; ? // default is NSTextAlignmentLeft
@property(nonatomic) ? ? ? ?NSLineBreakMode ? ?lineBreakMode; ? // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
UIView- UIImage View
UIImage View用于播放幀動畫的相關(guān)方法
- (void)startAnimating; ?//開始播放動畫
- (void)stopAnimating; ?//停止播放動畫
- (void)isAnimating; ?//判斷是否在播放動畫
UIImage View相關(guān)屬性
@property (nullable, nonatomic, strong) UIImage *image; // default is nil
@property (nullable, nonatomic, strong) UIImage *highlightedImage NS_AVAILABLE_IOS(3_0); // default is nil
@property (nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is NO
@property (nonatomic, getter=isHighlighted) BOOL highlighted NS_AVAILABLE_IOS(3_0); // default is NO//
@property (nullable, nonatomic, copy) NSArray*animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
@property (nullable, nonatomic, copy) NSArray*highlightedAnimationImages NS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
@property (nonatomic) NSTimeInterval animationDuration; ? ? ? ? // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
@property (nonatomic) NSInteger ? ? ?animationRepeatCount; ? ? ?// 0 means infinite (default is 0)
*實(shí)戰(zhàn)演練(動畫湯姆貓)
先看一下動畫效果
先將所用到的資源放到相應(yīng)的文件夾下
我們此次要向故事板中拖拽一個(gè)image view控件,把尺寸調(diào)到和故事板一樣大御板,再戳拽三個(gè)button(可以用shife + alt左擊長按拖拽復(fù)制)設(shè)置一下相應(yīng)的屬性锥忿。
拖線關(guān)聯(lián)后
.m文件 - 三個(gè)button方法(使用了封裝)
- (IBAction)drink {
[self startAnimating:81 picName:@"drink"];
}
// 放P
- (IBAction)fart {
[self startAnimating:28 picName:@"fart"];
}
// 敲頭
- (IBAction)knockout {
[self startAnimating:81 picName:@"knockout"];
}
執(zhí)行動畫的方法
- (void)startAnimating:(int)count picName:(NSString *)picName
{
// 如果當(dāng)前圖片框正在執(zhí)行動畫, 那么直接return, 什么都不做(沒有開啟一個(gè)新動畫)
if (self.imgViewCat.isAnimating) {
return;
}
// 1. 把圖片加載到數(shù)組中
// 0.動態(tài)加載圖片到一個(gè)NSArray中
NSMutableArray *arrayM = [NSMutableArray array];
for (int i = 0; i < count; i++) {
// 拼接圖片名稱
NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg", picName, i];
// 根據(jù)圖片名稱加載圖片
// 通過imageNamed: 這種方式加載圖片, 加載好的圖片會一直保存寫在內(nèi)存中, 不會釋放.這樣下次如果再使用同樣的圖片的時(shí)候就不需要再重新加載了, 因?yàn)閮?nèi)存里面已經(jīng)有了。缺點(diǎn)就是: 如果加載了大量的圖片, 那么這些圖片會一直保留在內(nèi)存中怠肋,導(dǎo)致應(yīng)用程序占用內(nèi)存過大(這就叫緩存)
// 使用這種方式加載圖片, 加載起來的圖片即便沒有強(qiáng)類型指針引用也不會銷毀(會被緩存)
//UIImage *imgCat = [UIImage imageNamed:imgName];
// 使用下面這種方式加載的圖片, 只要沒有強(qiáng)類型指針引用就會被銷毀了
// 解決: 換一種加載圖片的方式, 不要使用緩存
// 獲取圖片的完成的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
// 這里的參數(shù)不能再傳遞圖片名稱了, 這里需要傳遞一個(gè)圖片的完整路徑
UIImage *imgCat = [UIImage imageWithContentsOfFile:path];
// 把圖片加載到數(shù)組中
[arrayM addObject:imgCat];
}
// 2. 設(shè)置UIImageView的animationImages屬性為對應(yīng)的圖片集合
self.imgViewCat.animationImages = arrayM;
// 3. 動畫持續(xù)時(shí)間
self.imgViewCat.animationDuration = self.imgViewCat.animationImages.count * 0.1;
// 4. 重復(fù)次數(shù)
self.imgViewCat.animationRepeatCount = 1;
// 5. 啟動動畫
[self.imgViewCat startAnimating];
// 清空圖片集合
// 這樣些寫的問題是, 當(dāng)動畫啟動以后, 動畫還沒開始執(zhí)行, 就已經(jīng)讓圖片集合清空了, 也就是說self.imgViewCat.animationImages 里面已經(jīng)沒有圖片了, 所以動畫就不執(zhí)行了敬鬓。
//self.imgViewCat.animationImages = nil;
// self.imgViewCat.animationImages = nil; 需要延遲一段時(shí)間執(zhí)行, 當(dāng)動畫執(zhí)行完畢以后再清空這些圖片
//[self.imgViewCat setAnimationImages:nil];
// 設(shè)置圖片框在調(diào)用setAnimationImages:nil方法的時(shí)候延遲執(zhí)行
[self.imgViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imgViewCat.animationImages.count * 0.1];
}