ios - 固定顶部的 UIScrollview 弹跳和缩放

标签 ios xcode uiscrollview uiimageview

尝试在 ScrollView 向下弹跳时使 UIImageView 缩放。

这个想法是从这个运球中偷来的:https://dribbble.com/shots/3341878-Product-Page

如你所见,图像就像' ScrollView 向下弹跳时的缩放。

在我的应用程序中,我在 UIScrollview 中有 UIImageView,我不明白如何在 UIScrollvview 向下弹跳时修复 UIImageView 的顶部。

这是我想做的:

enter image description here

最佳答案

根据 ScrollView 的偏移量对 imageView 应用缩放变换:

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
        }
    }
}

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

关于ios - 固定顶部的 UIScrollview 弹跳和缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726487/

相关文章:

ios - ScrollView 不随 subview 滚动

ios - Fabric Crashlytics 不工作。 iOS 14 应用/XCode 12

ios - 使用AVAssetReader按时间值获取特定帧

ios - 无法使用 OAuth Starter Kit for LinkedIn 进行共享

iOS - 如何删除 UIButton 边框颜色?

ios - Assets 目录 : Access images with same name in different folders

ios - 静态单元格 : Content does not appear

ios - Iphone 模拟器无法定位

swift - 页面控件中的多个 UILabel

objective-c - UIScrollView 添加 UIViewController 作为 subview ?与 UIPageControl