我正在做一個3級水平滾動的選擇級別 - 滾動控制三個視圖「視圖1」「視圖2」和「視圖3」,我需要對用戶造成幻想,我已經放置了3個按鈕在每個視圖和一半的按鈕在每個面的UIview與2個標籤一個寫「lev」其他「el 2」...如何在滾動視圖轉到視圖2時更改文本標籤?
當用戶移動到查看2我希望設置標籤到「el 2」,並且一旦滾動已經在第2級上定位,我想讓標籤寫上「el 1」 - 這會產生錯覺,並且速度太快以至於用戶不會注意到。
繼承人的代碼: 此:
[_elone setText:[NSString stringWithFormat:@"Level 2"]];
但我不知道放在哪裏, 我應該做一個出口,一個動作一個說法?
@interface PagerViewController()
@property (assign) BOOL pageControlUsed;
@property (assign) NSUInteger page;
@property (assign) BOOL rotating;
- (void)loadScrollViewWithPage:(int)page;
@end
@implementation PagerViewController
@synthesize scrollViewTwo;
@synthesize pageControlTwo;
@synthesize pageControlUsed = _pageControlUsed;
@synthesize page = _page;
@synthesize rotating = _rotating;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollViewTwo setPagingEnabled:YES];
[self.scrollViewTwo setScrollEnabled:YES];
[self.scrollViewTwo setShowsHorizontalScrollIndicator:NO];
[self.scrollViewTwo setShowsVerticalScrollIndicator:NO];
[self.scrollViewTwo setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
[self loadScrollViewWithPage:i];
}
self.pageControlTwo.currentPage = 0;
_page = 0;
[self.pageControlTwo setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControlTwo.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollViewTwo.contentSize = CGSizeMake(scrollViewTwo.frame.size.width * [self.childViewControllers count], scrollViewTwo.frame.size.height);
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= [self.childViewControllers count])
return;
// replace the placeholder if necessary
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller == nil) {
return;
}
// add the controller's view to the scroll view
if (controller.view.superview == nil) {
CGRect frame = self.scrollViewTwo.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollViewTwo addSubview:controller.view];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
- (IBAction) changePage :(id) sender {
}
這是如何完成的?
等待,我想我必須做的是延長標籤,哎呀,再等下去!幫幫我?請輸入 – Ricky
使此VC成爲滾動視圖的委託,並實現scrollViewDidScroll。在那裏,您可以檢查scrollView.contentOffset並將其與視圖原點進行比較。 – danh
這裏有一個有用的教程(查看下面的分頁示例):http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content –