iOS 基礎控制項(下),ios基礎控制項
上篇介紹了UIButton、UILabel、UIImageView和UITextField,這篇就簡短一點介紹UIScrollView和UIAlertView。
UIScrollView
顧名思義也知道這個是和滾動相關的控制項,在Android開發時遇到過ScrollView,當內容的尺寸超出了螢幕範圍之後,用ScrollView則可以通過滾動的方式使得超出螢幕的那部分內容通過滾動的方式顯示出來,在Android裡面有水平的ScrollView和垂直的ScrollView,在iOS裡面就只有一個ScrollView,而且這個ScrollView的功能更大,某些功能已經超出了ScrollView的作用範圍了。下面則看一下ScrollView的一些屬性
- contentSize:CGSize類型,ScrollView的內容的實際大小;
- contentOffset:CGPoint類型,ScrollView當前滾動到的位置,以視圖的左上方來定位;
- contentInset:UIEdgeInsets類型,用於增加ScrollView內容的滾動範圍,相當於給ScrollView的四周補白;
下面則是其他屬性
- bounces:BOOL類型,是否有彈簧效果
- scrollEnabled :BOOL類型,是否能滾動
- showsHorizontalScrollIndicator:BOOL類型,是否顯示水平方向的捲軸
- showsVerticalScrollIndicator:BOOL類型,是否顯示垂直方向的捲軸
- indicatorStyle:UIScrollViewIndicatorStyle類型,設定捲軸的樣式,這個枚舉類型有三個值
UIScrollViewIndicatorStyleDefault UIScrollViewIndicatorStyleBlack UIScrollViewIndicatorStyleWhite
- dragging:BOOL類型,是否正在被拖拽
- tracking:BOOL類型,當touch後還沒有拖動的時候值是YES,否則NO
- decelerating:BOOL類型,是否正在減速
- zooming:BOOL類型, 是否正在縮放
ScrollView的最直接的功能就是滑動,但是但是它還兼備著手勢縮放(在Android中需要在ImageView中使用Matrix),還有滑動翻頁的功能(在Android中是通過ViewPager是實現),下面都通過代碼來實現,在viewDidLoad方法中加入
UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"]; self.imageView=[[UIImageView alloc]initWithImage:image]; self.imageView.tag=22; [self.scrollView addSubview:self.imageView];
單純通過上面的代碼則可以實現滑動查看一幅圖片的功能,一個UIScrollView其實也是一個容器,它裡面的子空間都是通過addSubview方法添加進去,而最重要的則是通過設定contentSize這個屬性來告訴UIScrollView究竟子控制項的尺寸有多大。
手勢縮放的樣本是在上面的基礎上再作修改,如果只需要單純地實現手勢縮放,主要是實現了UIScrollView的- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView方法,同時也要設定UIScrollView的最大最小的縮放比例:minimumZoomScale屬性和maximumZoomScale屬性,在viewDidLoad方法中加入
UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"]; self.imageView=[[UIImageView alloc]initWithImage:image]; self.imageView.tag=22; [self.scrollView addSubview:self.imageView]; self.scrollView.contentSize=imageView.image.size; self.scrollView.maximumZoomScale=5; self.scrollView.minimumZoomScale=0.2; self.scrollView.delegate=self;
在視圖控制器裡面的類中加入下面的方法
-(UIView*) viewForZoomingInScrollView:(UIScrollView*)scrollView{ return self.imageView;}
如果要額外加一些手勢則需要使用UITapGestureRecognizer,在viewDidLoad中加入
UITapGestureRecognizer *twoFingerTapRecongizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped)]; twoFingerTapRecongizer.numberOfTapsRequired=1; twoFingerTapRecongizer.numberOfTouchesRequired=2; [self.scrollView addGestureRecognizer:twoFingerTapRecongizer];
再定義下面這個方法
-(void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer{ CGFloat newZoomScale=self.scrollView.zoomScale/1.5f; newZoomScale =MAX(newZoomScale,self.scrollView.minimumZoomScale); [self.scrollView setZoomScale:newZoomScale animated:true];}
接下來說的是翻頁功能,要想翻頁功能最好是結合另一個控制項UIPageControl,關於這個控制項有幾個屬性要列舉一下
- numberOfPages : 總頁數
- currentPage : 當前的頁碼
- hidesForSinglePage : 當只有一頁的時候,是否要隱藏視圖監聽
要讓UIScrollView進入翻頁模式則需要設定pagingEnabled屬性,把它設定成True。代碼如下,在viewDidLoad中加入
1 CGFloat w=self.view.frame.size.width; 2 CGFloat h=self.view.frame.size.height; 3 for(int i=0;i<3;i++) 4 { 5 UIImageView *imageView=[[UIImageView alloc]init]; 6 imageView.frame=CGRectMake(i*w, 0, w, h); 7 imageView.image=[UIImage imageNamed:@"African Daisy.gif"]; 8 9 [self.scrollView addSubview:imageView];10 }11 12 self.scrollView.contentSize=CGSizeMake( 3*w, 0);13 self.scrollView.showsHorizontalScrollIndicator=false;14 self.scrollView.pagingEnabled=true;15 self.scrollView.delegate=self;16 17 UIPageControl *pagecontrol=[[UIPageControl alloc] init];18 pagecontrol.center=CGPointMake(w*0.5,h-20);19 pagecontrol.bounds=CGRectMake(0, 0, 150, 50);20 pagecontrol.numberOfPages=3;21 22 pagecontrol.pageIndicatorTintColor=[UIColor grayColor];23 pagecontrol.currentPageIndicatorTintColor=[UIColor whiteColor];24 25 pagecontrol.enabled=false;26 [self.view addSubview:pagecontrol];27 _pageControl=pagecontrol;28 29 -(void) scrollViewDidScroll:(UIScrollView *)scrollView30 {31 int page=scrollView.contentOffset.x/scrollView.frame.size.width;32 _pageControl.currentPage=page;33 }UIAlertView
UIAlertView是訊息彈窗,它的作用與效果不用多說了,要使用UIAlertView的就需要讓控制器實現UIAlertViewDelegate協議。有用到的屬性如下
- title:訊息框的標題
- message:訊息框的內容
- numberOfButtons:按鈕總數
- cancelButtonTitle:取消按鈕的標題
- cancelButtonIndex:取消按鈕的索引
- firstOtherButtonIndex:第一個其他類型按鈕的索引
- visible:訊息框可視
- alertViewStyle:UIAlertViewStyle類型,是訊息框的類型,它是一個枚舉類型,它的值如下
- UIAlertViewStyleDefault 只彈資訊和按鈕
- UIAlertViewStyleSecureTextInput 有一個textfield加密框
- UIAlertViewStylePlainTextInput 有一個不加密的textfield
- UIAlertViewStyleLoginAndPasswordInput 有兩個textfield,Login和password
對於上面提到的按鈕索引,是取消按鈕和其他按鈕組成的一個集合,其中第一個一般是Cancel按鈕,它的索引是0,如果Cancel按鈕沒有設定,則它會是-1,其他則按添加的順序。在iOS的訊息框中,只有Cancel有特有名稱,其他按鈕則沒有特有名稱,都統稱為OtherButton。
訊息框的其中一個建構函式如下
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OtherBtn1",@"OtherBtn2",@"OtherBtn3",nil];
上面otherButtonTitles參數可以添加多個按鈕。添加按鈕除了在建構函式裡面弄之外,還可以通過方法添加。如果不需要Cancel按鈕,則給參數傳一個nil值
[alert addButtonWithTitle:@"addButton"];
讓訊息框顯示則需要調用下面的方法
[alert show];
訊息框還有一系列的事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //這個事件在訊息框上面的按鈕被點擊之後觸發,通過buttonIndex來確定究竟是哪個按鈕被點擊,然後採取相應的動作}
還有其他6個事件方法
1 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 2 { 3 //AlertView已經消失時執行的事件 4 } 5 6 -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 7 { 8 //ALertView即將消失時的事件 9 }10 11 -(void)alertViewCancel:(UIAlertView *)alertView12 {13 //AlertView的取消按鈕的事件,但這個事件一直我沒有觸發過14 }15 16 -(void)didPresentAlertView:(UIAlertView *)alertView17 {18 //AlertView已經顯示時的事件19 }20 21 -(void)willPresentAlertView:(UIAlertView *)alertView22 {23 //AlertView即將顯示時24 }25 26 -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView27 {28 //第一個觸發的事件29 }
對於上面一系列的事件大體可以分為兩類,顯示的事件,他們觸發順序如下
alertViewShouldEnableFirstOtherButton——>willPresentAlertView——>didPresentAlertView
另一類是消失的事件,是在點擊後才會觸發
clickedButtonAtIndex——>(如果會觸發視圖取消,則會調用alertViewCancel)willDismissWithButtonIndex——>didDismissWithButtonIndex