iOS開發基礎篇-Button基礎

1、簡單介紹

   UIButton 的功能:響應用戶操做、顯示文字、顯示圖片、調整內部圖片和文字的位置。html

 

2、 UIButton 的狀態

   UIControlStateNormal :普通狀態,爲默認狀況。atom

   UIControlStateHighlighted :高亮狀態,按鈕被按下去的時候(手指還未鬆開)。spa

   UIControlStateDisabled :不可用狀態,此時 enabled 屬性爲 NO ,按鈕不能夠被點擊。code

 

3、注意點

  若干個空間共用一段代碼,經過使用 tag 屬性。orm

 

4、實例演示

  新建一個Single View Application,向Main.storyboard中添加按鈕,以下所示:htm

  其中上移按鈕的tag爲1,下移按鈕的tag爲2,左移按鈕的tag爲3,右移按鈕的tag爲4,都與事件move創建鏈接。放大按鈕的tag爲0,縮小按鈕的tag爲1,都與事件zoom創建鏈接。對象

 

 //ViewComtroller.m
1
#import "ViewController.h" 2 3 typedef NS_ENUM(NSUInteger, kMovingDir) 4 { 5 kMovingDirTop = 1, 6 kMovingDirButtom, 7 kMovingDirLeft, 8 kMovingDirRight, 9 }; 10 11 CGFloat const kMovingDelta = 50.0; //移動係數 12 CGFloat const kZoomingDelta = 50.0; //放大縮小系數 13 14 @interface ViewController () 15 @property (weak, nonatomic) IBOutlet UIButton *headImageView; //可移動的圖片按鈕 16 @end
 //ViewController.m
1
- (IBAction)move:(id)sender { 2 UIButton *button = (UIButton *)sender; 3 4 CGPoint p = self.headImageView.center; 5 switch (button.tag) { 6 case kMovingDirTop: p.y -= kMovingDelta; break; //往上移動 7 case kMovingDirButtom: p.y += kMovingDelta; break; //往下移動 8 case kMovingDirLeft: p.x -= kMovingDelta; break; //往左移動 9 case kMovingDirRight: p.x += kMovingDelta; break; //往右移動 10 } 11 12 [UIView beginAnimations:nil context:nil]; 13 [UIView setAnimationDuration:2.0]; 14 self.headImageView.center = p; 15 [UIView commitAnimations]; 16 } 17 18 - (IBAction)zoom:(id)sender { 19 UIButton *button = (UIButton *)sender; 20 21 CGRect rect = self.headImageView.bounds; 22 if (button.tag) { 23 rect.size.width -= kZoomingDelta; 24 rect.size.height -= kZoomingDelta; 25 } else { 26 rect.size.width += kZoomingDelta; 27 rect.size.height += kZoomingDelta; 28 } 29 30 [UIView beginAnimations:nil context:nil]; 31 [UIView setAnimationDuration:2.0]; 32 self.headImageView.bounds = rect; 33 [UIView commitAnimations]; 34 }

 

5、補充

一、修改對象的結構體成員

  在OC中,不容許直接修改對象的結構體屬性的成員,可是容許修改對象的結構體屬性。blog

  修改結構體屬性的成員方法以下:事件

  1)使用臨時變量記錄對象的結構體屬性,圖片

  2)修改臨時變量的屬性;

  3)將臨時變量從新設置給對象的結構體屬性。

 

參考博客:iOS開發UI篇—Button基礎

本部分iOS代碼:http://pan.baidu.com/s/1o6VmJ4Q

相關文章
相關標籤/搜索