一、UIView常見屬性
1.frame 位置和尺寸(以父控件的左上角為原點(0,0))
2.center 中點(以父控件的左上角為原點(0,0))
3.bounds 位置和尺寸(以自己的左上角為原點(0,0))
4.transform 形變屬性(縮放、旋轉)
5.backgroundColor 背景顏色
6.tag 標識(父控件可以根據這個標識找到對應的子控件,同一個父控件中的子控件tag不要一樣)
7.hidden 設置是否要隱藏
8.alpha 透明度(0~1)
9.opaque 不透明度(0~1)
10.userInteractionEnabled 能否跟用戶進行交互(YES能交互)
11.superview 父控件
12.subviews 子控件
13.contentMode 內容顯示的模式
二、UIView常見方法
1.addSubview:
添加子控件,被添加到最上面(subviews中的最后面)
2.removeFromSuperview
從父控件中移除
3.viewWithTag:
父控件可以根據這個tag標識找到對應的子控件(遍歷所有的子控件)
4.insertSubview:atIndex:
添加子控件到指定的位置
5.利用兩個類方法執行動畫
+ (void)beginAnimations:(NSString *)animationID context:(void *)context;
/* ...需要執行動畫的代碼..*/
+ (void)commitAnimations;
6.利用block執行動畫
/*
duration 動畫持續時間
animations 存放需要執行動畫的代碼
completion 存放動畫完畢后需要執行的操作代碼
*/
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
三、UIControl
1.只要繼承了UIControl,就能簡單處理一些事件(點擊事件、值改變事件)
2.繼承了UIControl的子類:
UIButton、UISlider、UISwitch、UIDatePicker等等
3.當需要監聽一個子控件事件的時候,解決步驟:
1> 先看它是否繼承自UIControl
2> 再看它內部是否有delegate屬性
4.常用屬性
1> enabled 能否處理事件,跟UIView的userInteractionEnabled屬性類似
2> contentVerticalAlignment 內容在垂直方向上的排布方式
3> contentHorizontalAlignment 內容在水平方向上的排布方式
5.常用方法
1> 添加監聽器
/*
target 監聽器對象
action 事件觸發時所調用的方法,調用target的方法
controlEvents 事件類型
*/
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
2> 刪除監聽器
// 刪除監聽器后,事件觸發時就不會再通知監聽器了,也就不會再調用target的action方法了
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
3> 獲得所有的監聽器對象
- (NSSet *)allTargets;
四、UILabel的常見屬性
1.text 所顯示的文本內容
2.textColor 文本顏色
3.font 字體
4.shadowColor 文字的陰影顏色
5.shadowOffset 陰影的偏差距離(width水平方向的偏差距離,正數右邊、height垂直方向的偏差距離,正數下邊)
6.textAlignment 設置文字的排布方式(偏左、偏右、居中)
7.numberOfLines 允許文字最多有幾行(默認是1,如果為0,自動換行)
五、UIButton
1.常見屬性
1> titleLabel 獲取內部的UILabel對象
2> imageView 獲取內部的UIImageView對象
2.常見方法
1> 設置內部UILabel顯示的文本內容
// 設置按鈕文本的時候不能 btn.titleLabel.text = @"4324324";
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
2> 設置內部UILabel的文字顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
3> 設置內部UILabel的文字陰影顏色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
4> 設置內部UIImageView的圖片
// 設置內部UIImageView的圖片不能:btn.imageView.image = [UIImage imagedName:@"0.png"];
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
5> 設置背景圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
6> 下面兩個方法需要交給子類去重寫,然后用於重新布局button。
// 返回內部UILabel的frame(位置和尺寸)
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
// 返回內部UIImageView的frame(位置和尺寸)
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
7> 下面這些方法可以獲取不同狀態下的一些屬性值
- (NSString *)titleForState:(UIControlState)state;
- (UIColor *)titleColorForState:(UIControlState)state;
- (UIColor *)titleShadowColorForState:(UIControlState)state;
- (UIImage *)imageForState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;