UIScrollView使用UIImageview和手势使用transitionFromView表现得很奇怪

时间:2012-10-17 14:35:18

标签: objective-c ios uiview uiscrollview uiimageview

我制作了一个包含UIScrollview的视图:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 65, 300, 188)];
//BackViews will hold the Back Image
BackViews = [[NSMutableArray alloc] init];
for (int i=0; i<BigPictures.count; i++) {
    [BackViews addObject:[NSNull null]];
}
FrontViews = [[NSMutableArray alloc] initWithCapacity:BigPictures.count];
[self.pageControl setNumberOfPages:BigPictures.count];

然后我添加了几个包含图像的UIImageviews:

//BigPictures holds objects of type UIImage
for (int i = 0; i < BigPictures.count; i++) {
    UIImageView *ImageView = [[UIImageView alloc] initWithImage:[BigPictures objectAtIndex:i]];
    ImageView.frame = [self.scrollView bounds];
    [ImageView setFrame:CGRectMake(self.scrollView.frame.size.width * i, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height)];
    //this saves the FrontView for later (flip)
    [FrontViews insertObject:ImageView atIndex:i];
    [self.scrollView addSubview:test];
}

// Detect Single Taps on ScrollView
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip)];
[self.scrollView addGestureRecognizer:tap];

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;

好到目前为止一切顺利。现在执行flipImage部分的方法:

- (void)flip {
    int currentPage = self.pageControl.currentPage;
    UIView *Back = nil;
    if ([BackViews objectAtIndex:currentPage] == [NSNull null]) {
    //CreateBackView is just creating an UIView with text in it.
    Back = [self CreateBackView];
    [BackViews replaceObjectAtIndex:currentPage withObject:Back];
    [UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[BackViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
} else {
    [UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[FrontViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL];
    [BackViews replaceObjectAtIndex:currentPage withObject:[NSNull null]];
}
[self.view addSubview:Back];
[self rebuildScrollView];

}

这就是rebuildScrollView的作用:

- (void)rebuildScrollView
{
    for (UIView *subview in self.scrollView.subviews) {
    [subview removeFromSuperview];
}

for (int i = 0; i < BigPictures.count; i++) {
    if ([BackViews objectAtIndex:i] == [NSNull null]) {
        [self.scrollView addSubview:[FrontViews objectAtIndex:i]];
    } else {
        [self.scrollView addSubview:[BackViews objectAtIndex:i]];
    }
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}

所以行为如下:

  1. 如果我点击第一张图片(滚动视图中的1张中的1张),效果就是我想要的方式,这意味着frontimage转过来并显示空白(白色)背面,中间有一些文字
  2. 如果我点击第二张图像,图像会转动但背面完全为空,显示窗口的灰色背景。如果我滚动到其他图像,仍会显示正面图像(如预期的那样)
  3. 现在我点击第三张图片,它与1)相同。
  4. 目前的布局是[BackView,Nothing,Backview]
  5. 让我们再次运行。但现在我点击最后一张图片,它与2)相同:(
  6. 这里有什么想法吗?

    编辑:一些新的发现。我将图片数量增加了一倍,这就是前视图和后视图的放置方式(翻转每张照片后)。 P =图片&amp; B = Backview。

    • P_1(B_1) - 实际上唯一正确的
    • P_2(空 - 应为B_2)
    • P_3(B_2 - 应为B_3)
    • P_4(空 - 应为B_4)
    • P_5(B_3 - 应为B_5)
    • P_6(空 - 应为B_6)

1 个答案:

答案 0 :(得分:0)

完成了重建,现在可行了。真奇怪,因为我使用了完全相同的代码。