Design ideas: Whenever the user clicks on an expression, the expression (djemotion) is stored in the sandbox, when the user switches to the "recent" column, from the sandbox to remove the user's most recently clicked Emoticon list, to display.
1. Save Djemotion
DJEmotion.h
#import<Foundation/Foundation.h>//because you want to save the most recently used emoticon to the sandbox, the object must implement the Nscoding protocol@interfaceDjemotion:nsobject <NSCoding>/** Emotion Chinese characters*/@property (nonatomic,copy) NSString*CHS;/** Emotion corresponding picture*/@property (nonatomic,copy) NSString*PNG;/** Emoji binary digits*/@property (nonatomic,copy) NSString*Code;@end
Djemotion.m
#import "DJEmotion.h"@implementationdjemotion/** This method is used to compare whether two objects are equal, that is: = = * Similar to the IsEqual method in Java * Overriding this method means that the overlay system is compared*/-(BOOL) IsEqual: (Djemotion *) Other {//overrides the comparison of emotion, that is, when CHS or code in the Emotion object is equal, the two objects are considered equal return[Self.chs IsEqualToString:other.chs] | |[Self.code IsEqualToString:other.code]; }//decoding-(Instancetype) Initwithcoder: (Nscoder *) Decoder {if(self =[Super Init]) {Self.chs= [Decoder Decodeobjectforkey:@"CHS"]; Self.png= [Decoder Decodeobjectforkey:@"PNG"]; Self.code= [Decoder Decodeobjectforkey:@"Code"]; } returnSelf ;}//Coding- (void) Encodewithcoder: (Nscoder *) Encoder {[Encoder encodeObject:self.chs Forkey:@"CHS"]; [Encoder encodeObject:self.png Forkey:@"PNG"]; [Encoder encodeObject:self.code Forkey:@"Code"];}@end
2. Tool classes that store emoticons in a sandbox
DJEmotionTool.h
#import <Foundation/Foundation.h>@class djemotion; @interface Djemotiontool:nsobject /* */+ (void) Saverecentemotion: (djemotion *) emotion; /* */+ (Nsarray *) recentemotions; @end
djemotiontool.m
#import "DJEmotionTool.h"//Recent expression Save path#defineDjemotionpath [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Emotion.archiver"]//static member variables similar to those in JavaStaticNsmutablearray *_recentemotions;@implementationDjemotiontool/** This method will be adjusted once for the first time using this class, and only once * similar to the static code block in Java*/+ (void) Initialize {_recentemotions=[Nskeyedunarchiver Unarchiveobjectwithfile:djemotionpath]; if(_recentemotions = =Nil) {_recentemotions=[Nsmutablearray array]; }}/** Save recently clicked Emotion*/+ (void) Saverecentemotion: (Djemotion *) Emotion {//remove the same emotion stored previously[_recentemotions removeobject:emotion]; //inserts a new emotion in the head of the array[_recentemotions insertobject:emotion Atindex:0]; //Save current Array[Nskeyedarchiver archiverootobject:_recentemotions tofile:djemotionpath];}/** Get the emotion collection stored in the sandbox recently clicked*/+ (Nsarray *) recentemotions {return_recentemotions;}@end
3. Because the most recent expression stored in the sandbox is transformed in real time, you cannot use lazy loading, so when you click the recent button, the ListView of the nearest button is assigned a value:
Djemotionkeyboard.m
#pragmaMark-djemotiontabbar's Proxy Method-(void) Emotiontabbar: (Djemotiontabbar *) TabBar Didselectedbuttontype: (djemotiontabbarbuttontype) ButtonType {//1. Empty the other view (similar to Viewgroup.removeallviews in Android) that existed before Placeview[self.placeView.subviews makeobjectsperformselector: @selector (Removefromsuperview)]; //2. Switch Contentview Switch(buttontype) { CaseDjemotiontabbarbuttontyperecent://recent[Self.placeview AddSubview:self.recentEmotionView]; //get the most recently used emoticons stored in the sandbox from djemotiontool, note! Lazy loading cannot be used here, as this will result in data not being updated in real time self.recentEmotionView.emotions = [Djemotiontool recentemotions]; Break; CaseDjemotiontabbarbuttontypedefault://default[Self.placeview AddSubview:self.defaultEmotionView]; Break; CaseDjemotiontabbarbuttontypeemoji://Emoji[Self.placeview AddSubview:self.emojiEmotionView]; Break; CaseDJEMOTIONTABBARBUTTONTYPELXH://The Little Flower of the wave[Self.placeview AddSubview:self.lxhEmotionView]; Break; default: Break; } //3. Notify the system to re-layout after the switch is complete, equivalent to call Layoutsubviews again[self setneedslayout]; }
4. When the ListView receives the new data, it empties the previous control and re-lays it (the goal is to have the new click in front of the latest expression ).
djemotionlistview.m
- (void) Setemotions: (Nsarray *) Emotions {_emotions=emotions; //clear the existing child controls inside the ScrollView to make it easy to refresh[self.emotionScrollView.subviews makeobjectsperformselector: @selector (Removefromsuperview)]; //total number of emotionNsuinteger Emotioncount =Emotions.count; //total number of page numbersNsuinteger Pagenums = (Emotioncount + djemotionpagesize-1) /djemotionpagesize; //Update Pagecontrol Display CountSelf.emotionPageControl.numberOfPages =pagenums; //Add a child view for ScrollView for(inti =0; i < pagenums; i++) {Djemotionpageview*pageview =[[Djemotionpageview alloc] init];//pageview.backgroundcolor = Djrandomcolor;Nsrange Range; Range.location= i *djemotionpagesize; Nsuinteger leftnums= Emotioncount-range.location;//Find out the current number of remaining emoji if(Leftnums >= djemotionpagesize) {//determine if the current number of remaining emoji is fullRange.length =djemotionpagesize; } Else{//If it is less than 20, then range.length equals the remaining number.Range.length =leftnums; } Nsarray*singlepageemotions =[Emotions Subarraywithrange:range]; Pageview.emotions=singlepageemotions; [Self.emotionscrollview Addsubview:pageview]; } //notifies the system to re-layout after data is added[self setneedslayout];
5. When you click the Emoji button or swipe the emoticon button, if you send an input notification, the click of the emoticon button is stored in the sandbox.
djemotionpageview.m
// Send click-to-broadcast (similar to Android, the difference is that Android broadcasts are as long as there are contextual objects in context and can be sent) // notifications sent and received in iOS are done through Nsnotificationcenter -(void) Sendbtnclicknotification: (Djemotionbutton *) btn { // Store Current emotion expression [Djemotiontool saveRecentEmotion:btn.emotion]; *userinfo = [nsmutabledictionary dictionary]; = btn.emotion; Object : nil userinfo:userinfo];}
Final effect:
Sina Weibo client (53)-record the user's most recently clicked emoticons