標籤:style blog color io os ar for sp div
ScrollViewController.h
#import <UIKit/UIKit.h>@interface ScrollViewController : UIViewController<UIScrollViewDelegate,UIPageViewControllerDelegate>{ UIScrollView *_scrollView; UIPageControl*_pageControl;}@end
ScrollViewController.m
#import "ScrollViewController.h"@interface ScrollViewController ()@end@implementation ScrollViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self;}- (void)viewDidLoad{ [super viewDidLoad]; //設定ScrollView的整體觸摸與顯示地區 //注意 寬 高不要超過 320X480 //否則會出現無法滾動的情況 _scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320,120)] autorelease]; //設定ScrollView滾動內容的地區 //它通常是需要大於ScrollerView的顯示地區的 //這樣才有必要在ScrollerView中滾動它 [_scrollView setContentSize:CGSizeMake(320 * 5, 120)]; //開啟滾動分頁功能,如果不需要這個功能關閉即可 [_scrollView setPagingEnabled:YES]; //隱藏橫向與縱向的捲軸 [_scrollView setShowsVerticalScrollIndicator:NO]; [_scrollView setShowsHorizontalScrollIndicator:NO]; //在本類中代理scrollView的整體事件 [_scrollView setDelegate:self]; //如果你開啟橫向或縱向的捲軸,這裡可以設定捲軸的風格 // UIScrollViewIndicatorStyleDefault, 預設風格 // UIScrollViewIndicatorStyleBlack, 黑色風格 // UIScrollViewIndicatorStyleWhite 白色風格 //[_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack] for (int i =0; i<5; i++) { //在這裡給每一個ScrollView添加一個圖片 和一個按鈕 UIImageView *imageView= [[[UIImageView alloc] initWithFrame:CGRectMake(i * 320,0,320,120)] autorelease]; [imageView setImage:[UIImage imageNamed:@"image.png"]]; UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(i * 320, 10, 100, 30); [button setTitle:@"這是一個按鈕" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; //把每頁需要顯示的VIEW添加進ScrollerView中 [_scrollView addSubview:imageView]; [_scrollView addSubview:button]; } //整體再將ScrollerView顯示在視窗中 [self.view addSubview:_scrollView]; //頁面控制小工具 //它會在底部繪製小圓點標誌當前顯示頁面 _pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width, 20)]autorelease]; //設定頁面的數量 [_pageControl setNumberOfPages:5]; //監聽頁面是否發生改變 [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:_pageControl]; }- (void)changePage:(id)sender{ //得到當前頁面的ID //int page = [sender currentPage]; //在這裡寫你需要執行的代碼 //......}//手指離開螢幕後ScrollView還會繼續滾動一段時間只到停止- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"結束滾動後緩衝滾動徹底結束時調用");}-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ NSLog(@"結束滾動後開始緩衝滾動時調用");}-(void)scrollViewDidScroll:(UIScrollView*)scrollView{ //頁面滾動時調用,設定當前頁面的ID [_pageControl setCurrentPage:fabs(scrollView.contentOffset.x/self.view.frame.size.width)]; NSLog(@"視圖滾動中X軸座標%f",scrollView.contentOffset.x); NSLog(@"視圖滾動中X軸座標%f",scrollView.contentOffset.y);}-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{ NSLog(@"滾動視圖開始滾動,它只調用一次");}-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"滾動視圖結束滾動,它只調用一次");}-(void)buttonClick{ NSLog(@"按鈕點擊了");}- (void)viewDidUnload{ [super viewDidUnload];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end
[IOS UIScrollView+PageControl]資訊展示橫幅