æ°åãã¼ãã¼ããã©ããä½ã£ã¦ã¿ã¾ãã。
ãã¼ããããã¿ããããã¨æ¨æºãã¼ãã¼ãã®ããã«ãããã¢ãããã¾ã。
CoreGraphicsã®é¢æ°ã使ã£ã¦å
¨é¨èªåã§æãã¦ã¾ã。
lakesoft/LKNumberPad - GitHub
ã¤ã³ã¹ãã¼ã«
1. GitHubããããã¸ã§ã¯ãããã¦ã³ãã¼ãããå¾ Xcode 4 ã§éã。
2. ãã®ä¸ãã LKNumberPadView.h 㨠LKNumberPadView.m ãèªåã®ããã¸ã§ã¯ãã¸ã³ãã¼ãã¦è¿½å ãã。
使ãæ¹
Xib ã使ãå ´åã¯æ¬¡ã®éã。
1. 表示ããããã¥ã¼ãéã、UIView ãè²¼ãã¤ããå¾、ã¯ã©ã¹ã LKNumberPadView ã«å¤æ´ãã。
æ¨å¥¨ãµã¤ãºã¯ 横 320 x 縦 54。
2. èæ¯è²(Background)ããããã£ãè¨å®ãã。
ããã ãã§è¡¨ç¤ºã§ããããã«ãªã。
次ã«ãã¿ã³ãã¿ãããããæã®åä½ãè¨è¿°ãã。
3. ã¢ã¦ãã¬ãã delegate ãè¨å®ãã。※ä¾ã§ã¯ File's Owner ã«è¨å®ãã¦ãã
4. ããªã²ã¼ã LKNumberPadViewDelegate ã®ã¡ã½ãããå®è£
ãã
@protocol LKNumberPadViewDelegate
@optional
- (void)didTouchNumberPadView:(LKNumberPadView*)numberPadView touchedString:(NSString*)string;
- (void)didTouchNumberPadView:(LKNumberPadView*)numberPadView withSequentialString:(NSString*)string;
@end
- didTouchNumberPadView:touchedString:
ã¿ãããããæ¯ã«å¼ã³åºãã、æ¼ãããæ°åãæååã¨ãã¦ï¼æåã ã渡ããã。
- didTouchNumberPadView:withSequentialString:
ã¿ãããããæ¯ã«å¼ã³åºãã、æ¼ãããæ°åãæååã¨ãã¦æ¸¡ããã。ä¸å®æéã«é£ç¶ãã¦ãã¿ã³ãã¿ãããããå ´åã¯é£ç¶ããæ°åãæååã¨ãã¦æ¸¡ããã。ä¾ãã°ï¼ãæ¼ããç´å¾ã«ããã«ï¼ãæ¼ã㨠@"14" ã渡ã£ã¦ãã。
ã«ã¹ã¿ãã¤ãº
èæ¯è²ãªã©ãããããã£ã§è¨å®ãããã¨ãã§ãã。
BOOL startWithZero
YES ã ã¨ï¼å§ã¾ãã«ãªã(ä¸çªå·¦ãï¼ã§、ä¸çªå³ãï¼)。ããã©ã«ã㯠NOã§æ¨æºã®ãã¼ãã¼ãã¨åãï¼å§ã¾ã(ä¸çªå·¦ãï¼ã§、ä¸çªå³ãï¼)。
UIColor* keyboardColor
ãã¼ãã¼ãã®ãã¼ãããã®èæ¯è²。
ããã©ã«ã㯠[UIColor colorWithWhite:0.95 alpha:1.0]。
UIColor* textColor
ãã¼ãã¼ãã®ãã¼ãããã®æåè²。
ããã©ã«ã㯠[UIColor blackColor]。
UIColor* disabledKeyboardColor
ãã£ã¼ã¼ãã«æã®ãã¼ãã¼ãã®ãã¼ãããã®èæ¯è²。
ããã©ã«ã㯠[UIColor colorWithWhite:0.85 alpha:1.0]。
UIColor* disabledTextColor
ãã£ã¼ã¼ãã«æã®ãã¼ãã¼ãã®ãã¼ãããã®æåè²
ããã©ã«ã㯠[UIColor lightGrayColor]。
NSTimeInterval sequenceInterval
ãã¼å
¥åéé(ç§)。次ã®ãã¼ããã®ééå
ã«ã¿ãããããå ´å、é£ç¶ãããã¼å
¥åã¨ã¿ãªã。
ããã©ã«ã㯠0.75ç§。
NSSet* enabledSet
ãã¼ãããã®æå¹・ç¡å¹ãæå®ãã。æå¹ã«ãããæ°å(注æ:ã¤ã³ããã¯ã¹ã§ã¯ãªã)ã NSNumber ã®å¤ã¨ãã¦å
¥ãã¦ãã。nil ã®å ´åã¯å
¨ã¦æå¹ã«ãªã。
view.enabledSet = [NSSet setWithObjects:
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2], nil];
ã½ã¼ã¹è§£èª¬
ãã¼ããã㯠UIBezierPath ãªã©ã使ãæç»ãã¦ãã。
- (void)- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect frame;
CGColorRef shadowColorRef;
UIColor* drawColor;
// (1) draw keyboard
frame = CGRectMake(0, 0,
self.bounds.size.width / LKNUMBERPADVIEW_KEYBOARD_NUM,
self.bounds.size.height);
:
}
ãã¼ãæ¼ãããæã®ãããã¢ãã表示ã«ã¯ CALayer ã使ã£ã¦ãã。ãã®æç»ãè¡ãçºã« CALayer ã®ãµãã¯ã©ã¹ LKNumberPadLayer ã LKNumberPadView.m å
ã§å®ç¾©ãã¦ãã。
enum {
LKNumberPadViewImageLeft = 0,
LKNumberPadViewImageInner,
LKNumberPadViewImageRight,
LKNumberPadViewImageMax
};
@interface LKNumberPadLayer : CALayer {
CGImageRef keytopImages_[LKNumberPadViewImageMax];
}
@property (nonatomic, copy) NSString* character;
@property (nonatomic, assign) int imageKind;
- (CGImageRef)createKeytopImageWithKind:(int)kind;
@end
ãããã¢ããã¯å·¦ã¯ã、å³ã¯ã、ãã以å¤ã®ï¼ç¨®é¡ã®è¡¨ç¤ºãå¿
è¦ã«ãªã。ãããããï¼ç¨®é¡ã®ç»åãæç»ãã¦ãã、é
å keytopImages_[] ã«åã£ã¦ãã。
ãããã¢ããã®æç»ã¯ CoreImageé¢æ°ã® CGPathAddArc() ã CGPathAddCurveToPoint() ã使ã£ã¦ãã¿ã«æ¸ãã¦ãã。
- (CGImageRef)createKeytopImageWithKind:(int)kind
{
CGMutablePathRef path = CGPathCreateMutable();
CGPoint p = CGPointMake(LKNUMBERPADVIEW_PADDING_X, LKNUMBERPADVIEW_PADDING_Y);
CGPoint p1 = CGPointZero;
CGPoint p2 = CGPointZero;
p.x += LKNUMBERPADVIEW_PAN_UPPER_RADIUS;
CGPathMoveToPoint(path, NULL, p.x, p.y);
p.x += LKNUMBERPADVIEW_PAN_UPPDER_WIDTH;
CGPathAddLineToPoint(path, NULL, p.x, p.y);
p.y += LKNUMBERPADVIEW_PAN_UPPER_RADIUS;
CGPathAddArc(path, NULL,
p.x, p.y,
LKNUMBERPADVIEW_PAN_UPPER_RADIUS,
3.0*M_PI/2.0,
4.0*M_PI/2.0,
false);
:
é ç¹ã®ã¤ã¡ã¼ã¸
ãã¼ãã¿ããããããé©åãªãããã¢ããç»åãé¸ã³ CALayer ä¸ã«è¡¨ç¤ºãã。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger index = [self _indexWithEvent:event];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[self _updateWithIndex:index];
[CATransaction commit];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger index = [self _indexWithEvent:event];
if (index != self.touchedIndex) {
[self _updateWithIndex:index];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchedIndex = -1;
self.numberPadLayer.opacity = 0.0;
[self setNeedsDisplay];
}
- (void)_updateWithIndex:(NSUInteger)index
{
self.touchedIndex = index;
NSUInteger number = [self _numberWithIndex:index];
self.touchedString = [NSString stringWithFormat:@"%d", number];
:
[self setNeedsDisplay];
[self.numberPadLayer setNeedsDisplay]; // ãããã¢ãã表示
:
}
ãããã¢ããã®æç»ã¯ -[CALayer drawInContext:] å
ã§æç»ãã。ãããããæãã¦ãããç»åã表示ã、ãã®ä¸ã«æ°åãæç»ãã。
- (void)drawInContext:(CGContextRef)context
{
CGColorRef shadowColorRef = [[UIColor colorWithWhite:0.1 alpha:1.0] CGColor];
CGContextSetShadowWithColor(context,
CGSizeMake(LKNUMBERPADVIEW_PAN_SHADOW_OFFSET_X,
LKNUMBERPADVIEW_PAN_SHADOW_OFFSET_Y),
LKNUMBERPADVIEW_PAN_SHADOW_BLUR,
shadowColorRef
);
CGImageRef imageRef = keytopImages_[self.imageKind];
CGRect imageFrame = CGRectMake(0, 0,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef));
CGContextDrawImage(context, imageFrame, imageRef);
// draw text
CGContextSelectFont(context, "Helvetica Bold", 44, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
shadowColorRef = [[UIColor whiteColor] CGColor];
CGContextSetShadowWithColor(context,
CGSizeMake(0.0,
1.0),
1.0,
shadowColorRef
);
CGContextShowTextAtPoint(context, 28, 55,
[self.character UTF8String],
[self.character length]);
}
ã©ã¤ã»ã³ã¹
MIT ã©ã¤ã»ã³ã¹ã§ã。åç¨・éåç¨ãåããå©ç¨å¯è½。ã«ã¹ã¿ãã¤ãºãã¦ã®åé
å¸ãèªç±。é£çµ¡ãä¸è¦(ã§ããããã¨ãããã)。
é¢é£æ
å ±
ãªã
åè
・ã¢ããªå¯©æ»ã®å®ç¸¾ãªã。ãã使ã£ã¦éã£ãæ¹ããããæ¯éæãã¦ä¸ãã。
・ãããã¢ãã表示ã®ä¸ã®å½±ã®é¨åã ãã¯æ¬ç©åæ§ã«ã§ããªãã£ã(æ¬ç©ã¯å½±ã®blurå¤ãå°ãã)。ãããããã¨ãªãã¨ãããªãã«æéããããã®ã§、ããã ãã¯æãæãã。æ¼ããæã«ããã¯è¦ããªãã。