UIView常見屬性總結


一 UIVIew 常見屬性
1.frame 位置和尺寸(以父控件的左上角為原點(0,0))
2.center 中點 (以父控件的左上角為原點(0,0))
3.bounds 位置和尺寸(以自己的左上角為原點 (0,0))
4.transform 形變屬性(縮放,旋轉)
5.backgroundColor 背景顏色
6.tag 標識(父控件可以根據這個標識找到對應的子控件,同一個父控件中的子控件不要一樣)
7. hidden 設置是否要隱藏
8.alpha 透明度(0~1);
9.opaque 不透明度(0~1);
10.userInteractionEnabled 能否跟用戶進行交互(YES 能交互)
11.superView 父控件
12.subviews 子控件
13.contentMode 內容顯示的模式 拉伸自適應


[view viewWithTag:10];
[
    btn1 9
    btn2 10
    imageView 2 10
 ]

二.UIView常見方法
1.addSubview
添加子控件,被添加到最上面(subviews中的最后面)

2.removeFromSuperview
從父控件中移除

3.viewWithTag:
父控件可以根據這個tag 標識找到對應的控件(遍歷所有的子控件)

4.insertSubview:atIndex:
添加子控件到指定的位置

5.利用兩個類方法來執行動畫的兩個方法
+(void) beginAnimations:(NSString *)animationID context:(void *)context;
/**..需要執行動畫的代碼..**/
+(void) commitAnimations;

6.利用blok 執行動畫
/*
 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 能否處理時間
2>contentVerticalAlignment 內容在垂直方向上的排布方式
3>contentHorizontalAlignment 內容在水平方向上的排布方式
5.常用方法
1> 添加監聽器
/*
 target 監聽器對象
 action 事件觸發時所調用的方法,調用target的方法
 */
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

2> 刪除監聽器
//刪除監聽器后,事件觸發時就不會再通知監聽器了,也就不會再調用target的action方法了
-(void roemoveTarget:(id)target action:](SEL)action forControlEvents:](UIControlEvents) controlEvents);

3> 獲得所有的監聽器對象
-(NSSet *) allTargets;

四,UILabel
1.常見屬性
1>text 所顯示的文本內容
2>textColor 文本顏色
3> font 字體
4> shadowColor 文字的陰影顏色
5> shadowOffset 陰影的偏差距離(width水平方向的偏差距離,height垂直方向的念頭距離,正數下邊)
6> textAlignment 設置文字的排布方法(偏左,偏右,居中).
7>numberOfLines 允許文字最多有幾行數(如果為0,自動換行).
五.UIButton
//.UISlider .UISwitch .UIDatePicker等等
1.常見屬性

1>titleLabel 獲取內部的UILabel 對象
2>imageView 獲取內部的UIImageView對象

2.常見方法
1>設置內部UILabel 顯示的文本內容
//設置按鈕文本的時候不能 btn .titleLabel.text  = @"4324324";
- (void)setTitle:(NSString *)title forState:(UIControlState)state;                     // default is nil. title is assumed to be single line

2> 設置內部UILabel的文字顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white
3>設置內部UILabel 的文字陰影顏色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
4>設置內部UIImageView的圖片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;                      // default is nil. should be same size if different for different states
5>設置內部UIImageView的圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil

6>下面兩個方法需要交給子類去重寫
//返回沒物控部UILabel的frame (位置和尺寸)
-(CGRect)titleRectForContentRect:(CGRect)contentRect;
//返回內部UIImage的尺寸和位置
-(CGRect)imageRectForContentRect:(CGRect) contentRect;

7> 下面這些方法可以獲取不同狀態下的一些屬性
- (NSString *)titleForState:(UIControlState)state;          // these getters only take a single state value
- (UIColor *)titleColorForState:(UIControlState)state;
- (UIColor *)titleShadowColorForState:(UIControlState)state;
- (UIImage *)imageForState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;
- (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM