UIPageControl对点击没有正确的反应

时间:2012-07-27 17:52:22

标签: objective-c ios uiscrollview swipe uipagecontrol

在我的TestApp中,我创建了一个类“Carousel”,它应该让我以简单的方式创建一个带有UIPageControl的滑动菜单(通过简单地创建Carousel类的实例)。

  • Carousel是UIView的子类
  • 在init,它创建一个UIView,包含UIScrollView,UIPageControl
  • 我可以在滚动视图中添加更多UIViews

我不知道这是否是正确的方法,但我的示例在我的TestApp中运行得很好。页面之间的滑动完美地工作,并且UIPageControl中当前页面的显示是正确的。

如果没有一个问题:UIPageControl有时会对点击/点击作出反应(我到目前为止只在模拟器中测试过!),有时却没有。让我们说大部分时间都没有。我还不知道它什么时候发生,对我来说它只是随机的......

如下所示,我添加了

[pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];

到我的代码。我以为这会正确处理水龙头?但不幸的是,pageChange并不总是被调用(因此每次点击时UIPageControl的值都不会改变。)

我很感激对此的任何意见,因为我还没有找到任何解决方案。

这是我到目前为止所做的:

Carousel.h

#import <UIKit/UIKit.h>

@interface Carousel : UIView {
    UIScrollView *scrollView;
    UIPageControl *pageControl;
    BOOL pageControlBeingUsed;
}

- (void)addView:(UIView *)view;
- (void)setTotalPages:(NSUInteger)pages;
- (void)setCurrentPage:(NSUInteger)current;
- (void)createPageControlAt:(CGRect)cg;
- (void)createScrollViewAt:(CGRect)cg;

@end

Carousel.m

#import "Carousel.h"

@implementation Carousel

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        // Create a scroll view
        scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];

        scrollView.delegate = (id) self;

        // Init Page Control
        pageControl = [[UIPageControl alloc] init];
        [pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:pageControl];

    }

    return self;

}

- (IBAction)pageChange:(id)sender {
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:TRUE];
    NSLog(@"%i", pageControl.currentPage);
}

- (void)addView:(UIView *)view {
    [scrollView addSubview:view];
}

- (void)createPageControlAt:(CGRect)cg {
    pageControl.frame = cg;
}

- (void)setTotalPages:(NSUInteger)pages {
    pageControl.numberOfPages = pages;
}

- (void)setCurrentPage:(NSUInteger)current {
    pageControl.currentPage = current;
}

- (void)createScrollViewAt:(CGRect)cg {
    [scrollView setPagingEnabled:TRUE];
    scrollView.frame = cg;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*pageControl.numberOfPages, scrollView.frame.size.height);
    [scrollView setShowsHorizontalScrollIndicator:FALSE];
    [scrollView setShowsVerticalScrollIndicator:FALSE];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    float frac = scrollView.contentOffset.x / scrollView.frame.size.width;
    NSUInteger page = lround(frac);
    pageControl.currentPage = page;

}

@end

ViewController.m (在viewDidLoad中的某处)

Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

for (int i=0; i<5; i++) {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, 420)];
    UIColor *color;
    if(i%3==0) color = [UIColor blueColor];
    else if(i%3==1) color = [UIColor redColor];
    else color = [UIColor purpleColor];
    view.backgroundColor = color;
    [carousel addView:view];
    view = nil;
}

[carousel setTotalPages:5];
[carousel setCurrentPage:0];

[carousel createPageControlAt:CGRectMake(0,420,320,40)];
[carousel createScrollViewAt:CGRectMake(0,0,320,420)];

1 个答案:

答案 0 :(得分:3)

您的代码是正确的。最有可能的是pageControl的框架非常小,因此查找触摸事件的区域并不多。您需要增加pageControl高度的大小,以确保始终识别点按。