本文介绍了如何在滚动时使 UIImage 淡入淡出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照本教程制作了一个有弹性的标题http://blog.matthewcheok.com/design-teardown-stretchy-headers/.无论如何,它运行良好,但我无法在其顶部制作 UIView 随着视图拉伸而淡出并在视图恢复正常时返回到原始 alpha.我能想到的最好的是:

I have a stretchy header that I made following this tutorial http://blog.matthewcheok.com/design-teardown-stretchy-headers/. Anyway it's working perfectly but I'm having trouble making a UIView on top of it fade out as the view stretched and returning to original alpha as the view is returned to normal. Best I could come up with is this:

override func

    scrollViewDidScroll(scrollView: UIScrollView) {
            updateHeaderView()
            var offset = scrollView.contentOffset.y
            if offset < -170 {

                headerBlurImageView?.alpha = max(0.0, offset - 95/35)
            } else {
            self.headerBlurImageView?.alpha = 1
            }
        }

但它几乎不起作用.alpha 之间没有平滑过渡,当视图恢复正常时,alpha 不会返回.有什么建议或提示吗?

But it barely works. There is no smooth transition between the alphas and when the view is returned to normal the alpha doesn't return. Any advice or hints?

更新:我设法做的与我想要的完全相反:p 这是代码:

Update: I managed to do the exact opposite of what I wanted :p Here's the code:

override func scrollViewDidScroll(scrollView: UIScrollView) {
        updateHeaderView()
        var height: CGFloat
        var position: CGFloat
        var percent: CGFloat

        height = scrollView.bounds.size.height/2
        position = max(-scrollView.contentOffset.y, 0.0)
        percent = min(position / height, 1.0)
        self.headerBlurImageView.alpha = percent
    }

推荐答案

看看 UIScrollViewDelegate 协议.有许多消息与开始和结束拖动以及减速有关.您应该能够将视图控制器设置为滚动视图的委托并实现其中一些方法.我将创建一个 UIView 动画,在滚动开始时将标题的 alpha 向下动画,另一个 UIView 动画在滚动结束(或可能在减速结束时)将其动画恢复为不透明.

Take a look at the UIScrollViewDelegate protocol. there are a number of messages that relate to starting and ending dragging, and decelerating. You should be able to set your view controller up as the scroll view's delegate and implement some of those methods. I'd create a UIView animation that animates the header's alpha down when scrolling begins, and another UIView animation that animates it back to opaque once scrolling ends (or possibly once deceleration ends.)

这篇关于如何在滚动时使 UIImage 淡入淡出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:42