#import "SaleView.h" #import "UIImageView+AFNetworking.h" #define SALEVIEWWIDTH self.frame.size.width #define SALEVIEWHIGHT self.frame.size.height @interface SaleView()<UIScrollViewDelegate> { //滾動(dòng)視圖 UIScrollView * scrollView; //頁(yè)碼指示器 UIPageControl * pageControl; NSString*imageStr; UIImageView*imageView; int timecount; } @end @implementation SaleView -(void)prepareScrollView; {
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,SALEVIEWWIDTH,SALEVIEWHIGHT)]; //設(shè)置默認(rèn)的第一張圖片imageStr=self.dataArr[0][@"ImgPath"]; imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, SALEVIEWWIDTH,SALEVIEWHIGHT)]; [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil]; [scrollView addSubview:imageView];
//設(shè)置滾動(dòng)區(qū)域 scrollView.contentSize = CGSizeMake(self.frame.size.width,SALEVIEWHIGHT); //設(shè)置分頁(yè),默認(rèn)為NO scrollView.pagingEnabled = YES; //關(guān)閉回彈 scrollView.bounces = NO; //關(guān)閉滾動(dòng)條 scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator=NO; //設(shè)置代理 scrollView.delegate = self; //將滾動(dòng)視圖加到視圖上 [self addSubview:scrollView];
//這個(gè)控件叫做面碼指示器,用來(lái)提示頁(yè)碼 pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width/2, 120,self.frame.size.width/2-80,30)]; //設(shè)置總頁(yè)碼數(shù),也就告訴指示器有多少頁(yè),顯示多少個(gè)點(diǎn) pageControl.numberOfPages =self.dataArr.count; //設(shè)置當(dāng)前頁(yè) pageControl.currentPage =0; //設(shè)置指示器的顏色 pageControl.pageIndicatorTintColor = [UIColor grayColor]; pageControl.currentPageIndicatorTintColor = [UIColor redColor]; //給頁(yè)碼指示器加響應(yīng)事件 [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; [self addSubview:pageControl];
//使用NSTimer實(shí)現(xiàn)定時(shí)觸發(fā)滾動(dòng)控件滾動(dòng)的動(dòng)作。 timecount=0; [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES]; } //滾圖的動(dòng)畫(huà)效果 -(void)pageTurn:(UIPageControl *)aPageControl{ long int whichPage = aPageControl.currentPage; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [scrollView setContentOffset:CGPointMake(self.frame.size.width * whichPage, 0.0f) animated:YES]; [UIView commitAnimations]; } //定時(shí)滾動(dòng) -(void)scrollTimer { self.flag=timecount+1; timecount++; if ( timecount==self.dataArr.count) { timecount = 0; } imageStr=_dataArr[timecount][@"ImgPath"]; [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil]; [scrollView reloadInputViews]; pageControl.currentPage=timecount; } |
|
來(lái)自: 久辰堡 > 《iOS開(kāi)發(fā)總結(jié)》