轉載自:http://blog.csdn.net/z343929897/article/details/7974753
按照趙總要求,在首頁上邊需要加廣告條,本來以為挺複雜的,原來挺簡單
在類的申明檔案(.h)裡添加對Page控制器的申明:
@property (strong, nonatomic) IBOutlet UIPageControl *page;@property (strong, nonatomic) IBOutlet UIScrollView *imageScrollView;
然後在實現檔案(.m)裡添加 對page對象的
@synthesize page;@synthesize imageScrollView;
實現page對象的自動存取器。 改寫viewDidLoad方法如下
- (void)viewDidLoad{ [super viewDidLoad]; //這裡定義了滾動視圖的大小,是否支援翻頁,是否顯示水平滾動標示,委派物件是哪個 imageScrollView.contentSize = CGSizeMake(PAGENUM * 320.0f, imageScrollView.frame.size.height); imageScrollView.pagingEnabled = YES; imageScrollView.showsHorizontalScrollIndicator = NO; imageScrollView.delegate = self; //這裡為滾動視圖添加了子視圖,為了能添加後續操作,我這裡定義的子視圖是按鍵UIButton for (int i = 0; i < PAGENUM; i++) { NSString * fileName = [NSString stringWithFormat:@"%d.jpg",i+1]; UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * 320.0f, 0.0f, 320.0f, 218.0f)]; [imageButton setBackgroundImage:[UIImage imageNamed:fileName] forState:UIControlStateNormal]; imageButton.tag = 900 + i; [imageScrollView addSubview:imageButton]; } //定義PageController 設定總頁數,當前頁,定義當控制項被使用者操作時,要觸發的動作。 �0�2page.numberOfPages = PAGENUM; page.currentPage = 0; [page addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //使用NSTimer實現定時觸發滾動控制項滾動的動作。 timeCount = 0; [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];}
增加兩個翻頁動畫和自動翻頁的函數
//滾圖的動畫效果-(void)pageTurn:(UIPageControl *)aPageControl{ int whichPage = aPageControl.currentPage; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [imageScrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES]; [UIView commitAnimations];}//定時滾動-(void)scrollTimer{ timeCount ++; if (timeCount == PAGENUM) { timeCount = 0; }
//chenyong 在這個地方加上self.pageController.currentpage = timeCount; 即可改變pageCon了,切記不要加到timeCount++的下邊
[imageScrollView scrollRectToVisible:CGRectMake(timeCount * 320.0, 65.0, 320.0, 218.0) animated:YES];}