2011-11-21 149 views
1

我想向我的滾動視圖添加頁面控件,並且遵循了大量的網絡教程,其中大部分使用與此相同的代碼tutorial.但是,一旦將代碼放入我的項目,甚至在我對代碼進行更改以嘗試使其工作的情況下,它也不會。我已經設法讓代碼在頁面控件被按下時工作,但它不適用於頁面滾動。我的問題與this類似,但答案沒有幫助。這裏是我的代碼:頁面滾動視圖滾動時控件不會更改

MainViewController.h

@interface MainViewController : UIViewController 
{ 
UIScrollView *svCollegeMain; 
UIScrollView *svCollegePage; 
UIPageControl *pcCollege; 
UIView *viewP1; 
} 

@property (nonatomic, retain) IBOutlet UIScrollView* svCollegeMain; 
@property (nonatomic, retain) IBOutlet UIScrollView* svCollegePage; 
@property (nonatomic, retain) IBOutlet UIPageControl * pcCollege; 
- (IBAction)changePage; 

@end 

和MainViewController.m

@implementation MainViewController 

@synthesize svCollegeMain, svCollegePage, pcCollege; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.svCollegeMain.contentSize = CGSizeMake(960, 332); 
self.svCollegePage.contentSize = CGSizeMake(320, 500); 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 
CGFloat pageWidth = 320; 
int page = floor((svCollegeMain.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
pcCollege.currentPage = page; 
} 

- (IBAction)changePage 
{ 
CGRect frame; 
frame.origin.x = self.svCollegeMain.frame.size.width * self.pcCollege.currentPage; 
frame.origin.y = 0; 
frame.size = self.svCollegeMain.frame.size; 
[self.svCollegeMain scrollRectToVisible:frame animated:YES]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
self.svCollegeMain = nil; 
self.svCollegePage = nil; 
self.pcCollege = nil; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

只是櫃面這使得任何區別,我的觀點闡述了觀點,那麼主滾動這個視圖中的視圖和頁面控制,主滾動視圖中的另一個視圖和滾動視圖(彼此相鄰),最後是第二個滾動視圖中的最終視圖(全部在IB中,不需要太多的代碼)以及所有內容在IB中鏈接起來。

回答

3

我注意到你的MainViewController沒有聲明自己實現UIScrollViewDelegate,所以我也假設你已經忘記將它設置爲IB的滾動視圖的委託(否則它不會編譯) 。

由於沒有定義委託,因此滾動視圖不會調用您的scrollViewDidScroll函數。

Tim

+0

Thankyou SOOO很多!每次我檢查代碼時,該行都直接從我身邊飛過。那和下面的一行都是它所花費的:svCollegeMain.delegate = self; (.m)和.h中的。謝謝 –

1

試試這個

HeaderFile:

@interface DemoPageControlViewController : UIViewController <UIScrollViewDelegate> 
{ 
IBOutlet UIScrollView *scrollView; 
IBOutlet UIPageControl *pageControl; 
BOOL pageControlUsed; 
NSMutableArray *imageArray; 
int pageNumber; 


} 


@property (nonatomic, retain) UIScrollView *scrollView; 
@property (nonatomic, retain) UIPageControl *pageControl; 
@property (nonatomic, retain) NSMutableArray *imageArray; 


- (IBAction) changePage:(id)sender; 

實現文件:

#import "DemoPageControlViewController.h" 

@implementation DemoPageControlViewController 
@synthesize pageControl, scrollView, imageArray; 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

CGRect frame; 
frame.origin.x = 0; 
frame.origin.y = 0; 
frame.size = self.scrollView.frame.size; 
scrollView.showsVerticalScrollIndicator = NO; 
scrollView.showsHorizontalScrollIndicator = NO; 

imageArray = [[NSMutableArray alloc]init]; 
[imageArray addObject:@"small_one.png"]; 
[imageArray addObject:@"small_two.png"]; 
[imageArray addObject:@"small_three.png"]; 
[imageArray addObject:@"small_four.png"]; 


// add the last image to first 
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:  [imageArray objectAtIndex:([imageArray count] -1)]]]; 
imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height); 
[self.scrollView addSubview:imageView]; 
[imageView release]; 

for(int i = 0; i < imageArray.count; i++) 
{ 
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]]; 
    imageView.frame = CGRectMake((scrollView.frame.size.width * i) + 320 , 0, scrollView.frame.size.width, scrollView.frame.size.height); 
    [self.scrollView addSubview:imageView]; 
    [imageView release]; 
} 
// add the first image to last 
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:0]]]; 
imageView.frame = CGRectMake(scrollView.frame.size.width * ([imageArray count]+1), 0, scrollView.frame.size.width, scrollView.frame.size.height); 
[self.scrollView addSubview:imageView]; 
[imageView release]; 

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([imageArray count]+ 2), self.scrollView.frame.size.height); 
[scrollView setContentOffset:CGPointMake(0, 0)]; 
[self.view addSubview:scrollView]; 
[self.scrollView scrollRectToVisible:CGRectMake(scrollView.frame.size.width,0,scrollView.frame.size.width,scrollView.frame.size.height) animated:NO]; 
} 

- (IBAction)changePage :(id)sender 
{ 
CGRect frame; 
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage ; 
frame.origin.y = 0; 
frame.size = self.scrollView.frame.size; 
[self.scrollView scrollRectToVisible:frame animated:YES]; 
} 


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
pageControlUsed = NO;    
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
//pageControlUsed = NO; 

NSLog(@"%f", self.scrollView.contentOffset.x); 

CGFloat pageWidth = self.scrollView.frame.size.width; 
//pageNumber = floor((self.scrollView.contentOffset.x - pageWidth/([imageArray count]+2))/pageWidth) + 1 ; 
pageNumber = self.scrollView.contentOffset.x/pageWidth; 

if(pageNumber == 0) 
{ 
    [self.scrollView scrollRectToVisible:CGRectMake((self.scrollView.frame.size.width * [imageArray count]), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; 
    pageNumber = [imageArray count]; 
    //self.pageControl.currentPage = pageNumber; 
} 
else if(pageNumber == ([imageArray count]+1)) 
{ 
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; 
    pageNumber = 1; 
    //self.pageControl.currentPage = pageNumber; 
} 

self.pageControl.currentPage = pageNumber - 1; 

    } 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
    { 

    } 

此代碼工作正常。試試這個