Swift encapsulation of a countdown Label with animation, swiftlabel

Source: Internet
Author: User

Swift encapsulation of a countdown Label with animation, swiftlabel

Wow, it's all about in the morning. (sorry, I am still writing a blog, but thanks for the time. There is nothing wrong with my summary. I hope I can make progress with you ), finally, I wrote a countdown Label that I suddenly wanted to implement yesterday. It feels like logging on to the app now. There will be a lot of places to get the verification code, I hope to confirm the shortcomings!

Let's get started with other things. Next, let's get started!

I. Rewrite buttonOf initMethod

The Code is as follows:

Convenience init (count: Int, frame: CGRect, var color: UIColor ?) {Self. init (frame: frame) // if a color is set, the color is displayed. if no color is set, the default if color = nil {color = normal_bgColor} else {normal_bgColor = color! } Self. backgroundColor = color self. startCount = count self. originNum = count self. addLabel () super. addTarget (self, action: Selector ("startCountDown"), forControlEvents: UIControlEvents. touchUpInside )}

The init method is rewritten here, followedcount(Countdown duration ),frame,colorThe three parameterscolorYes, it can be set with or without morality, so it is declared as an optional value (Opational Value)And declared as variable (varWhen you do not set the color value, the default value isnormal_bgColorThis color (set in source code ).

2. Enable the timer and start the countdown
// Enable the timer func startCountDown () {self. timer = NSTimer. scheduledTimerWithTimeInterval (1, target: self, selector: Selector ("countDown"), userInfo: nil, repeats: true) self. backgroundColor = enabled_bgColor // self. setTitleColor (UIColor. blackColor (), forState: UIControlState. disabled) self. enabled = false // The animation starts to switch self. animaType {case. CHWBtnTypeScale: self. numAnimation () case. CHWBtnTypeRotate: self. rotateAnimation ()} println ("pass ")}

Here I have setbuttonTwo types, one is the effect of amplification and disappearanceCHWBtnTypeScale, One is the effect of rotating and narrowing down and disappearingCHWBtnTypeRotate, These two types are defined as enumeration types, as follows:

enum CountBtnType {    case CHWBtnTypeScale    case CHWBtnTypeRotate}

In this way, I can flexibly set my countdown Label and use different effects. I will not talk much about the swift enumeration. If I am not clear about it, I can see it.Here. The animation part is relatively simple. I will not mention it here. The download link of the code will be attached later. If you need it, you can download it.

Iii. Use

It is actually very easy to use.CHWButton.swiftThis file is pulled into your project, and then you can create an object of this type. Two examples are listed here:

        let btn = CHWButton(count: 5, frame: CGRectMake(50, 100, 100, 50), color:nil)        btn.animaType = CountBtnType.CHWBtnTypeScale        btn.layer.masksToBounds = true        btn.layer.cornerRadius = 5        self.view.addSubview(btn)        let btn2 = CHWButton(count: 5, frame: CGRectMake(200, 100, 100, 50), color:UIColor.cyanColor())        btn2.enabled_bgColor = UIColor.greenColor()        btn2.animaType = CountBtnType.CHWBtnTypeRotate        btn2.layer.masksToBounds = true        btn2.layer.cornerRadius = 5        self.view.addSubview(btn2)

The effects of the two types are as follows:
1.CHWBtnTypeScale

2.CHWBtnTypeRotate

Here is just a simple result, mainly to practice the encapsulation ideas of swift and swift, so we should first prepare for the arrival of the swift trend. Well, today is the end of the day, happy Bayi Army Day, coming to bed, good night everyone!

Finally, you canHereDownload source code!

Copyright Disclaimer: This article is an original article by the blogger. Please indicate the source for reprinting. Thank you.

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.