• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

稳定高频函数调用的技巧

2017-05-22

稳定高频函数调用

有些时候会频繁调用一些函数,但是不想每次调用都让函数进行触发,比如

  1. 高频对一个View进行动画,但是仅仅想以最后一次的数据为准做动画
  2. 疯狂点击一个button,想控制其真正生效的次数

这里以第二个例子做一些稳定的模型 这些模型仅仅能对1秒这个量级进行控制,不能针对毫秒级的控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class FrequencyButton: UIButton {

convenience public init(size: CGSize) {
self.init(frame: CGRect(origin: CGPoint.zero, size: size))
self.addTarget(self, action: #selector(touchUpInsideButton(button:)), for: .touchUpInside)
}


@objc func touchUpInsideButton(button: UIButton) {
//延迟模型
delayMethod(delay: 3)
//稳定模型
stableMethod()
//平滑模型
smoothMethod(invoke: 1.0)
}

@objc func fireMethod() {
print("Method Fired At \(Date().timeIntervalSince1970)")
}

.........
}

延迟模型

无论调用多少次,仅仅在 最后一次调用后延迟一个固定时间执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//-----------------------------------
// MARK: Delay Object
//-----------------------------------

var fireInterval: TimeInterval = 0.0

func delayMethod(delay: TimeInterval) {
let delayInterval: TimeInterval = CFAbsoluteTimeGetCurrent() + delay
if delayInterval > fireInterval {
fireInterval = delayInterval
self.perform(#selector(willFireMethodDelay), with: nil, afterDelay: fireInterval - CFAbsoluteTimeGetCurrent())
}
}

@objc func willFireMethodDelay() {
if CFAbsoluteTimeGetCurrent() >= fireInterval {
fireMethod()
}
}

稳定模型

无论进行多少次调用,仅仅在 最后一次调用前的周期内以稳定的频率执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//-----------------------------------
// MARK: Stable Object
//-----------------------------------

var stableInterval: TimeInterval = 1.0
var fired: Bool = true

func stableMethod() {
if fired == false {
return
}
fired = false
self.perform(#selector(willFireMethodStable), with: nil, afterDelay: stableInterval)
}

@objc func willFireMethodStable() {
fireMethod()
fired = true
}

平滑模型

进行了 N次 调用,以稳定的时间周期,调用N次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//-----------------------------------
// MARK: Smooth Object
//-----------------------------------

var smoothIntervals: [TimeInterval] = [TimeInterval]()
var fired: Bool = true

func smoothMethod(invoke: TimeInterval?) {
if invoke != nil {
smoothIntervals.append(invoke!)
}
if fired == false {
return
}
if smoothIntervals.count > 0 {
fired = false
self.perform(#selector(willFireMethodSmooth), with: nil, afterDelay: smoothIntervals.first!)
smoothIntervals.removeFirst()
}
}


@objc func willFireMethodSmooth() {
fireMethod()
fired = true
smoothMethod(invoke: nil)
}

赏

请问老板还招人么(/ω\)

支付宝
微信
  • iOS
  • Tips

扫一扫,分享到微信

微信分享二维码
OC的init函数中self运行时指向问题
局域网流量也需要获取数据访问权限
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。