ãµã³ãã«
ã»ã«ãå³æ¹åã«ã¹ã¯ã¤ãããã¨
ã¹ã©ã¤ãã¢ãã¡ã¼ã·ã§ã³ãå§ã¾ãä¸ã«é ãã¦ãããã¥ã¼ã姿ãç¾ã。
éããã£ãç¶æ 。
ãã®å¾å·¦ã«ã¹ã¯ã¤ãããã、ä»ã®ã»ã«ãã¹ã¯ã¤ãããã¨ã¹ã©ã¤ããéãã。
å®è£
ãã¸ãã¯
ã¹ã¯ã¤ãã¤ãã³ã㯠RootViewController ã§åãåã、ã»ã«ã®éãéãã管çãã。ãã¥ã¼ã®é ç½®
é常表示ã®ãã¥ã¼(BaseView)ã®ä¸ã«ã¹ã©ã¤ãæã«ç¾ãããã¥ã¼(SlideView)ãéãã¦ãã。æ®æ®µã¯ SlideView ã¯é ãã¦è¦ããªã。
ä¸æçã«é çªãå
¥ãæ¿ãã¦å
容ã確èª。SlideView ã¯ãããªæã。
ã¹ã¯ã¤ãã®å¦ç
RootViewController ã«UIGestureRecognier ãå·¦å³ä¸¡æ¹åã«ã¤ãã¦ç»é²ãã¦ãã。
- (void)viewDidLoad { [super viewDidLoad]; : UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeCell:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.tableView addGestureRecognizer:swipeGesture]; [swipeGesture release]; swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeCell:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.tableView addGestureRecognizer:swipeGesture]; [swipeGesture release]; }ãã³ãã©ã¯ãã。
- (void)didSwipeCell:(UISwipeGestureRecognizer*)swipeRecognizer { CGPoint loc = [swipeRecognizer locationInView:self.tableView]; NSIndexPath* indexPath = [self.tableViewindexPathForRowAtPoint:loc]; CustomCell* cell = (CustomCell*)[self.tableViewcellForRowAtIndexPath:indexPath]; if ([openedIndexPath_ isEqual:indexPath]) { if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) { // close cell [cell setSlideOpened:NO animated:YES]; openedIndexPath_ = nil; } } else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight) { if (openedIndexPath_) { // close previous opened cell NSArray* visibleIndexPaths = [self.tableView indexPathsForVisibleRows]; if ([visibleIndexPaths containsObject:openedIndexPath_]) { CustomCell* openedCell = (CustomCell*)[self.tableView cellForRowAtIndexPath:openedIndexPath_]; [openedCell setSlideOpened:NO animated:YES]; } } // open new cell [cell setSlideOpened:YES animated:YES]; openedIndexPath_ = indexPath; } }ã¢ã¼ããã¯ãã£ã§èª¬æãã②③④、②’③’ãå®è¡ãã。éããã®ã¯å³æ¹åã®ã¹ã¯ã¤ãã®æã ã、ã¾ãéããæã¯å·¦ã¹ã¯ã¤ãã®æã ãæå¹ã«ãã¦ãã。ã¹ã¯ã¤ãã®çµæéããç¶æ ã®ã»ã«ãåå¨ããå ´åã¯ãã®å ´æã openedIndexPath_ ã¸åã£ã¦ãã。
ã¹ã©ã¤ãã¢ãã¡ã¼ã·ã§ã³
ã«ã¹ã¿ã ã»ã«ã«ã¹ã©ã¤ãã¢ãã¡ã¼ã·ã§ã³ç¨ã®ã¡ã½ãããç¨æãã。
@interface CustomCell : UITableViewCell { BOOL slideOpened_; } : - (void)setSlideOpened:(BOOL)slideOpened animated:(BOOL)animated;ç¡é§ãªéãéããé²ãçº、ç¾å¨ã®ç¶æ ã slideOpened_ ã«ã¨ã£ã¦ããã¦ãã。å®è£ ã¯ãããªæã。
- (void)setSlideOpened:(BOOL)slideOpened animated:(BOOL)animated { if (slideOpened == slideOpened_) { return; } slideOpened_ = slideOpened; if (animated) { if (slideOpened_) { // open slide [UIViewanimateWithDuration:0.2 animations:^{ CGRect frame = self.baseView.frame; frame.origin.x += frame.size.width; self.baseView.frame = frame; }]; } else { // close slide [UIViewanimateWithDuration:0.1 animations:^{ CGRect frame = self.baseView.frame; frame.origin.x = 0; self.baseView.frame = frame; }]; } } else { CGRect frame = self.baseView.frame; if (slideOpened_) { // open slide frame.origin.x += frame.size.width; } else { // close slide frame.origin.x = 0; } self.baseView.frame = frame; } }ããããã¢ãã¡ã¼ã·ã§ã³æã・ç¡ãã®ã±ã¼ã¹ãç¨æãã¦ããã®ã¯、ã»ã«ãåæ表示ããå ´åãªã©ã¢ãã¡ã¼ã·ã§ã³ãä¸è¦ãªã±ã¼ã¹ããããã。ã¢ãã¡ã¼ã·ã§ã³å¦çèªä½ã¯ UIViewanimateWithDuration:animations: ã使ããã¥ã¼ã®ä½ç½®ãå¤ãã¦ããã ãã®åç´ãªãã®。éãã¨ãã¨éããã¨ãã§ãããæéãå¤ãã¦ã¿ã。
ã»ã«ã®è¡¨ç¤º
ã»ã«ã¯åå©ç¨ããããã、表示ã®åº¦ã«ã¢ãã«ã®ç¶æ ããã¡ãã¨åæ ãã¦ããå¿ è¦ããã。
RootViewController.m - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { : // Configure the cell. : if ([openedIndexPath_ isEqual:indexPath]) { [cell setSlideOpened:YES animated:NO]; } else { [cell setSlideOpened:NO animated:NO]; } return cell; }æåãç»åã®è¨å®ã®ä»、ã¹ã©ã¤ãã®ééç¶æ ãåæ ãã¦ãã。ãã®å¤æã«ã¹ã¯ã¤ãã¸ã§ã¹ãã£ã®å¦çã§åã£ã¦ããã openedIndexPath_ ã使ã。
é¸æç¶æ ã®èªåå®è£
UITableViewCellä¸ã§ãã¥ã¼ãï¼æéãã¦ããå ´åã§é¸æããæã«ã¯ä¸ã®ãã¥ã¼ä¸ã®ã³ã³ããã¼ã«ã表示ããã¦ãã¾ã。UITableViewCell ã¯é¸æç¶æ ã®æã«ãã®ä¸ã«é ç½®ããã¦ãããã¹ã¦ã®ãã¥ã¼ã«å¯¾ã㦠-setHilight:YES ã®ã¡ãã»ã¼ã¸ãéãããã (ããã«ãã®å ´åã¯èæ¯ã¨ãªã£ã¦ãããã¥ã¼ãããåã«è¡¨ç¤ºãããããã )。
ãããåé¿ããã«ã¯æ¨æºã®é¸æç¶æ ãæ¶ããä¸ã§èªåã§æç»ãã。
ã¾ã SelectionãNoneã«ãã。
次ã«ã«ã¹ã¿ã ãã¥ã¼ã¯ã©ã¹ BaseView ãç¨æã、é¸ææã®æç»ãè¡ããã。åè²ã ã¨å³æ°ãªãã®ã§é¸æç¶æ ã®è¡¨ç¤ºã«ã°ã©ãã¼ã·ã§ã³ãããã¦ã¿ã。
@interface BaseView : UIView { } @property (nonatomic, assign) BOOL selected; @end @implementation BaseView @synthesize selected;
- (void)drawRect:(CGRect)rect { // draw if (selected) { CGContextRef context = UIGraphicsGetCurrentContext(); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGFloat components[] = { 0.9f, 0.9f, 0.9f, 0.9f, 0.7f, 0.7f, 0.7f, 0.7f }; size_t count = sizeof(components)/ (sizeof(CGFloat)* 4); CGContextAddRect(context, self.frame); CGRect frame = self.bounds; CGPoint startPoint = frame.origin; CGPoint endPoint = frame.origin; endPoint.y = frame.origin.y + frame.size.height; CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, components, NULL, count); CGContextDrawLinearGradient(context, gradientRef, startPoint, endPoint, kCGGradientDrawsAfterEndLocation); CGGradientRelease(gradientRef); CGColorSpaceRelease(colorSpaceRef); } } @endãªãSelection ã Noneã«ããã¨æ¼ãã¦ããéã®ãã¤ã©ã¤ãç¶æ ã®è¡¨ç¤ºãå¤ãããªããªãã®ã§ãããæå½ãã¦ãã。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { UIColor* selectedColor = [UIColor whiteColor]; // default color if (highlighted) { selectedColor = [UIColor lightGrayColor]; } self.baseView.backgroundColor = selectedColor; [super setHighlighted:highlighted animated:animated]; }ããã§é¸æç¶æ ã¯ãããªã£ã。
å½±ãä»ãã
ã»ã«ãã¹ã©ã¤ããã¦éãã¦ããç¶æ ã¯ãã®ã¾ã¾ã ã¨ãããªæã。
å¹³é¢çã§å°ã å³æ°ãªã。å°ãç«ä½æãæãããçºã«å½±ãè½ã¨ããã¨ã«ãã。ããã§ã«ã¹ã¿ã ãã¥ã¼ SlideView ã«å½±ãæã。
@interface SlideView : UIView { } @end @implementation SlideView #define CUSTOMCELL_OBJECT_LENGTH 10.0 #define CUSTOMCELL_SHADOW_OFFSET 5.0 #define CUSTOMCELL_SHADOW_BLUR 5.0 - (void)drawRect:(CGRect)rect { // draw edge shadow CGRect frame = self.bounds; frame.origin.x -= CUSTOMCELL_OBJECT_LENGTH; frame.origin.y -= CUSTOMCELL_OBJECT_LENGTH; frame.size.width += CUSTOMCELL_OBJECT_LENGTH; frame.size.height = CUSTOMCELL_OBJECT_LENGTH; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShadow(context,CGSizeMake( CUSTOMCELL_SHADOW_OFFSET, CUSTOMCELL_SHADOW_OFFSET), CUSTOMCELL_SHADOW_BLUR); [[UIColorwhiteColor] setFill]; CGContextFillRect(context, frame); } @endå·¦ä¸ã®æç»æ å¤ã«ç©å½¢ãæããã®å½±ã®é¨åã ã表示ããããã«ããã¨ããããã°ãã¨ãªã。
ã½ã¼ã¹ã³ã¼ã
GitHub ããã©ãã。
CustomCellSample at 2011-06-29b from xcatsan/iOS-Sample-Code - GitHub
ãã®ä»
ãã¹ã¦ã®ã»ã«ã«å¯¾ãã¦éããæã«è¡¨ç¤ºããããã¥ã¼(SlideView)ãç¨æããã®ã¯ã¡ã¢ãªæ¶è²»ã®è¦³ç¹ããç¡é§ãããããªã。ããæ¹è¯ããã¨ããã°、ã¹ã¯ã¤ããè¡ãããæã« SlideView ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ã、éãç´åã«ã»ã«ã¸è²¼ãä»ããæ¹æ³ãèãããã。ã¾ãéããå¾ã¯ã»ã«ããåãé¤ãã¦å»æ£ãã。ã»ã«ã®è¡¨ç¤ºæ å ±ãå¤ãç»é¢ãéããªã£ã¦ãã¾ã£ãæã«ã¯ãããã£ãæ¹æ³æ³ã¯æå¹ã ã¨æããã。
åèæ å ±
ã«ã¹ã¿ã ã»ã«ã®ã·ãªã¼ãº
Cocoaã®æ¥ã : [iOS] UINib ã使ã£ãã«ã¹ã¿ã UITableViewCell ã®ä½ãæ¹Cocoaã®æ¥ã : [iOS] UINib ã使ã£ãã«ã¹ã¿ã UITableViewCell ã®ä½ãæ¹(ãã®ï¼)ãã¿ã³ã®å¦ç
Cocoaã®æ¥ã : [iOS] UINib ã使ã£ãã«ã¹ã¿ã UITableViewCell ã®ä½ãæ¹(ãã®ï¼)ãã¿ã³ã®å¦ç[æ¹è¯ç]
ä»åã®ã³ã¼ãã¯ãããããã¼ã¹ã«ä½ã£ã¦ãã。
ã°ã©ãã¼ã·ã§ã³
Cocoaã®æ¥ã : [iOS][Mac] Core Graphics - ã°ã©ãã¼ã·ã§ã³ã°ã©ãã¼ã·ã§ã³ã®æãæ¹ã¯ååæ¸ãã。