我使用for循环在滚动条上创建多个图像和滑块,如何通过更改滑块的值来更改图像的alpha?这是我的代码 我试图在函数loadImages之外声明uiimages,但我不认为这是个好主意。 我知道如何在不使用for循环的情况下完成它,但是如果我想使用for循环,我怎么能让函数知道它应该在哪个uiimage上运行。因为在for循环中创建的所有uiimages都不是我认为的公共对象。
-(void)loadImages
{
for (int i = 0; i < 10; i++){
//show drawing position in the output
NSLog(@"x:%i, y:%i",xInc, yInc);
//text label on the slider button
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(78 + xInc, 55+yInc, 200, 20)];
Person *thisPerson = [arrayOfPerson objectAtIndex:i];
NSString *namePerson = thisPerson.name;
[label setText:[NSString stringWithFormat:@"%@",namePerson]];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
label.textAlignment = NSTextAlignmentCenter;
//front office background image of slider
NSString *frontimageName = [NSString stringWithFormat:@"frontofficeBUTTON.png"];
UIImageView *frontimgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:frontimageName]];
frontimgView.frame = CGRectMake(xInc, 10 + yInc, 332, 70);
[frontimgView setTag:i];
//back office background image of slider
NSString *backimageName = [NSString stringWithFormat:@"backofficeBUTTON.png"];
UIImageView *backimgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:backimageName]];
backimgView.frame = CGRectMake(xInc, 10 + yInc, 332, 70);
[backimgView setTag:i];
//slider
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(5+ xInc, 25+yInc, 322, 40)];
//give a id tag to the slider
[slider setTag:i];
//set a action to the slider
[slider addTarget:self action:@selector(UnlocklinkSlider:) forControlEvents:UIControlEventTouchUpInside];
//define the appearance of the slider
UIImage *stetchLeftTrack= [[UIImage imageNamed:@"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
UIImage *stetchRightTrack= [[UIImage imageNamed:@"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
[slider setThumbImage: [UIImage imageNamed:@"rightBUTTON.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
yInc = yInc + 80;
[_scrollView addSubview:backimgView];
[_scrollView addSubview:frontimgView];
[_scrollView addSubview:label];
[_scrollView addSubview:slider];
}
[_scrollView setContentSize:CGSizeMake(320, scrollLength)];
}
- (void)UnlocklinkSlider:(UISlider*)slider
{...}
答案 0 :(得分:0)
您的滑块应为UIControlEventValueChanged
添加目标和操作,以便图像同步更新(尽管它也会在触摸时起作用)。获得回调时,使用滑块标签查找带有匹配标记的图像视图。然后将图片视图alpha
设置为slider.value
。
要查找图像视图,您可以循环滚动视图的子视图并检查子视图的标记。您需要更改当前代码中的标记,因为您有许多具有相同标记的内容。
更好的选择是在控制器的数组属性中存储对图像视图的引用,然后使用滑块标记作为该数组中的索引。