1、iOS 開發之基礎控制項,ios開發基礎控制項

來源:互聯網
上載者:User

1、iOS 開發之基礎控制項,ios開發基礎控制項
一、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轉換到view1CGPoint是frame前面是subviewCGPoint是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--> UIControlStateNormal2> highlighted-->UIControlStateHighlighted3> selected-->UIControlStateSelected4> 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 就是循環參考了,會造成記憶體泄露。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.