滚动视图进入视图2时如何更改文本标签?

时间:2014-04-01 22:37:57

标签: ios iphone objective-c

我正在制作一个水平滚动的3个级别的选择级别 - 滚动控制三个视图“视图1”“视图2”和“视图3”,我需要向用户做出幻觉,我已经放置了3个每个视图上的按钮和UIview两侧的半按钮有2个标签,一个写“lev”另一个“el 2”......

当用户移动到View 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 {

}

这是怎么做到的?

1 个答案:

答案 0 :(得分:0)

//实现UIScrollview Delegate方法

-(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"]];
}

 }