你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS一個滾動視圖的小例子(代碼)

IOS一個滾動視圖的小例子(代碼)

編輯:關於IOS

程序的大部分

@interface BIDViewController : UIViewController

//可滾動頁面

@property (retain, nonatomic) UIScrollView *scrollView;

//頁點

@property (retain, nonatomic) UIPageControl *pageControl;

//保存圖片的數組

@property (retain, nonatomic) NSMutableArray *imageArray;

@end

@implementation BIDViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//創建可滑動視圖

UIScrollView *tempView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

self.scrollView = tempView;

[tempView release];

//創建頁點

UIPageControl *tempPage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 420, 320, 36)];

[tempPage setBackgroundColor:[UIColor clearColor]];

[tempPage setAlpha:1];

self.pageControl = tempPage;

//[tempPage release];

//初始化數組,將圖片壓入數組

self.imageArray = [NSMutableArray arrayWithObjects:

[UIImage imageNamed:@"html5.png"],

[UIImage imageNamed:@"jQuery.png"],

[UIImage imageNamed:@"iphone1.png"],

[UIImage imageNamed:@"iphone2.png"], nil];

//將滾動視圖和翻頁視圖添加到視圖中

[self.view addSubview:self.scrollView];

[self.view addSubview:self.pageControl];

self.view.backgroundColor = [UIColor whiteColor];

[self createsCrollView:nil];

}

#pragma ------創建視圖區域----------

//創建滾動視圖區域

- (void)createsCrollView:(id)sender

{

//設置委托

self.scrollView.delegate = self;

//將是否取消對內容視圖觸摸的反應設為NO

self.scrollView.canCancelContentTouches = NO;

//設置滾動條類型

self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

//是否可以縮放

self.scrollView.scrollEnabled = YES;

//是否可以進行頁面切換

self.scrollView.pagingEnabled = YES;

NSUInteger page = 0;//為了記錄頁數

int originX = 0;

//填充滑動視圖內容

for (UIImage *image in self.imageArray) {

//首先創建一個視圖

UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectZero];

//為這個圖片設置適當位置

CGRect rect ;

rect.origin.x = originX;

rect.origin.y = 0;

rect.size.width = self.scrollView.frame.size.width;

rect.size.height = self.scrollView.frame.size.height;

imageview.frame = rect;

//設置圖片為背景

imageview.image = image;

//添加到視圖中

[self.scrollView addSubview:imageview];

[imageview release];

//確定下張圖片開始的x坐標

originX += self.scrollView.frame.size.width;

//記錄頁數

page++;

}

//設置總頁數

self.pageControl.numberOfPages = page;

//設置默認頁為首頁

self.pageControl.currentPage = 0;

self.pageControl.tag = 100;

//為頁數控制關聯方法

[self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

//顯示適當區域

[self.scrollView setContentSize:CGSizeMake(originX, self.scrollView.frame.size.height)];

}

//點擊小點切換頁面

- (void)changePage:(id)sender

{

//創建一個區域

CGRect rect;

rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;

rect.origin.y = 0;

rect.size.width = self.scrollView.frame.size.width;

rect.size.height = self.scrollView.frame.size.height;

//設置滾動視圖的可視區域

[self.scrollView scrollRectToVisible:rect animated:YES];

}

//重寫滑動結束後的反應,就是把pageConteol的當前頁跟隨滑動而動

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//取滾動視圖區域的寬

float orginX = self.scrollView.frame.size.width;

//利用當前視圖顯示區域到view.frame的偏移量,來換算獲取當前處在第幾頁

int page = ((self.scrollView.contentOffset.x - orginX))/orginX +1;

//當前頁改為這一頁。

self.pageControl.currentPage = page;

}

自己寫的代碼,經檢測未出錯 ――LC

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved