【问题标题】:UIScrollView with UITapGestureRecognizer and UIPageControl带有 UITapGestureRecognizer 和 UIPageControl 的 UIScrollView
【发布时间】:2011-02-25 21:29:39
【问题描述】:

我正在创建一个包含图像的水平滚动表格视图。我的滑动功能运行良好,并且能够将图像添加到 scrollView,就像在 cellForRowAtIndexPath 中一样:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
    [scrollView setContentSize:CGSizeMake(700, 78)];
    UIImage *footImage = [UIImage imageNamed:@"Foot.png"];
    UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
    footImageView.frame = CGRectMake(0, 0, 80, 78);
    footImageView.userInteractionEnabled = YES;
    [scrollView addSubview: footImageView];

    UIImage *handImage = [UIImage imageNamed:@"Hand.png"];
    UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
    handImageView.frame = CGRectMake(90, 0, 80, 78);
    handImageView.userInteractionEnabled = YES;
    [scrollView addSubview: handImageView];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightImage)];
    [scrollView addGestureRecognizer:tapGesture];
    NSLog(@"tapGesture added to scrollView");


    [[cell contentView] addSubview:scrollView];

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
    [pageControl setNumberOfPages:4];
    [[cell contentView] addSubview:pageControl];

我的tapGesture 正在向选择器hightlightImage 注册,并记录它正确调用此方法。

- (void) highlightImage
{
    NSLog(@"tapping tapping");
}

真正工作的目的是能够突出显示/选择这些图像(如果点击),以及突出显示/取消选择如果再次点击它们.我将在屏幕上有另一个按钮导航到下一页(此处无关)。

我走在正确的道路上吗?显然,我将填充UIImagesNSArray 并以这种方式填充scrollView,但现在只是将其删除。如果有人可以给我一些指导或示例,说明如何单独选择/取消选择每个按钮,那就太好了:)

【问题讨论】:

    标签: iphone scrollview uigesturerecognizer uipagecontrol


    【解决方案1】:

    使用 UIButton 代替 UIImageView。

    它具有内置的selected 功能。

    摆脱你的手势,将 highlightImage 作为动作添加到每个按钮。

    把你的highlightImage 变成highlightImage:(id)sender

    sender 应该是一个按钮,所以你可以这样做

    [sender setSelected:YES];

    【讨论】:

    • 轰隆隆!我已经迁移到使用 UIButtons,但我什至没有想过将每个按钮的操作用于UIControlEventTouchUpInside。我的新highlightImage 方法正在接收它发送的不同发件人(每个UIButton 实例)。现在,我使用createScrollButtons 方法在滚动视图的所需位置创建每个按钮。对于我要创建的每个按钮,我都必须调用 addTarget: action: forControlEvents:。我真正喜欢的是某种循环,因此请检查每个想要创建的UIButton 并将它们放置在沿滚动视图的正确位置。
    猜你喜欢
    • 2018-11-09
    • 2014-02-22
    • 1970-01-01
    • 2015-09-02
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多