我想在我的横向滚动中实现这个淡入/淡出,但我无法弄清楚如何访问我的UIScrollView子视图中的按钮。这就是我正在尝试实施的Fade in/out stackoverflow。
这是我的代码......
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];
如何将观察者添加到我的按钮,可以这样做吗?
[self.scrollView addObserver:[self.contentViews objectAtIndex:i]
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew
context:@selector(updateAlpha:)];
答案 0 :(得分: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
如果这不是你想要的,你真的应该编辑你的问题,以便更清楚。
希望它有所帮助。