简体   繁体   English

如何在滚动时使UIImage淡入和淡出?

[英]How do I make a UIImage fade in and fade out as I scroll?

I have a stretchy header that I made following this tutorial http://blog.matthewcheok.com/design-teardown-stretchy-headers/ . 我在遵循本教程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. 无论如何,它都可以正常工作,但是我无法使UIView在其顶部逐渐消失,因为视图会拉伸,并且在视图恢复正常时会返回原始alpha。 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
            }
        }

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. Alpha之间没有平滑的过渡,当视图返回正常状态时,Alpha不会返回。 Any advice or hints? 有什么建议或提示吗?

Update: I managed to do the exact opposite of what I wanted :p Here's the code: 更新:我设法做到了与我想要的完全相反的方法:p这是代码:

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
    }

Take a look at the UIScrollViewDelegate protocol. 看一下UIScrollViewDelegate协议。 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.) 我将创建一个UIView动画,该动画在滚动开始时将标题的Alpha向下动画,以及另一个UIView动画,一旦滚动结束(或可能一旦减速结束),则将其动画化为不透明。

Layout two UIImageViews on top of one another (in this example, bg is under bgB), then layout the UIScrollview at the very top. 将两个UIImageViews互相布局(在此示例中,bg在bgB下),然后将UIScrollview布局在最顶层。 In the scrollviewDidScroll method, place the following code: 在scrollviewDidScroll方法中,放置以下代码:

     float off = cv.contentOffset.x;
     float floatedIndex = off/cv.frame.size.width;
     int left = floatedIndex;
     int right = ceil(floatedIndex);
     if (right>events.count-1){ right=events.count-1; }
     float alpha = floatedIndex-left;

     UIImage * leftImage = nil;
     UIImage * rightImage = nil;
     NSDictionary * ld = events[left];
     NSDictionary * rd = events[right];
     NSObject * o = ld[@"artwork"];
     if ([o isKindOfClass:[UIImage class]]){
     leftImage = ld[@"artwork"];
     }
     o = rd[@"artwork"];
     if ([o isKindOfClass:[UIImage class]]){
     rightImage = rd[@"artwork"];
     }

     bg.image = leftImage;
     bgB.image = rightImage;
     bgB.alpha = alpha;

The 'left' and 'right' ints correspond to indices in an array. 'left'和'right'整数对应于数组中的索引。 There's a line checking that the scrollview cannot accidently go 'too far right' ie trying to find an index outside the bounds of the array. 有一行检查滚动视图不会意外地“太右”,即试图找到数组边界之外的索引。

Then I'm pulling data from an array called events, and checking for the existence of an image in the resulting dictionary (but you could use it for a straight up array of images), updating both UIImageviews and then fading the one on top (bgB) in and out depending on the scroll's content offset. 然后,我从称为事件的数组中提取数据,并检查结果字典中是否存在图像(但是您可以将其用于图像的直接排列),同时更新两个UIImageviews,然后将其顶部褪色( bgB)进出,具体取决于滚动条的内容偏移量。

The 'animation' correlates with user-interaction, which is great, but sometimes it's better to use animation blocks with another scrollview delegate method. “动画”与用户交互相关,这很好,但是有时最好将动画块与其他scrollview委托方法一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让CAEmitterCell在其生命周期结束时淡出? - How can I make a CAEmitterCell fade out at the end of its lifetime? ios swift 4 Google Maps-如何在拖动时删除GMS标记上的淡入/淡出动画? - ios swift 4 Google Maps - How do I remove the fade in/fade out animation on a GMS Marker while dragging? 如何让 AudioKit AKPluckedString 优雅地淡出? - How do I get an AudioKit AKPluckedString to fade out gracefully? 我如何“合法地”淡出应用程序中的导航栏? - How do I “legally” fade out the navigation bar in an app? 如何淡出 Xamarin Forms iOS 中的滚动视图的内容? - How do I fade out the contents of a scrollview in Xamarin Forms iOS? 如何让整个 UIView 淡出白色并返回? - How do I make the entire UIView fade to white and back? 如何让文字淡入UILabel? - How do I have text fade in to a UILabel? 如何使此淡入淡出动画起作用? - How do I get this fade animation to work? 当collectionView滚动时,如何使iOS collectionView的特定单元格淡出? - How can I make a particular cell of an iOS collectionView fade out as the collectionView scrolls? 尝试使用UIScrollView创建自定义水平UIPickerView-如何像UIPickerView中那样淡化边缘? - Trying to create a custom horizontal UIPickerView using UIScrollView - how do I fade the edges out like in UIPickerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM