From 70fe2a76ff65b90f146e348b503883d6377f4f81 Mon Sep 17 00:00:00 2001 From: isaced Date: Tue, 30 Dec 2014 17:13:37 +0800 Subject: [PATCH] Add delete button --- Example/ISEmojiViewSample/ViewController.m | 7 ++ ISEmojiView/ISEmojiView.h | 5 ++ ISEmojiView/ISEmojiView.m | 97 ++++++++++++++++++---- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/Example/ISEmojiViewSample/ViewController.m b/Example/ISEmojiViewSample/ViewController.m index 452e0e6..76bb119 100644 --- a/Example/ISEmojiViewSample/ViewController.m +++ b/Example/ISEmojiViewSample/ViewController.m @@ -40,4 +40,11 @@ self.textView.text = [self.textView.text stringByAppendingString:emoji]; } +-(void)emojiView:(ISEmojiView *)emojiView didPressDeleteButton:(UIButton *)deletebutton{ + if (self.textView.text.length > 0) { + NSRange lastRange = [self.textView.text rangeOfComposedCharacterSequenceAtIndex:self.textView.text.length-1]; + self.textView.text = [self.textView.text substringToIndex:lastRange.location]; + } +} + @end diff --git a/ISEmojiView/ISEmojiView.h b/ISEmojiView/ISEmojiView.h index ea3243a..bf2e6c2 100644 --- a/ISEmojiView/ISEmojiView.h +++ b/ISEmojiView/ISEmojiView.h @@ -19,5 +19,10 @@ @protocol ISEmojiViewDelegate -(void)emojiView:(ISEmojiView *)emojiView didSelectEmoji:(NSString *)emoji; +-(void)emojiView:(ISEmojiView *)emojiView didPressDeleteButton:(UIButton *)deletebutton; + +@end + +@interface ISDeleteButton : UIButton @end \ No newline at end of file diff --git a/ISEmojiView/ISEmojiView.m b/ISEmojiView/ISEmojiView.m index 6dc9405..48985f4 100644 --- a/ISEmojiView/ISEmojiView.m +++ b/ISEmojiView/ISEmojiView.m @@ -31,8 +31,8 @@ static const CGFloat EmojiFontSize = 32; self.emojis = [NSArray arrayWithContentsOfFile:plistPath]; // - NSInteger rowNum = (CGRectGetWidth(frame) / EmojiWidth); - NSInteger colNum = (CGRectGetHeight(frame) / EmojiHeight); + NSInteger rowNum = (CGRectGetHeight(frame) / EmojiHeight); + NSInteger colNum = (CGRectGetWidth(frame) / EmojiWidth); NSInteger numOfPage = ceil((float)[self.emojis count] / (float)(rowNum * colNum)); // init scrollview @@ -51,34 +51,48 @@ static const CGFloat EmojiFontSize = 32; NSInteger column = 0; NSInteger page = 0; - for (int i = 0; i < [self.emojis count]; i++) { - - NSString *emoji = self.emojis[i]; - - // init Emoji Button - UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom]; - emojiButton.titleLabel.font = [UIFont fontWithName:@"Apple color emoji" size:EmojiFontSize]; - [emojiButton setTitle:emoji forState:UIControlStateNormal]; - [emojiButton addTarget:self action:@selector(emojiButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + NSInteger emojiPointer = 0; + for (int i = 0; i < [self.emojis count] + numOfPage - 1; i++) { // Pagination if (i % (rowNum * colNum) == 0) { page ++; // Increase the number of pages row = 0; // the number of lines is 0 column = 0; // the number of columns is 0 - }else if (i % rowNum == 0) { + }else if (i % colNum == 0) { // NewLine row += 1; // Increase the number of lines column = 0; // The number of columns is 0 } - emojiButton.frame = CGRectMake(((page-1) * frame.size.width) + (column * EmojiWidth), - row * EmojiHeight, - EmojiWidth, - EmojiHeight); - column++; + CGRect currentRect = CGRectMake(((page-1) * frame.size.width) + (column * EmojiWidth), + row * EmojiHeight, + EmojiWidth, + EmojiHeight); - [self.scrollView addSubview:emojiButton]; + if (row == (rowNum - 1) && column == (colNum - 1)) { + // last position of page, add delete button + + ISDeleteButton *deleteButton = [ISDeleteButton buttonWithType:UIButtonTypeCustom]; + [deleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + deleteButton.frame = currentRect; + deleteButton.tintColor = [UIColor blackColor]; + [self.scrollView addSubview:deleteButton]; + + }else{ + NSString *emoji = self.emojis[emojiPointer++]; + + // init Emoji Button + UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom]; + emojiButton.titleLabel.font = [UIFont fontWithName:@"Apple color emoji" size:EmojiFontSize]; + [emojiButton setTitle:emoji forState:UIControlStateNormal]; + [emojiButton addTarget:self action:@selector(emojiButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + + emojiButton.frame = currentRect; + [self.scrollView addSubview:emojiButton]; + } + + column++; } // add PageControl @@ -94,6 +108,8 @@ static const CGFloat EmojiFontSize = 32; pageControlSize.height); [self.pageControl addTarget:self action:@selector(pageControlTouched:) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.pageControl]; + + } return self; } @@ -128,4 +144,49 @@ static const CGFloat EmojiFontSize = 32; } } +- (void)deleteButtonPressed:(UIButton *)button{ + // Add a simple scale animation + CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; + animation.toValue = @0.9; + animation.duration = 0.1; + animation.autoreverses = YES; + [button.layer addAnimation:animation forKey:nil]; + + // Callback + if ([self.delegate respondsToSelector:@selector(emojiView:didPressDeleteButton:)]) { + [self.delegate emojiView:self didPressDeleteButton:button]; + } +} + +@end + +@implementation ISDeleteButton + +-(void)drawRect:(CGRect)rect{ + + // Rectangle Drawing + UIBezierPath* rectanglePath = UIBezierPath.bezierPath; + [rectanglePath moveToPoint: CGPointMake(5, 25.05)]; + [rectanglePath addLineToPoint: CGPointMake(20.16, 36)]; + [rectanglePath addLineToPoint: CGPointMake(45.5, 36)]; + [rectanglePath addLineToPoint: CGPointMake(45.5, 13.5)]; + [rectanglePath addLineToPoint: CGPointMake(20.16, 13.5)]; + [rectanglePath addLineToPoint: CGPointMake(5, 25.05)]; + [rectanglePath closePath]; + [self.tintColor setStroke]; + rectanglePath.lineWidth = 1; + [rectanglePath stroke]; + + + // Bezier Drawing + UIBezierPath* bezierPath = UIBezierPath.bezierPath; + [bezierPath moveToPoint: CGPointMake(26.5, 20)]; + [bezierPath addLineToPoint: CGPointMake(36.5, 29.5)]; + [bezierPath moveToPoint: CGPointMake(36.5, 20)]; + [bezierPath addLineToPoint: CGPointMake(26.5, 29.5)]; + [self.tintColor setStroke]; + bezierPath.lineWidth = 1; + [bezierPath stroke]; +} + @end