自定义键盘 – 长按键盘button时添加多个表情符号图标
任何人有一个想法如何创build多个表情符号图标的自定义视图,而用户长按键盘。
你可以在这里find所有的细节和一些原始的Obj-C代码(gist) 。 代码不完整,但足以启动。
这里只画出背景图片代码:
- (UIImage*)hintImage { CGMutablePathRef path = CGPathCreateMutable(); CGPoint p = CGPointMake(__PADDING_X, __PADDING_Y); CGPoint p1 = CGPointZero; CGPoint p2 = CGPointZero; //1 px += __PAN_UPPER_RADIUS; CGPathMoveToPoint(path, NULL, px, py); NSUInteger hintCount = [self.hintSymbolsList count]; //!!!:magic numbers self.hintAdditionalWidth = 2*(hintCount-1)*(self.frame.size.width+__GAP_X); //2 px += __PAN_UPPDER_WIDTH + self.hintAdditionalWidth; //!!!:+hintCount*(__PAN_LOWER_WIDTH+__GAP_X); CGPathAddLineToPoint(path, NULL, px, py); //3 py += __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 3.0*M_PI/2.0, 4.0*M_PI/2.0, false); px += __PAN_UPPER_RADIUS; //4 py += __PAN_UPPER_HEIGHT - __PAN_UPPER_RADIUS; if (self.hintPosition == ACHintPositionFarRight || self.hintPosition == ACHintPositionRight) { py += - 2*__PAN_UPPER_RADIUS + __PAN_CURVE_SIZE + __PAN_MIDDLE_HEIGHT; } else { //ACHintPositionLeft & ACHintPositionFarLeft py -= __PAN_CURVE_SIZE; } CGPathAddLineToPoint(path, NULL, px, py); if (self.hintPosition == ACHintPositionFarRight || self.hintPosition == ACHintPositionRight) { //5.1 px -= __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 4.0*M_PI/2.0, 1.0*M_PI/2.0, false); py += __PAN_UPPER_RADIUS; //5.2 px -= self.hintAdditionalWidth + __PAN_UL_WIDTH-2*__PAN_UPPER_RADIUS; CGPathAddLineToPoint(path, NULL, px, py); //5.3 py += __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 3.0*M_PI/2.0, 2.0*M_PI/2.0, true); px -= __PAN_UPPER_RADIUS; } else { //ACHintPositionLeft & ACHintPositionFarLeft //5 p1 = CGPointMake(px, py + __PAN_CURVE_SIZE); px -= __PAN_UL_WIDTH; py += __PAN_MIDDLE_HEIGHT + __PAN_CURVE_SIZE*2; p2 = CGPointMake(px, py - __PAN_CURVE_SIZE); CGPathAddCurveToPoint(path, NULL, p1.x, p1.y, p2.x, p2.y, px, py); } //6 py += __PAN_LOWER_HEIGHT - __PAN_CURVE_SIZE - __PAN_LOWER_RADIUS; CGPathAddLineToPoint(path, NULL, px, py); //7 px -= __PAN_LOWER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_LOWER_RADIUS, 4.0*M_PI/2.0, 1.0*M_PI/2.0, false); //8 px -= __PAN_LOWER_WIDTH; py += __PAN_LOWER_RADIUS; CGPathAddLineToPoint(path, NULL, px, py); //9 py -= __PAN_LOWER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_LOWER_RADIUS, 1.0*M_PI/2.0, 2.0*M_PI/2.0, false); //10 px -= __PAN_LOWER_RADIUS; py -= __PAN_LOWER_HEIGHT - __PAN_LOWER_RADIUS - __PAN_CURVE_SIZE; CGPathAddLineToPoint(path, NULL, px, py); if (self.hintPosition == ACHintPositionFarRight || self.hintPosition == ACHintPositionRight) { //11 p1 = CGPointMake(px, py - __PAN_CURVE_SIZE); px -= __PAN_UL_WIDTH; py -= __PAN_MIDDLE_HEIGHT + __PAN_CURVE_SIZE*2; p2 = CGPointMake(px, py + __PAN_CURVE_SIZE); CGPathAddCurveToPoint(path, NULL, p1.x, p1.y, p2.x, p2.y, px, py); } else { //ACHintPositionLeft & ACHintPositionFarLeft //11.1 px -= __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 4.0*M_PI/2.0, 3.0*M_PI/2.0, true); py -= __PAN_UPPER_RADIUS; //11.2 px -= self.hintAdditionalWidth + __PAN_UL_WIDTH-2*__PAN_UPPER_RADIUS; CGPathAddLineToPoint(path, NULL, px, py); //11.3 py -= __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 1.0*M_PI/2.0, 2.0*M_PI/2.0, false); px -= __PAN_UPPER_RADIUS; } //12 py -= __PAN_UPPER_HEIGHT - __PAN_UPPER_RADIUS; if (self.hintPosition == ACHintPositionFarRight || self.hintPosition == ACHintPositionRight) { py += __PAN_CURVE_SIZE; } else { py -= - 2*__PAN_UPPER_RADIUS + __PAN_CURVE_SIZE + __PAN_MIDDLE_HEIGHT; } CGPathAddLineToPoint(path, NULL, px, py); //13 px += __PAN_UPPER_RADIUS; CGPathAddArc(path, NULL, px, py, __PAN_UPPER_RADIUS, 2.0*M_PI/2.0, 3.0*M_PI/2.0, false); //---- CGContextRef context; UIGraphicsBeginImageContext(CGSizeMake(__WIDTH+self.hintAdditionalWidth, __HEIGHT)); context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0.0, __HEIGHT); CGContextScaleCTM(context, 1.0, -1.0); CGContextAddPath(context, path); CGContextClip(context); //---- CGRect frame = CGPathGetBoundingBox(path); CGColorRef backColor = [UIColor whiteColor]; CGContextSetFillColorWithColor(context, backColor); CGContextFillRect(context, frame); CGImageRef imageRef = CGBitmapContextCreateImage(context); UIImage * image = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationDown]; CGImageRelease(imageRef); UIGraphicsEndImageContext(); CFRelease(path); return image; }