美文网首页iOS Tips
SwiftUI 完美实现 上拉加载,下拉刷新

SwiftUI 完美实现 上拉加载,下拉刷新

作者: Renew全栈工程师 | 来源:发表于2021-03-25 02:57 被阅读0次

    1.不多说,看图

    SwiftUI 完美实现上拉加载,下拉刷新

    部分代码展示,需要的私信我

    import SwiftUI
    
    
    struct MJScrollView<Content: View>: UIViewRepresentable {
        ///
        /// Offset
        typealias Offset = MJContentOffset
    
        ///
        /// UIViewType
        typealias UIViewType = MJUIHostingScrollView<Content>
    
        ///
        /// content
        private let content: Content
    
        ///
        /// config
        private var configuration = MJScrollViewConfiguration<Content>()
    
        ///
        /// init
        /// - Parameters:
        ///   - axes:
        ///   - showsIndicators:
        ///   - content:
        public init(_ axes: Axis.Set = .vertical, showsIndicators: Bool = true, @ViewBuilder content: () -> Content) {
            self.content = content()
    
            configuration.axes = axes
    
            configuration.showsIndicators = showsIndicators
        }
    
        ///
        /// MAKE UI View
        /// - Parameter context:
        /// - Returns:
        public func makeUIView(context: Context) -> UIViewType {
            MJUIHostingScrollView(rootView: content)
        }
    
        ///
        /// update ui view
        /// - Parameters:
        ///   - uiView:
        ///   - context:
        public func updateUIView(_ uiView: UIViewType, context: Context) {
            uiView.isUserInteractionEnabled = context.environment.isEnabled
    
            uiView.configuration = configuration.updating(from: context.environment)
    
            uiView.rootView = content
        }
    }
    
    ///
    /// config
    extension MJScrollView {
        ///
        /// alwaysBounceVertical
        /// - Parameter alwaysBounceVertical:
        /// - Returns:
        public func alwaysBounceVertical(_ alwaysBounceVertical: Bool) -> Self {
            then({ $0.configuration.alwaysBounceVertical = alwaysBounceVertical })
        }
    
        ///
        /// alwaysBounceHorizontal
        /// - Parameter alwaysBounceHorizontal:
        /// - Returns:
        public func alwaysBounceHorizontal(_ alwaysBounceHorizontal: Bool) -> Self {
            then({ $0.configuration.alwaysBounceHorizontal = alwaysBounceHorizontal })
        }
    
        ///
        /// onOffsetChange
        /// - Parameter body:
        /// - Returns:
        public func onOffsetChange(_ body: @escaping (Offset) -> ()) -> Self {
            then({ $0.configuration.onOffsetChange = body })
        }
    
        ///
        /// / content inset
        /// - Parameter contentOffset:
        /// - Returns:
        public func contentOffset(_ contentOffset: Binding<CGPoint>) -> Self {
            then({ $0.configuration.contentOffset = contentOffset })
        }
    
        ///
        /// / content inset
        /// - Parameter contentInset:
        /// - Returns:
        public func contentInset(_ contentInset: UIEdgeInsets) -> Self {
            then({ $0.configuration.contentInset = contentInset })
        }
    
        ///
        /// content inset
        /// - Parameter contentInset:
        /// - Returns:
        public func contentInset(_ contentInset: EdgeInsets) -> Self {
            self.contentInset(.init(
                    top: contentInset.top,
                    left: contentInset.leading,
                    bottom: contentInset.bottom,
                    right: contentInset.trailing
            ))
        }
    
        ///
        /// content inset
        /// - Parameters:
        ///   - edges:
        ///   - length:
        /// - Returns:
        public func contentInset(_ edges: Edge.Set = .all, _ length: CGFloat = 0) -> Self {
            var contentInset = self.configuration.contentInset
            if edges.contains(.top) {
                contentInset.top += length
            }
            if edges.contains(.leading) {
                contentInset.left += length
            }
            if edges.contains(.bottom) {
                contentInset.bottom += length
            }
            if edges.contains(.trailing) {
                contentInset.right += length
            }
            return self.contentInset(contentInset)
        }
    }
    
    ///
    /// mj event
    extension MJScrollView {
        ///
        /// on refresh
        /// - Parameter body:
        /// - Returns:
        public func onRefresh(_ body: @escaping () -> ()) -> Self {
            then({ $0.configuration.onRefresh = body })
        }
    
        ///
        /// is view refreshing
        /// - Parameter isRefreshing:
        /// - Returns:
        public func isRefreshing(_ isRefreshing: Bool) -> Self {
            return then({ $0.configuration.isRefreshing = isRefreshing })
        }
    
        ///
        /// on loading
        /// - Parameter body:
        /// - Returns:
        public func onLoading(_ body: @escaping () -> ()) -> Self {
            then({ $0.configuration.onLoading = body })
        }
    
        ///
        /// view is loading
        /// - Parameter isLoading:
        /// - Returns:
        public func isLoading(_ isLoading: Bool) -> Self {
            then({ $0.configuration.isLoading = isLoading })
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:SwiftUI 完美实现 上拉加载,下拉刷新

        本文链接:https://www.haomeiwen.com/subject/rwakhltx.html