UIScrollview 弹跳和缩放固定顶部

编程入门 行业动态 更新时间:2024-10-28 15:28:15
本文介绍了UIScrollview 弹跳和缩放固定顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

试图在滚动视图向下弹跳时使 UIImageView 缩放.

这个想法是从这个 dribbble 中窃取的:

解决方案

根据滚动视图的偏移量对 imageView 应用缩放变换:

extension ViewController: UIScrollViewDelegate {public func scrollViewDidScroll(_ scrollView: UIScrollView) {让偏移量 = scrollView.contentOffset如果 offset.y <0.0 {var 变换 = CATransform3DTranslate(CATransform3DIdentity, 0, offset.y, 0)让 scaleFactor = 1 + (-1 * offset.y/(imageViewHeightConstraint.constant/2))变换 = CATransform3DScale(变换,比例因子,比例因子,1)imageView.layer.transform = 变换} 别的 {imageView.layer.transform = CATransform3DIdentity}}}

通过这种方式,您无需将其固定"到顶部,imageView 将随着弹跳时内容偏移量的减小而简单地放大.你只需要你的 imageView 有一个顶部约束(加上前导、尾随)和一个高度.

Trying to make UIImageView scale when the scrollview is bouncing down.

The idea was stolen from this dribbble: https://dribbble/shots/3341878-Product-Page

As you can see, image is like'a scaling when the scrollview is bouncing down.

In my app I have the UIImageView inside the UIScrollview, I can't understand how to fix the top of the UIImageView while the UIScrolvview is bouncing down.

Here is what I want to do:

解决方案

Apply a scale transform to your imageView based on your scrollview's offsets:

extension ViewController: UIScrollViewDelegate {
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset

        if offset.y < 0.0 {
            var transform = CATransform3DTranslate(CATransform3DIdentity, 0, offset.y, 0)
            let scaleFactor = 1 + (-1 * offset.y / (imageViewHeightConstraint.constant / 2))
        transform = CATransform3DScale(transform, scaleFactor, scaleFactor, 1)
            imageView.layer.transform = transform
        } else {
            imageView.layer.transform = CATransform3DIdentity
        }
    }
}

In this way you don't need to "fix" it to top, the imageView will simply scale up as the content offset decreases when bouncing. All you need is that your imageView has a top constraint (plus leading, trailing) and a height.

这篇关于UIScrollview 弹跳和缩放固定顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:48:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1184034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   UIScrollview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!