0

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

在此处输入图像描述

如您所见,除了居中的图像之外,所有图像都有阴影。当用户滚动时它会改变,中心图像总是清晰的。它非常完美,直到左边缘图像:

在此处输入图像描述

这是代码:

-(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" 中,我强制将标签作为绝望的解决方案,但图像并不清楚。

4

1 回答 1

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 并查看当您登陆第一个图像时发生了什么。

于 2012-06-05T20:22:47.430 回答