UIImageView基于ScrollView位置的淡化

时间:2013-08-15 02:57:12

标签: ios objective-c

我有UIPageControl翻转不同的页面。当用户从第1页滚动到第2页时,我想让UIImageView开始消失。我正在努力找出根据位置添加图层的正确方法。任何帮助都将非常感激......

[UIView beginAnimations:@"fade out" context:nil];
    [UIView setAnimationDuration:1.0]; // some how based on position?? 
    imageView.alpha = 1.0; // here to?
    [UIView commitAnimations];

修改

设置代理:

 self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;

1 个答案:

答案 0 :(得分:9)

您需要委托来覆盖以下方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

然后你应该检查scrollView.contentOffset.x值来确定滚动视图水平滚动了多少,然后使用它来确定你的imageView的alpha,如下所示:

CGFloat newAlpha = scrollView.contentOffset.x / scrollView.frame.size.width; // This will need to be adapted depending on the content size of your scroll view -- do not just copy and paste this expecting it to work :)
imageView.alpha = newAlpha;