2014-04-01 35 views
1

我正在做一個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 { 

} 

這是如何完成的?

+0

等待,我想我必須做的是延長標籤,哎呀,再等下去!幫幫我?請輸入 – Ricky

+1

使此VC成爲滾動視圖的委託,並實現scrollViewDidScroll。在那裏,您可以檢查scrollView.contentOffset並將其與視圖原點進行比較。 – danh

+0

這裏有一個有用的教程(查看下面的分頁示例):http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content –

回答

0

//落實UIScrollView的委託方法

-(void)scrollViewDidScroll:(UIScrollView *)sender 

{ 

CGFloat pageWidth = yourScrollView.frame.size.width; 
int currentPage = floor((yourScrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

if (currentPage != featureVenPageControll.currentPage) 
{ 
    [_elone setText:[NSString stringWithFormat:@"Level 2"]]; 
} 

} 
+0

嘿,已經使用了這個我可以使它工作,_btnIllusionLeft只有當它在View2上,我需要它被設置隱藏設置隱藏在視圖1上,只出現在視圖2和3上,_btnIllusionRight沒有隱藏,當頁面進入View3。事實上,我需要它隱藏在視圖3之前,甚至到達那裏滾動... 另一件事,一旦它從視圖2離開它不會恢復正常... – Ricky