0

I want to implement this fade in/out in a horizontal scroll I have, but I can't figure out how to access my button that is in a subview of my UIScrollView. This is what I'm trying to implement Fade in/out stackoverflow.

This is my code...

        Game *game = (Game *)[_schedule.games objectAtIndex:i];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
        [button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
        //[button setTitle:game.opponent forState:UIControlStateNormal];
        [_gameScrollList addSubview:button];

How Can I add the observer to my button, Can this be done?

        [self.scrollView addObserver:[self.contentViews objectAtIndex:i] 
                          forKeyPath:@"contentOffset" 
                             options:NSKeyValueObservingOptionNew
                             context:@selector(updateAlpha:)];
4

1 回答 1

1

要访问添加为子视图的此按钮的引用,您可以:

  • 将此按钮用作 iVar,保存参考以供以后使用;

  • 同样,将此按钮用作属性;

  • 遍历 _gameScrollList 的子视图,寻找 UIButton 类的子视图(不推荐,只有当它只有一个 UIButton 作为子视图时才能正常工作)

如何实施第二种方法的示例。只需声明一个属性:

@property(nonatomic, strong) UIButton *button;

接着:

    Game *game = (Game *)[_schedule.games objectAtIndex:i];
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
    [self.button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
    //[self.button setTitle:game.opponent forState:UIControlStateNormal];
    [_gameScrollList addSubview:self.button];

因此,之后,当您想在某处访问此按钮时,只需调用该属性

self.button.alpha = 0.5; //example of interation with your button

如果这不是您想要的,您真的应该编辑您的问题以使其更清楚。

希望它有所帮助。

于 2013-08-20T01:13:58.770 回答