我是iPhone新手,正在研究本教程:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 - 但我在XCode 4.3.2中做了這個。這個例子非常簡單,我理解代碼,但由於某種原因,scrollViewDidScroll函數在我的ViewController.m中沒有爲我啓動(因此它不會更改Page Control分頁圖標並將其更新到當前頁面)。我把一個的NSLog()這個函數,所以我可以知道它是不斷激活 - 但在我調試控制檯我從來沒有看到這段文字iPhone Dev- Page Control - scrollViewDidScroll not firing
從我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
任何幫助將不勝感激。我一直在從他們在教程中的可下載文件中來回回顧我自己的一遍又一遍......當我開始滾動時,我只是覺得這個東西應該觸發,不知道爲什麼它不是......提前致謝!
非常感謝!這工作就像一個魅力......我不相信我花了這麼多小時試圖弄清楚這一點。我愛你,每個人都在stackoverflow! – Robbie