您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UIView 常見屬性與方法整理

編輯:iOS 開發綜合

以下透過實例代碼,詳細介紹 iOS 中 UIView 的常見屬性與方法:

 UIView : UIResponder /** 通過一個 frame 來初始化一個 UI 控件 */ - (id)initWithFrame:(CGRect)frame; // YES:能夠與用戶進行交互 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // 控件的一個標記(父控件可以通過 tag 找到對應的子控件) @property(nonatomic) NSInteger tag; // 圖層(可用來設置圓角、陰影效果) @property(nonatomic,readonly,retain) CALayer *layer; @interface UIView(UIViewGeometry) // 位置和尺寸(以父控件左上角為原點 (0, 0)) @property(nonatomic) CGRect frame; // 位置和尺寸(以自己的左上角為原點 (0, 0)) @property(nonatomic) CGRect bounds; // 中點(以父控件左上角為原點 (0, 0)) @property(nonatomic) CGPoint center; // 形變屬性(平移、縮放、旋轉) @property(nonatomic) CGAffineTransform transform; // YES:支持多點觸摸 @property(nonatomic,getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled; @end @interface UIView(UIViewHierarchy) // 父控件 @property(nonatomic,readonly) UIView *superview; // 子控件(新添加的默認顯示在最上層) @property(nonatomic,readonly,copy) NSArray *subviews; // 獲得當前控件所在的 window @property(nonatomic,readonly) UIWindow *window; // 從父控件中移除一個控件 - (void)removeFromSuperview; // 添加子控件到指定位置 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; // 交換子控件位置 - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; // 添加子控件 - (void)addSubview:(UIView *)view; // 添加子控件(擋在 siblingSubview 下面) - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; // 添加子控件(蓋在 siblingSubview 上面) - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; // 將子控件拉到最上層顯示 - (void)bringSubviewToFront:(UIView *)view; // 將子控件拉到最下層顯示 - (void)sendSubviewToBack:(UIView *)view; /** 系統自動調用(留給子類實現) **/ - (void)didAddSubview:(UIView *)subview; - (void)willRemoveSubview:(UIView *)subview; - (void)willMoveToSuperview:(UIView *)newSuperview; - (void)didMoveToSuperview; - (void)willMoveToWindow:(UIWindow *)newWindow; - (void)didMoveToWindow; // 是否為 view 的後代 - (BOOL)isDescendantOfView:(UIView *)view; // 通過 tag 獲得對應的子控件(含後代) - (UIView *)viewWithTag:(NSInteger)tag; /** 系統自動調用(留給子類實現) **/ // 控件 frame 改變時調用,一般在這裡布局子控件,重寫後須調用 [super layoutSubviews]; - (void)layoutSubviews; @end @interface UIView(UIViewRendering) // YES:超出控件邊框的內容都剪掉 @property(nonatomic) BOOL clipsToBounds; // 背景色 @property(nonatomic,copy) UIColor *backgroundColor; // 透明度 (0.0~1.0) @property(nonatomic) CGFloat alpha; // YES:不透明 @property(nonatomic,getter=isOpaque) BOOL opaque; // YES:隱藏 @property(nonatomic,getter=isHidden) BOOL hidden; // 內容模式 @property(nonatomic) UIViewContentMode contentMode; @end // 動畫 @interface UIView(UIViewAnimationWithBlocks) + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations; + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; @end

以上便是 iOS 中 UIView 常見屬性與方法的整理,涵蓋層級管理、幾何定位、渲染與動畫等常用 API,希望對大家有所幫助。若有任何疑問,歡迎留言。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved