如何获得UIview在屏幕上的百分比

时间:2015-07-31 10:48:24

标签: ios uiview uiimageview fadein

我正在构建一个IOS应用程序,当您第一次开始使用该应用程序时,该应用程序以UIPageViewController开始。我一直在寻找一种淡入imageView的方法,具体取决于用户刷到下一个屏幕的距离。即:当他们刷过屏幕的一半时,alpha = 0.5,当屏幕被完全刷过时,alpha = 1.0?

我尝试使用' animateWithDuration'但这似乎等到视图完全在屏幕上,所以它显示了一个滞后的淡入。我想到使用gestureRecognizers,但pageViewController已经有它们,因此我将不得不重写它们。

3 个答案:

答案 0 :(得分:0)

在视图上添加平移手势,以了解在屏幕上刷了多少。定义一个变量说CGFloat xpostion;

- (void)panRecognized:(UIPanGestureRecognizer *)gesture
{
  CGPoint translate = [gesture translationInView:self.view];

  if (gesture.state == UIGestureRecognizerStateBegan)
  {
     xpostion = translate.x;
  }
  else if (gesture.state == UIGestureRecognizerStateEnded)
  {
     CGFloat totalXSwipe = translate.x - xpostion;

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    CGFloat percentage = fabs(totalXSwipe)/width;
    NSLog(@"Swipe percentage %f",percentage);

    //Can use in your requirement to set as alpha value as it will be always less or equal to zero
  }
}

答案 1 :(得分:0)

将Paresh的Objective-C代码转换为Swift:

var xpostion: CGFloat = 0.0
func panRecognized(_ gesture: UIPanGestureRecognizer?) {
    guard let translate: CGPoint? = gesture?.translation(in: view) else { return }

    if gesture?.state == .began {
        xpostion = (translate?.x)!
    } else if gesture?.state == .ended {
        let totalXSwipe: CGFloat = (translate?.x ?? 0.0) - xpostion

        let width: CGFloat = UIScreen.main.bounds.size.width

        let percentage = CGFloat(fabs(Float(totalXSwipe))) / width
        print("Swipe percentage \(percentage)")

        //Can use in your requirement to set as alpha value as it will be always less or equal to zero
    }
}

编码愉快!

答案 2 :(得分:0)

您还可以简单地利用UIScrollView API来帮助您实现已查看/滚动的UIView的百分比。这是一个想法。

拖动UIScrollView对象并将其正确连接到Storyboard中的相应场景上,或者在代码中正确实例化,继续使当前对应的ViewController类与要计算到 UIScrollViewDelegate的屏幕百分比一致协议。

例如class YourCustomViewController: UIViewController, UIScrollViewDelegate {}

然后,在YourCustomViewController类中的某个位置,您可以实现以下代码。这是实现屏幕垂直滚动百分比的代码:

func computePercentage(in scrollView: UIScrollView) {
    let height = scrollView.contentSize.height
    let viewed = scrollView.contentOffset.y + scrollView.bounds.size.height
    let pagePercentScrolled = viewed / height * 100.0
   // print("Percent Scrolled: \(pagePercentScrolled)")

    if pagePercentScrolled >= 50.0 && pagePercentScrolled < 100.0 {
        //Do something.
        //Example, set imageView alpha to half
        yourCustomImageView.alpha = 0.5
    } else if pagePercentScrolled == 100 {
        //Do something.
        //Example, fade in/show full imageView
        yourCustomImageView.alpha = 1.0
    }
}

以相同的方式,您可以稍作更改以计算水平滚动屏幕的百分比:

    func computePercentage(in scrollView: UIScrollView) {
    let width = scrollView.contentSize.width
    let viewed = scrollView.bounds.size.width - scrollView.contentOffset.x
    let pagePercentScrolled = viewed / width * 100.0
    //print("Percent Scrolled: \(pagePercentScrolled)")

    if pagePercentScrolled >= 50.0 && pagePercentScrolled < 100.0 {
        //Do something.
        //Example, set imageView alpha to half
        yourCustomImageView.alpha = 0.5
    } else if pagePercentScrolled == 100 {
        //Do something.
        //Example, fade in/show full imageView
        yourCustomImageView.alpha = 1.0
    }
}

当然,您可以将上述两种方法简单地组合为一个。但是为了简单起见,我将它们分开。 然后,您可以在YourCustomViewController上实现UIScrollViewDelegate方法,如下所示:

    // MARK: Scroll View Delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    computePercentage(in: scrollView)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    computePercentage(in: scrollView)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
    computePercentage(in: scrollView)
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    computePercentage(in: scrollView)
}