上篇介紹了UIButton、UILabel、UIImageView和UITextField,這篇就簡(jiǎn)短一點(diǎn)介紹UIScrollView和UIAlertView。
顧名思義也知道這個(gè)是和滾動(dòng)相關(guān)的控件,在Android開發(fā)時(shí)遇到過ScrollView,當(dāng)內(nèi)容的尺寸超出了屏幕范圍之后,用ScrollView則可以通過滾動(dòng)的方式使得超出屏幕的那部分內(nèi)容通過滾動(dòng)的方式顯示出來,在Android里面有水平的ScrollView和垂直的ScrollView,在iOS里面就只有一個(gè)ScrollView,而且這個(gè)ScrollView的功能更大,某些功能已經(jīng)超出了ScrollView的作用范圍了。下面則看一下ScrollView的一些屬性
下面則是其他屬性
UIScrollViewIndicatorStyleDefault UIScrollViewIndicatorStyleBlack UIScrollViewIndicatorStyleWhite
ScrollView的最直接的功能就是滑動(dòng),但是但是它還兼?zhèn)渲謩?shì)縮放(在Android中需要在ImageView中使用Matrix),還有滑動(dòng)翻頁的功能(在Android中是通過ViewPager是實(shí)現(xiàn)),下面都通過代碼來實(shí)現(xiàn),在viewDidLoad方法中加入
UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"]; self.imageView=[[UIImageView alloc]initWithImage:image]; self.imageView.tag=22; [self.scrollView addSubview:self.imageView];
單純通過上面的代碼則可以實(shí)現(xiàn)滑動(dòng)查看一幅圖片的功能,一個(gè)UIScrollView其實(shí)也是一個(gè)容器,它里面的子空間都是通過addSubview方法添加進(jìn)去,而最重要的則是通過設(shè)置contentSize這個(gè)屬性來告訴UIScrollView究竟子控件的尺寸有多大。
手勢(shì)縮放的示例是在上面的基礎(chǔ)上再作修改,如果只需要單純地實(shí)現(xiàn)手勢(shì)縮放,主要是實(shí)現(xiàn)了UIScrollView的- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView方法,同時(shí)也要設(shè)置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;}
如果要額外加一些手勢(shì)則需要使用UITapGestureRecognizer,在viewDidLoad中加入
UITapGestureRecognizer *twoFingerTaPRecongizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped)]; twoFingerTapRecongizer.numberOfTapsRequired=1; twoFingerTapRecongizer.numberOfTouchesRequired=2; [self.scrollView addGestureRecognizer:twoFingerTapRecongizer];
再定義下面這個(gè)方法
-(void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer{ CGFloat newZoomScale=self.scrollView.zoomScale/1.5f; newZoomScale =MAX(newZoomScale,self.scrollView.minimumZoomScale); [self.scrollView setZoomScale:newZoomScale animated:true];}
接下來說的是翻頁功能,要想翻頁功能最好是結(jié)合另一個(gè)控件UipageControl,關(guān)于這個(gè)控件有幾個(gè)屬性要列舉一下
要讓UIScrollView進(jìn)入翻頁模式則需要設(shè)置pagingEnabled屬性,把它設(shè)置成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的就需要讓控制器實(shí)現(xiàn)UIAlertViewDelegate協(xié)議。有用到的屬性如下
對(duì)于上面提到的按鈕索引,是取消按鈕和其他按鈕組成的一個(gè)集合,其中第一個(gè)一般是Cancel按鈕,它的索引是0,如果Cancel按鈕沒有設(shè)置,則它會(huì)是-1,其他則按添加的順序。在iOS的消息框中,只有Cancel有特有名稱,其他按鈕則沒有特有名稱,都統(tǒng)稱為OtherButton。
消息框的其中一個(gè)構(gòu)造函數(shù)如下
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OtherBtn1",@"OtherBtn2",@"OtherBtn3",nil];
上面otherButtonTitles參數(shù)可以添加多個(gè)按鈕。添加按鈕除了在構(gòu)造函數(shù)里面弄之外,還可以通過方法添加。如果不需要Cancel按鈕,則給參數(shù)傳一個(gè)nil值
[alert addButtonWithTitle:@"addButton"];
讓消息框顯示則需要調(diào)用下面的方法
[alert show];
消息框還有一系列的事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //這個(gè)事件在消息框上面的按鈕被點(diǎn)擊之后觸發(fā),通過buttonIndex來確定究竟是哪個(gè)按鈕被點(diǎn)擊,然后采取相應(yīng)的動(dòng)作}
還有其他6個(gè)事件方法
1 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 2 { 3 //AlertView已經(jīng)消失時(shí)執(zhí)行的事件 4 } 5 6 -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 7 { 8 //ALertView即將消失時(shí)的事件 9 }10 11 -(void)alertViewCancel:(UIAlertView *)alertView12 {13 //AlertView的取消按鈕的事件,但這個(gè)事件一直我沒有觸發(fā)過14 }15 16 -(void)didPresentAlertView:(UIAlertView *)alertView17 {18 //AlertView已經(jīng)顯示時(shí)的事件19 }20 21 -(void)willPresentAlertView:(UIAlertView *)alertView22 {23 //AlertView即將顯示時(shí)24 }25 26 -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView27 {28 //第一個(gè)觸發(fā)的事件29 }
對(duì)于上面一系列的事件大體可以分為兩類,顯示的事件,他們觸發(fā)順序如下
alertViewShouldEnableFirstOtherButton——>willPresentAlertView——>didPresentAlertView
另一類是消失的事件,是在點(diǎn)擊后才會(huì)觸發(fā)
clickedButtonAtIndex——>(如果會(huì)觸發(fā)視圖取消,則會(huì)調(diào)用alertViewCancel)willDismissWithButtonIndex——>didDismissWithButtonIndex
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注