我是 iPhone 开发的新手,正在编写本教程: http: //www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 - 但我在 XCode 4.3.2 中进行。该示例非常简单,我理解代码,但由于某种原因,我的 ViewController.m 中的 scrollViewDidScroll 函数没有为我触发(因此这不会更改页面控件分页图标并将其更新到当前页面)。我在这个函数中放置了一个 NSLog() 以便我可以判断它是否正在激活 - 但在我的调试控制台中我从来没有看到这个文本
我的 ViewController.h 中的代码是:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView* scrollView;
UIPageControl* pageControl;
}
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
@end
我的 ViewController.m 中的代码是:
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
//!!!!!!!!!!!!!!
//THIS IS WHERE MY PROBLEM IS -- THIS CODE DOESN'T SEEM TO EVER GET RUN EVEN WHEN I SCROLL!!!!!
//!!!!!!!!!!!!!
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(@"this just fired");
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
@end
任何帮助将不胜感激。我一直在一遍又一遍地从他们在教程中的可下载文件到我自己的文件来回移动......我只是觉得当我开始滚动时这个东西应该会触发并且不知道为什么它不是......提前致谢!