IOS High Performance Timer solution

Source: Internet
Author: User

Recently in an e-commerce project, because there are many interfaces have discounted items, discounted items will be time-limited, so there is a large number of timers to count the countdown. Each page is a timer. Feel too much performance, think of a more economical way of performance. Global on a timer, control all the countdown of the interface.


The project was swift3.0.


1, solve the idea:

Create a Timer class, this system is also a timer, the timer inside a proxy method, and then the timer execution method is the executor of the agent, the need to use the interface of the timer, to inherit the agent, and then implement the method of the agent line.


2. Code:

Timer class


Import UIKit


Protocol Ssttimerdelegate:class {

Func UpdateTime ()

}

Final class Ssttimer:nsobject {

Weak var delegate:ssttimerdelegate?

static Var Shared:ssttimer {

struct Static {

static Let Instance:ssttimer = Ssttimer ()

}

Return static.instance

}

Private override Init () {

Super.init ()

Timer.scheduledtimer (timeinterval:1.0, target:self, selector: #selector (didtimeralarm), Userinfo:nil, Repeats:true)

}

Func Didtimeralarm () {

Self.delegate?. UpdateTime ()


}

}


Other classes to use for timers:

1.


Class Viewcontroller:uiviewcontroller {

var i = 0

@IBOutlet weak var timelabel:uilabel!

Override Func Viewdidload () {

Super.viewdidload ()

}


Override Func Viewwillappear (_ Animated:bool) {

TimerUtil.shared.delegate = Self

}

}


Extension Viewcontroller:timerdelegate {

Func UpdateTime () {

i = i + 1;

Timelabel.text = "\ (i)"

}

}



2.

Class Viewthreevc:uiviewcontroller {

var i = 0

@IBOutlet weak var timelabel:uilabel!

Override Func Viewdidload () {

Super.viewdidload ()

}

Override Func Viewwillappear (_ Animated:bool) {

TimerUtil.shared.delegate = Self

}

}

Extension Viewthreevc:timerdelegate {

Func UpdateTime () {

i = i + 1;

Timelabel.text = "\ (i)"

}

}



This article is from the "Java" blog, so be sure to keep this source http://5378610.blog.51cto.com/5368610/1906259

IOS High Performance Timer solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.