UIView之常用方法
- 將一個視圖添加為子視圖,并使之在最上面顯示
-(void)addSubView:(UIView *)view;
- 將指定子視圖移動到頂部
-(void)bringSubViewToFront:(UIView *)view;
- 將指定之視圖放到最下面
-(void)sendSubViewToBack:(UIView *)view;
- 將指定視圖添加到subviews數組的index位置
-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- 將指定視圖添加到指定子視圖下面
-(void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- 將指定視圖添加到指定子視圖上面
-(void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
- 交換subviews數組中兩個位置的子視圖
-(void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- 從父視圖中移除
-(void)removeFromSuperview;
- 根據tag值獲取對應的子孫控件
-(UIView *)viewWithTag:(NSInteger)tag;
- 將視圖中點從自己的坐標系轉換到指定的視圖坐標系中
-(CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- 將指定視圖中坐標系內的某點轉換到自己的坐標系中
-(CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
- 將視圖中矩形區域從自己的坐標系轉換到指定的視圖坐標系中
-(CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- 將指定視圖中坐標系內的矩形區域轉換到自己的坐標系中
-(CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
- 刷新視圖,調用后自動調用drawRect:(CGRect)rect
-(void)setNeedsDisplay;
- 繼承自UIResponder用于響應觸摸事件的方法
1.- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
2.- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
3.- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
4.- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
以上方法需要自定義view重寫,若需要對觸摸點到判斷(使用)那么重寫方法時,在方法體內首先獲取觸摸點:
1.UITouch *touch = [touches anyObject];
2.CGPoint point = [touch locationInView:self];
- 動畫
1.// 首先需要設置動畫頭,告訴編譯器下面是動畫
2.[UIView beginAnimations:nil context:nil];
3.// 再設置動畫執行的配置、動畫
4.[UIView setAnimationDuration:0.5];
5.[UIView setAnimationRepeatCount:2];
6.[UIView setAnimationDelay:3.0];
7.// balabala需要執行的動畫
8. ................
9.// 最后提交動畫
10.[UIView commitAnimations];
- 使用block設置動畫
- 方法一
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
- 方法二
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
- 方法三
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
- 方法一