-2

我使用 for 循环在滚动条上创建了多个图像和滑块,如何通过更改滑块的值来更改图像的 alpha?这是我尝试在函数 loadImages 之外声明 uiimages 的代码,但我认为这不是一个好主意。我知道如何在不使用 for 循环的情况下做到这一点,但是如果我想使用 for 循环,我怎样才能让函数知道它应该对哪个 uiimage 进行操作。因为我认为在 for 循环中创建的所有 uiimage 都不是公共对象。

-(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 
{...}
4

1 回答 1

0

您的滑块应该添加一个目标和操作,UIControlEventValueChanged以便图像同步更新(尽管它也可以用于修饰)。当您收到回调时,使用滑块标签查找具有匹配标签的图像视图。然后将图像视图设置alphaslider.value.


要找到图像视图,您可以遍历滚动视图的子视图并检查子视图的标签。您需要更改当前代码中的标签,因为您有很多具有相同标签的东西。

更好的选择是将图像视图的引用存储在控制器上的数组属性中,然后使用滑块标签作为该数组中的索引。

于 2013-08-13T15:21:18.933 回答