滚动视图问题并访问其子视图

时间:2012-06-05 19:51:21

标签: objective-c ios5

我有一个滚动视图,其中包含大小为68x78的图像视图:

enter image description here

正如您所看到的,除了居中的图像之外的所有图像都有阴影。当用户滚动时,它会发生变化,中心图像始终清晰。它对于左边缘图像来说是完美的:

enter image description here

以下是代码:

-(void) buildSlideView{

    fotosJugadores = [[NSArray alloc] initWithObjects:@"cara5.png", @"cara7.png", @"cara8.png", @"cara9.png", @"cara11.png", @"cara13.png", @"cara14.png", @"cara15.png", @"cara18.png",@"cara19.png", @"cara20.png", @"cara32.png",@"cara44.png",@"cara51.png", @"cara100.png", @"cara101.png", @"cara102.png",@"cara103.png", @"cara104.png", @"cara105.png",@"cara106.png",@"cara107.png", nil];

    numberOfViews = [fotosJugadores count];

    for (int i = 0; i <    [fotosJugadores count]; i++) {

        UIImage *myImage = [UIImage imageNamed:[fotosJugadores objectAtIndex:i]];
        CGFloat yOrigin = i * (myImage.size.width+3) + 120;
        UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, myImage.size.width, myImage.size.height)];
        awesomeView.tag = i;
        awesomeView.image = myImage;
        awesomeView.alpha = 0.5f;
        awesomeView.backgroundColor = [UIColor blackColor];
        [self.jugadorSlide addSubview:awesomeView];
        awesomeView=nil;

    }

    [jugadorSlide setBackgroundColor:[UIColor blackColor]];
    jugadorSlide.contentSize = CGSizeMake(68 * numberOfViews+240,78);
    jugadorSlide.layer.cornerRadius = 11;
    jugadorSlide.layer.masksToBounds = YES;
    [jugadorSlide setContentOffset:CGPointMake(((68 * numberOfViews)/2), 0)];
    //jugadorSlide.decelerationRate = UIScrollViewDecelerationRateNormal;
    [self scrollViewDidEndDragging:jugadorSlide willDecelerate:NO];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    currentIndex = roundf(scrollView.contentOffset.x / 68);
    NSLog(@"current end %d", currentIndex);
    UIImageView *currentImage = [scrollView viewWithTag:currentIndex];
    if (currentIndex>0&&currentIndex<=21){
         if (currentIndex==currentImage.tag){
            currentImage.alpha=1.0f;
            [self changePerfilImage:currentImage.tag];}
             CGPoint pointZero= CGPointMake(scrollView.contentOffset.x, currentImage.frame.origin.y);
            [jugadorSlide setContentOffset:pointZero animated:YES];

    }else {
            UIImageView *currentImage = [scrollView viewWithTag:0];
            NSLog(@"end dragging image tag %d", currentImage.tag);
            currentImage.alpha=1.0f;
            [self changePerfilImage:currentImage.tag];}
            CGPoint pointZero= CGPointMake(currentImage.frame.origin.x+15, 0);
            //[jugadorSlide setContentOffset:pointZero animated:YES];

}

正如您所看到的,在scrollViewDidEndDragging:“else”中,我强制将标记作为一个desesperate解决方案,但图像并不清晰。

1 个答案:

答案 0 :(得分:1)

您正在递增标签两次......

awesomeView.tag = i++;

应该是:

awesomeView.tag = i+1;

我使用i + 1因为我从不使用0的标记,因为任何未分配标记的子视图都会有0的标记。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSInteger currentIndex = roundf(scrollView.contentOffset.x / 68.0) + 1;
    UIImageView *currentImage = [scrollView viewWithTag:currentIndex];
    ...
}

如果这不起作用,那么每次登陆新图像时都应该NSLog currentIndex,看看当你登陆第一张图像时发生了什么。