漸變動畫、圖檔拉伸、copy、按鈕、kvc、kvo
标簽(空格分隔): ios基礎
漸變動畫
- 方式1:頭尾式
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:];
/* 需要執行動畫的代碼 */
[UIView commitAnimations];
- 方式2:block
[UIView animateWithDuration: delay: options:kNilOptions animations:^{
/* 需要執行動畫的代碼 */
} completion:nil]
// 1s後,再執行動畫(動畫持續2s)
按鈕
- 自定義按鈕:調整内部子控件的frame
- 方式1:實作titleRectForContentRect:和imageRectForContentRect:方法,分别傳回titleLabel和imageView的frame
-
方式2:在layoutSubviews方法中設定
-内邊距
// 設定按鈕内容的内邊距(影響到imageView和titleLabel)
@property(nonatomic) UIEdgeInsets contentEdgeInsets;
// 設定titleLabel的内邊距(影響到titleLabel)
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
// 設定imageView的内邊距(影響到imageView)
@property(nonatomic) UIEdgeInsets imageEdgeInsets;
圖檔拉伸
- ios5之前
// 隻拉伸中間的1x1區域
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- ios5開始
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
拷貝
-實作拷貝的方法有2個
-copy:傳回不可變副本
- mutableCopy:傳回可變副本
- 普通對象實作拷貝的步驟
- 遵守NSCopying協定
- 實作-copyWithZone:方法
- 建立新對象
- 給新對象的屬性指派
KVC
- 全稱:Key Value Coding(鍵值編碼)
- 指派
// 能修改私有成員變量
- (void)setValue:(id)value forKey:(NSString *)key;
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
- 取值
//能取得私有成員變量的值(**這個很有用**)
- (id)valueForKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
- 特殊的用法,包括計算books數組的count,屬性最大值和平均值
NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
KVO
- 全稱:Key Value Observing(鍵值監聽)
- 作用:監聽模型的屬性值改變
- 步驟
- 添加監聽器
// 利用b對象來監聽a對象name屬性的改變 [a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
- 取消監聽
[a removeObserver:b forKeyPath:@"name"];
- 在監聽器中實作監聽方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@ %@ %@ %@", object, keyPath, change, context); }
- 添加監聽器