检测哪个图像在滚动视图上

时间:2012-12-08 09:02:50

标签: iphone objective-c ios xcode

请帮助我,滚动时检测哪个图像在滚动视图上。

- (void)viewDidLoad
{
    [super viewDidLoad];
NSArray *colors = [[NSArray alloc] initWithObjects:@"ip1.jpg", @"ip2.jpg", @"ip3.jpg", @"ip4.jpg", @"ip5.jpg", @"ip6.jpg", @"ip7.jpg", @"ip8.jpg", @"ip9.jpg", @"ip10.jpg",@"ip11.jpg",@"ip12.jpg",@"ip13.jpg",@"ip14.jpg",@"ip15.jpg",@"ip16.jpg",@"ip17.jpg",@"ip18.jpg",@"ip19.jpg",@"ip20.jpg",@"ip21.jpg", nil];

    NSMutableArray *imgArray = [[NSMutableArray alloc] init];
    UIImageView *tempImageView;
    for(NSString *name in colors) {
        tempImageView = [[UIImageView alloc] init];
        tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        tempImageView.image = [UIImage imageNamed:name];
        [imgArray addObject:tempImageView];

    }

    CGSize pageSize = scrollView.frame.size; // scrollView is an IBOutlet for our UIScrollView
    NSUInteger page = 0;
    for(prevView in imgArray) {

        [scrollView addSubview:prevView];


        // This is the important line
        prevView.frame = CGRectMake(pageSize.width * page++ + 30, 0, pageSize.width - 60, pageSize.height);
 }   




    scrollView.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

}



- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (!pageControlBeingUsed) {
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;



    }
}

请帮帮我。如果我使用slog来显示CurrentPage,它会显示我像“117892”,我只需要一个NSLog,它将显示我的“颜色”NSArray中的当前图像。非常感谢

1 个答案:

答案 0 :(得分:1)

试试这个,你可以将“page”变量声明为全局然后传递,如代码所示

NSLog(@"%@",[colors objectAtIndex:page]);