【问题标题】:Creating a completely custom UIRefreshControl创建一个完全自定义的 UIRefreshControl
【发布时间】:2013-07-17 13:04:11
【问题描述】:

我需要创建一个完全自定义的UIRefresh 控件。动画、图片、下拉量等...

我最初的想法是从头开始,使用 UIViewController,向其中添加我自己的视图,通过访问 UIScrollViewDelegate 方法进行动画处理。

我可以做到这一切,但是有没有一种方法可以减少工作量并且更容易添加到多个 UITableViews 中?

是否可以继承 UIRefreshControl 并更改其中的这些内容?

【问题讨论】:

    标签: ios objective-c uirefreshcontrol


    【解决方案1】:

    答案已更新

    • GitHub 项目更新到 Swift 3.1
    • 将 QuartzCode 更新到版本 1.55.0(生成代码的变化)
    • 代码重构为使用新的refreshControl 属性(introduced in iOS 10)(现在也更“迅速”了)。
    • 包含@Hanny 的建议(在下方)(谢谢!)

    我喜欢您发布的 YouTube 链接。 :) 不错的结果。

    供参考:QuartzCode 非常适合从头开始创建 UIRefreshControl 动画。

    查看this little project (GitHub)。 在其中,您将找到 QuartzCode 项目文件以及如何将其与 UITableView 集成的示例。

    我认为其中最重要的部分是 refresh 函数:

    /// Called everytime refresh control's value changes.
    ///
    /// - parameter sender: The `UIRefreshControl` of this TableView.
    @IBAction func refresh(_ sender: UIRefreshControl) {
    
        animate()
    
        // In this "demo", the refresh will last 5.0 seconds.
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
    
            // Do something with the retrieved data...
            // TODO
    
            // ... then end the refresh operation.
            self.refreshControl?.endRefreshing()
    
            // Stop animations.
            self.stopAnimations()
        }
    }
    

    看看使用动画是多么容易:

    /// Three examples. Uncomment / comment to check all of them.
    func animate() {
    
        // Example 01.
        animateCloudUpAndDown()
    
        // Example 02.
        //animateCloudStrokeWithGradientFill()
    
        // Example 03.
        //animateCloudStrokeWithSolidFill()
    }
    
    // MARK: - Animation Examples
    
    /// Animates the cloud up and down.
    func animateCloudUpAndDown() {
        customUIRefreshControl.addRefreshUpDownAnimation()
    }
    
    /// "Draws" the cloud by make its stroke line gradually visible, then shows
    /// a solid blueish background and then fades everything out.
    func animateCloudStrokeWithGradientFill() {
        customUIRefreshControl.addRefreshGradientAnimation()
    }
    
    /// "Draws" the cloud by make its stroke line gradually visible, then shows
    /// a gradient blueish background and then fades everything out.
    func animateCloudStrokeWithSolidFill() {
        customUIRefreshControl.addRefreshSolidAnimation()
    }
    

    干杯!

    【讨论】:

    • 我认为 animate() 最好放在刷新函数的开头。当我们滚动浏览数据时,动画毫无意义。
    • @Hanny,非常感谢您的评论。我同意。我对代码进行了一些更改,并包含了您的建议。看一看。 ;)
    【解决方案2】:

    EGOTableViewPullRefresh 是很棒的“下拉刷新”功能。它可以在github 上找到。您可以自定义您的图片、行为等。 UIRefreshControl 仅在 iOS 6.0 及更高版本中可用。 EGOTableViewPullRefresh 可在 iOS 5 及更早版本中使用!

    【讨论】:

    • 谢谢,看起来和我自己做的一样:)
    【解决方案3】:

    我们有一个教程,其中包含 Objective-C 和 Swift 中的示例代码,用于实现自定义拉取刷新控件。你可以在这里找到它:http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

    希望对您有所帮助,如果您有任何问题,请告诉我!

    --安东尼

    【讨论】:

    • 谢谢,我终于让它工作了。 youtu.be/lN1HejGmlYI 非常少的代码,因为它完全是为了获得正确的布局约束。
    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多