一、UIView
1、UIView的常見的屬性
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
@property(nonatomic) CGAffineTransform transform;
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
補充:
// 視圖的內容模式
@property(nonatomic) UIViewContentMode contentMode;
一般選作:UIViewContentModeCenter,圖片的大小保持原來的size
* frame和bounds,center的區別:*
* frame:以父控件的左上角爲座標原點,可以確定控件的位置(origin)和大小(size)
* bounds:以自己的左上角爲座標原點,可以確定控件的大小(size)
* center:可以確定控件的位置
* 注意:OC不允許修改對象結構體屬性成員
2、transform的屬性
利用transform可以修改控件的位置縮放旋轉
1> 創建一個transform屬性
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty) ;
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
2> 在transform的基礎上進行疊加
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);
3> 清空之前設置的transform屬性
view.transform = CGAffineTransformIdentity;
補充:座標轉換
CGPoint *newpoint = [view1 convertPoint:<#(CGPoint)#> toView:view2];
座標從view1轉換到view2
//CGRect *newRect = [view1 convertPoint:<#(CGPoint)#> fromView:view2];
//座標從view2轉換到view1
CGPoint是frame前面是subview
CGPoint是bounds前面是自身的view
3.UIView的常見方法
- (void)addSubview:(UIView*)view;
- (void)removeFromSuperView;
- (UIView*)viewWithTag:(NSInteger)tag;
1> 加餐(監聽添加子控件的過程)
// 添加子控件
- (void)didAddSubview:(UIView *)subview;
// 將要移除子控件
- (void)willRemoveSubview:(UIView *)subview;
// 將要添加到父控件
- (void)willMoveToSuperview:(UIView *)newSuperview;
// 添加到控件
- (void)didMoveToSuperview;
4.UIView實現簡單動畫
兩種方式實現動畫
1> 頭尾式
[UIView beginAnimations:nil context:nil];
// 需要執行的動畫
[UIView commitAnimations];
2> Block式
[UIView animateWithDuration:0.5 animations:^{
// 需要執行的動畫的代碼
}];
**
二、UIButton
**
- 特點:顯示圖片,顯示文字,響應監聽事件的點擊
- 四種狀態:
1> normal--> UIControlStateNormal
2> highlighted-->UIControlStateHighlighted
3> selected-->UIControlStateSelected
4> disabled-->UIControlStateDisabled
1.常見屬性
@property(nonatomic,readonly,retain) NSString *currentTitle;
@property(nonatomic,readonly,retain) UIColor *currentTitleColor;
@property(nonatomic,readonly,retain) UIImage *currentImage;
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage;
2.常見屬性設置
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
btn.titleLabel.font = [UIFont systemFontOfSize:13];
3.屬性的獲得
- (NSString *)titleForState:(UIControlState)state;
- (UIColor *)titleColorForState:(UIControlState)state;
- (UIImage *)imageForState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;
4.手動創建UIButton的代碼
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forstate:UIControlStateNormal];
[btn setTitle:@"點我啊" forstate:UIControlStateNormal];
[btn settitleColor:[UIColor redColor] forstate:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIcontrolEventTouchUpInside];
5.UIControl 控件的佈局
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
三、UIImageView
1.幀動畫相關屬性和方法
1> 屬性
// 序列幀圖片數組
@property(nonatomic,copy) NSArray *animationImages;
// 幀動畫持續的時間
@property(nonatomic)NSTimeInterval animationDuration;
// 幀動畫執行的次數
@property(nonatomic)NSInteger animationRepeatCount;
2> 方法
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
2.UIImagede的2種加載方式:
1> 有緩存(程序所佔的內存會一直停留在程序中)
+ (UIImage*)imageNamed:(NSString*)name;
2> 無緩存(圖片所佔內存會在一些特定的操作後被清除)
+ (UIImage*)imageWithContentsOfFile:(NSString*)path;
- (id)initWithContentsOfFile:(NSString*)path;
補充:
// 渲染模式,可以更改原圖片的顏色
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
四、UILabel
1.屬性
text font textColor textAlignment
2.UIFont的設置方法
// 系統默認的字體
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
// 粗體
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
// 斜體
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;
五、UIAlertViewt提示框
控制器UIViewController遵守UIAlertView的代理協議
<UIAlertViewDelegate>
1.創建
[[[UIAlertView alloc] initWithTitle:@"標題" message:@"消息" delegate:self cancelButtonTitle:@"取消按鈕的標題" otherButtonTitles:@"其他按鈕的標題1",@"其他按鈕標題2" nil] show];
2.實現代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{}
六、UIActionSheet
1.控制器UIViewController遵守UIActionSheet的代理協議
<UIActionSheetDelegate>
2.創建
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"確定要註銷?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:nil, nil];
3.展示
[sheet showInView:self.view];
4.實現代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{}
七、UITextField
1.通過UITextField的代理方法能夠監聽鍵盤最右下角按鈕的點擊
1> 成爲UITextField的代理
self.textField.delegate = self;
2> 遵守UITextFieldDelegate協議,實現代理方法
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
3> 在UITextField的左邊放一個view(用於鍵盤的輸入框)
self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,8,0)];
self.textField.leftViewMode = UITextFieldViewModeAlways;
2.通過監聽UITextField來判斷文本框是否有輸入
// 監聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField];
// 監聽到有輸入時執行的方法
- (void)textChange
{
}
// 移除監聽器
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserve:self];
}
3.通過添加事件(UITextField也是繼承自UIControl的)
[self.accountField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
八、鍵盤的彈出和退去
1.鍵盤退去
1> 註銷響應者
[self.textField resignFirstResponder];
2> 整個控制器的view都不能進行鍵盤輸入
[self.view endEditing:YES];
2.鍵盤彈出
[self.textField becomeFirstResponder];
九、補充:
1.JPG和PNG圖片格式的比較
JPG:壓縮比比較高,通常用於照片,網頁,屬於有損壓縮,解壓時,對CPU的消耗大
PNG:壓縮比較高,無損壓縮,解壓效率高,推薦使用
2.weak和strong的使用
1> 爲什麼UI控件使用weak:
UI控件要添加到UIViewController的_view屬性中(addSubview:方法),其中addSubview:操作就是強引用
所以沒有必要在UIViewController屬性中把UI控件再次設置爲strong強引用了
2> delegate代理爲什麼是weak:
UIViewController控制器中強引用的UI控件,它的代理一般設置爲控制器本身UIViewController,如果把delegate代理也設置爲strong 就是循環引用了,會造成內存泄露。