ScrollView: PageControl dot doesnt change with the

2019-06-04 08:19发布

问题:

I've written an iPhone App with a TableView and a detailView which contains a ScrollView with 2 pages (both containing an image) and a pageControl displaying 2 white dots.

It al seems to work perfect. The view scrolls to the left and show the next page (just as it should) Except for the little white dots (PageControl dots) that are under the scrollView. If I press the little dots: the page scrolls and the second dot becomes highlighted (perfect!). But if I slide the first page (manualy with my thumb) to the next page the white dot doesnt change with it. (It should highlight the second dot).

Hope somebody can help me. I've read the code 10 times and searched through al sample code I coould find.

Here's my Code:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

<UIScrollViewDelegate>
    {

 NSString *selectedImage1;
 NSString *selectedImage2;
 NSNumber *selectedNpages;

 IBOutlet UIScrollView* scrollView;
 IBOutlet UIPageControl* pageControl;

 BOOL pageControlIsChangingPage;
    }

@property (nonatomic, retain) NSString *selectedImage1;    
@property (nonatomic, retain) NSString *selectedImage2;    
@property (nonatomic, retain) NSNumber *selectedNpages;    
@property (nonatomic, retain) UIView *scrollView;    
@property (nonatomic, retain) UIPageControl* pageControl;

-(IBAction)changePage:(id)sender;    
-(void)setupPage;

 @end



#import "DetailViewController.h"

@implementation DetailViewController


@synthesize selectedImage1;
@synthesize selectedImage2;
@synthesize selectedNpages;
@synthesize scrollView;
@synthesize pageControl;


#pragma mark -
#pragma mark UIView boilerplate
- (void)viewDidLoad 
{
 [self setupPage];
 [super viewDidLoad];
}


- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload 
{ 
 [scrollView release];
 [pageControl release];
}



#pragma mark -
#pragma mark The Guts
- (void)setupPage
{

 scrollView.scrollsToTop = NO;
 scrollView.delegate = self; 
 scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
 scrollView.clipsToBounds = YES;
 scrollView.scrollEnabled = YES;
 scrollView.pagingEnabled = YES;
 scrollView.showsHorizontalScrollIndicator = NO;
 scrollView.showsVerticalScrollIndicator = NO;
 [scrollView setCanCancelContentTouches:NO];
 pageControl.currentPage = 0;

 NSString *image1a = selectedImage1;
 NSString *image2a = selectedImage2;

 CGFloat cx = 0;


 //FIRST PAGE

  // Image

 UIImage *bimage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle]  pathForResource:image1a ofType:@"jpg"]];
 UIImageView *bimageView = [[UIImageView alloc] initWithImage:bimage];

 CGRect brect = bimageView.frame;

 brect.size.height = bimage.size.height;
 brect.size.width = bimage.size.width;
 brect.origin.x = ((scrollView.frame.size.width - bimage.size.width) / 2) + cx;
 brect.origin.y = ((scrollView.frame.size.height - bimage.size.height) / 2);

 bimageView.frame = brect; 
 [scrollView addSubview:bimageView];

 [bimageView release];

 cx += scrollView.frame.size.width;


 //SECOND PAGE

 if([selectedNpages intValue] == 2)
 {

  //Image

  UIImage *bimage2 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle]    pathForResource:image2a ofType:@"jpg"]];
  UIImageView *bimage2View = [[UIImageView alloc] initWithImage:bimage2];

  UIImageView *bimageView2 = [[UIImageView alloc] initWithImage:bimage2];

  CGRect brect2 = bimageView2.frame;
  brect2.size.height = bimage2.size.height;
  brect2.size.width = bimage2.size.width;
  brect2.origin.x = ((scrollView.frame.size.width - bimage2.size.width) / 2) + cx;
  brect2.origin.y = ((scrollView.frame.size.height - bimage2.size.height) / 2);

  bimageView2.frame = brect2;

  [scrollView addSubview:bimageView2];
  [bimageView2 release];

  cx += scrollView.frame.size.width;

 }

 self.pageControl.numberOfPages = [selectedNpages intValue];

 [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)]; 

 [image1a release];
 [image2a release]; 
}

#pragma mark -
#pragma mark UIScrollViewDelegate stuff

- (void)ScrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }
 CGFloat pageWidth = _scrollView.frame.size.width;
 int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

 pageControl.currentPage = page;
}


- (void)ScrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;

    [scrollView scrollRectToVisible:frame animated:YES]; 

    pageControlIsChangingPage = YES;
}


- (void)dealloc {
 [DetailViewController release];
 [selectedImage1 release];
 [selectedImage2 release];
 [selectedImage2 release];
 [selectedNpages release];
 [super dealloc];
}

@end

Thank you for taking time to read this.

回答1:

ScrollViewDidScroll: should start with lowercase "S", like this scrollViewDidScroll:. The same applies to scrollViewDidEndDecelerating:.

Well, in Objective-C you should ALWAYS start methods with lowercase character. Uppercase is reserved for class-names, constants, maybe globals. Methods - never!



回答2:

If Michal's answer doesn't fix it try this:

Is the pageControl IBOutlet connected in Interface Builder to your class? If it was not connected the pageControl.currentPage = page; in - (void)scrollViewDidScroll:(UIScrollView *)_scrollView would not change the selected dot.