"Original" GitHub series two: Open source support multiple forms of animation style push Little red dot Wzlbadge (IOS)

Source: Internet
Author: User

Overview

Today we are going to implement a love and hate push "Little Red dot" Wzlbadge in iOS. So what is badge? Displays a "Little Red dot" alert user on a button or other control when there is data update in the background that needs to be known to the user. Note that the "Little Red dot" here is just a generalization, the actual view can be fanciful, in this version we first implement the following:

    • Little Red Dot
    • White "new" on red background
    • White numerals on red background

In order to make the little red dot more visible, in this version I implemented three different state animations (status animation):

    • Heart beat effect (Wbadgeanimtypescale)
    • Breathing light effect (wbadgeanimtypebreathe)
    • Transverse jitter (wbadgeanimtypeshake)
    • Quiesce State (wbadgeanimtypenone, default)

Wzlbadge also has the following advantages:

    • Support Screen
    • Support Ios5~ios8
    • Allows for highly customizable, including "red dot" background color, text (font size, color), location, etc.

Let's look at two sample pictures first:

Analysis

Want to make the implementation of the badge interface call convenient, I used to UIView do category, interface unified as an example method. In this way, you can add badge to any uiview and its subclasses, and the interface is simple. The interface should look something like this:

[Someview Showbadgewithstyle:wbadgestylereddot value:0 Animationtype:wbadgeanimtypeshake];

This is often the case, the simpler the user is, the more the interface provider needs to do. We know that a little red dot should have multiple attributes, such as size, background color, text color, and so on. To make badge more customizable, you need to open up these rich genera . So here's the problem, in theory, you can add only methods to the category, and you can't add attributes. At this point, you need some running time runtime skills. We can add properties to UIView at run time, and the two runtime APIs we need to use are:

Objc_export ID objc_getassociatedobject (ID object, const void *key)    __osx_available_starting (__mac_10_6, __iphone _3_1);

Objc_export void Objc_setassociatedobject (ID object, const void *key, ID value, objc_associationpolicy policy)    __OSX _available_starting (__mac_10_6, __iphone_3_1);

For example, suppose we now want to add the Badgebgcolor property to UIView dynamically, to manipulate the background color of the badge angle label "

-(Uicolor *) badgebgcolor//getter{    return Objc_getassociatedobject (self, &badgebgcolorkey);}

-(void) Setbadgebgcolor: (Uicolor *) badgebgcolor//setter{    objc_setassociatedobject (self, &badgebgcolorkey, Badgebgcolor, Objc_association_retain);}

The knowledge about adding properties dynamically here is not specific, want to know more can refer to my this "iOS runtime Runtime" (forced to get and modify private variables, forced addition and modification of private methods, etc.). This knowledge point is also the core point of the open source Wzlbadge of this article, which is actually the common practice of most open source tools. In addition, the other technical details of Wzlbadge are no longer detailed here, specific to the end of the article to go to GitHub to view.

How to use

Since Wzlbadge uses the way to extend category to UIView, all UIView and their inherited subclasses (Uicontrol, etc.) can be used seamlessly, and the current version of the Open interface is mainly the following:

/** *  Show badge with the red dot style and Wbadgeanimtypenone by default. */-(void) Showbadge;

/** *  Showbadge * *  @param style wbadgestyle type *  @param value (if ' style ' is Wbadgestylereddot or wbadgesty Lenew, this value would be ignored. Any value would be OK.) *   @param anitype */-(void) Showbadgewithstyle: (Wbadgestyle) style value: (nsinteger) value Animationtype: ( Wbadgeanimtype) Anitype;

/** *  Clear badge */-(void) Clearbadge;


Source code Sharing

I am hosting the source code on GitHub, and I personally hope that Wzlbadge will continue to optimize to be the ultimate solution for pushing red dots on the iOS platform, and if you're interested, I'd be glad to join you to make Wzlbadge better. Any questions or suggestions please leave a message or mention issue on GitHub.

In addition, I hope you can star/fork/watch the Wzlbadge on GitHub, and then my update will notify you directly ^ ^ ^.

GitHub Address: Wzlbadge (Https://github.com/weng1250/WZLBadge)

Original article, reproduced please indicate the program small Weng @ Blog Park, mail [email protected], welcome you and I in the c/c++/objective-c/machine vision and other fields to start communication!

"Original" GitHub series two: Open source support multiple forms of animation style push Little red dot Wzlbadge (IOS)

Related Article

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.