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

Posted

技术标签:

【中文标题】如何获得屏幕上 UIview 的百分比【英文标题】:How to get a percentage of how much the UIview is on screen 【发布时间】:2015-07-31 10:48:23 【问题描述】:

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

我尝试过使用“animateWithDuration”,但这似乎要等到视图完全显示在屏幕上,所以它显示出延迟淡入。我考虑过使用gestureRecognizers,但pageViewController 已经有了它们,因此我必须覆盖它们。

【问题讨论】:

【参考方案1】:

您还可以简单地利用 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)

【讨论】:

【参考方案2】:

在视图中添加平移手势以了解在屏幕上滑动了多少。定义一个变量说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
  

【讨论】:

这个函数不能是@IBAction吗?两个手势状态也都应该“结束”吗?不应该一个“开始”而另一个“结束”吗?最后,这会覆盖 UIPageViewController 的手势识别器,因为我知道它已经内置了滑动手势。【参考方案3】:

这是 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
    

编码愉快!

【讨论】:

以上是关于如何获得屏幕上 UIview 的百分比的主要内容,如果未能解决你的问题,请参考以下文章

UIView动画

故事板iOS - 如何以屏幕尺寸的百分比而不是静态值顶部引脚边距给出上边距?

iphonexr如何显示电池电量百分比

在css里如何让背景图片在不同的屏幕上都完全显示出来,怎么设置百分比

从自定义 UIView 类设置视图宽度约束不更新帧

向串口发送数据时如何获得链接速度和传输百分比?