Custom Control Development set for iOS development series-custom UITableViewCell slide Menu Control Development

Source: Internet
Author: User

Custom Control Development set for iOS development series-custom UITableViewCell slide Menu Control Development

In many apps, UITableViewCell slides left and displays menus such as the system delete button. However, the system can only have one menu, there are many requirements that require personalization. You can not only put text but also put buttons to modify the background color to create multiple menu items. Therefore, the system certainly does not provide the appropriate options, so we need to create them manually.
The Code is as follows:
The WHC_MenuCell.h header file is as follows:

/// WHC_MenuCell.m // WHC_MenuCell /// Created by Wu haichao on 15/4/3. // Copyright (c) 2015 Sinosun Technology Co ., ltd. all rights reserved. // # import
  
   
@ Class WHC_MenuCell; @ protocol WHC_MenuCellDelegate
   
    
-(BOOL) WHC_MenuCell :( WHC_MenuCell *) whcCell didPullCell :( NSInteger) index; // pull the tableView callback @ end @ interface WHC_MenuCell: UITableViewCell @ property (nonatomic, assign) CGFloat comment; // total menu width @ property (nonatomic, retain) NSArray * menuItemTitles; // the title of each menu @ property (nonatomic, retain) NSArray * menuItemTitleColors; // The text color of each menu @ property (nonatomic, retain) NSArray * menuItemBackImages; // The background image of each menu @ property (nonatomic, retain) NSArray * menuItemNormalImages; // The normal image of each menu @ property (nonatomic, retain) NSArray * menuItemSelectedImages; // The image selected by each menu @ property (nonatomic, retain) NSArray * menuItemBackColors; // The background color of each menu @ property (nonatomic, retain) NSArray * menuItemWidths; // The width of each menu @ property (nonatomic, strong) UIView * ContentView; // custom content view @ property (nonatomic, assign) CGFloat fontSize; // font size @ property (nonatomic, assign) NSInteger index; // cell subscript @ property (nonatomic, assign) id
    
     
Delegate; // cell proxy // click the menu item-(void) clickMenuItem :( UIButton *) sender; // close the menu-(BOOL) closeCellWithAnimation :( BOOL) B; // close the batch menu-(BOOL) closeCellWithTableView :( UITableView *) tableView index :( NSInteger) index animation :( BOOL) B; // start or open the menu-(void) startScrollviewCell :( BOOL) state x :( CGFloat) moveX; // end the menu-(void) didEndScrollViewCell :( BOOL) state; @ end
    
   
  

The WHC_MenuCell.m source file is as follows:

/// WHC_MenuCell.m // WHC_MenuCell /// Created by Wu haichao on 15/4/3. // Copyright (c) 2015 Sinosun Technology Co ., ltd. all rights reserved. // # import "WHC_MenuCell.h" # define KWHC_MENUCELL_ANMATION_PADING (10.0) @ interface WHC_MenuCell ()
  
   
{BOOL _ isOpen; // whether to open the menu BOOL _ isScorllClose; // whether to scroll down the menu CGFloat _ startX; // start to touch the x coordinate UIView * _ menuView after the menu is opened; // menu view Details * _ panGesture; // gesture} @ end @ implementation WHC_MenuCell // initialize UI-(void) initUI {_ panGesture = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (handpolicangesture :)]; _ panGesture. delegate = self; [self. contentView addGestureRecognizer: _ panGesture]; UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleTapGestrue :)]; tapGesture. delegate = self; [self. contentView attributes: tapGesture]; if (_ menuItemTitles = nil) {_ menuItemTitles = @ [];} if (_ menuItemBackImages = nil) {_ menuItemBackImages = @ [];} if (_ menuItemBackColors = nil) {_ menuItemBackColors = @ [[UIColor redColor];} If (_ menuItemTitleColors = nil) {_ menuItemTitleColors = @ [[UIColor blackColor];} if (_ menuItemWidths = nil) {_ menuItemWidths = @ [];} if (_ menuItemNormalImages = nil) {_ menuItemNormalImages = @ [];} if (_ menuItemSelectedImages = nil) {_ menuItemSelectedImages = @ [];} CGFloat _ menuViewX = CGRectGetWidth (_ ContentView. frame)-_ menuViewWidth; _ menuView = [[UIView alloc] initWithFrame: CGRectMake (_ me NuViewX + CGRectGetMinX (_ ContentView. frame), 0.0, _ menuViewWidth, CGRectGetHeight (_ ContentView. frame)]; _ menuView. backgroundColor = [UIColor clearColor]; [self. contentView insertSubview: _ menuView belowSubview: _ ContentView]; NSInteger menuItemCount = _ menuItemTitles. count; NSInteger menuBackImageCount = _ menuItemBackImages. count; NSInteger menuBackColorCount = _ menuItemBackColors. count; NSInteger MenuTitleColorCount = _ menuItemTitleColors. count; NSInteger menuItemWidthCount = _ menuItemWidths. count; NSInteger menuItemNormalImageCount = _ menuItemNormalImages. count; NSInteger menuItemSelectedImageCount = _ menuItemSelectedImages. count; CGFloat btnWidth = _ menuViewWidth/(CGFloat) menuItemCount; CGFloat (^ currentWidth) (NSInteger I) = ^ (NSInteger I) {CGFloat width = 0.0; for (NSInteger j = 0; j <= I; j ++) {width + = [_ menuItemWidths [j] floatValue];} return width ;}; // create a menu button for (NSInteger I = 0; I <menuItemCount; I ++) {UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. tag = I; CGRect btnRc = CGRectMake (I * btnWidth, 0.0, btnWidth, CGRectGetHeight (_ ContentView. frame); btn. frame = btnRc; if (menuItemWidthCount = menuItemCount) {btnRc. origin. x = currentWidth (I-1); B TnRc. size. width = [_ menuItemWidths [I] floatValue]; btn. frame = btnRc;} [btn setTitle: _ condition [I] forState: UIControlStateNormal]; NSInteger titleColorIndex = I; if (titleColorIndex> = menuTitleColorCount) {titleColorIndex = menuTitleColorCount-1; if (titleColorIndex <0) {titleColorIndex = 0 ;}} if (titleColorIndex <menuTitleColorCount) {[btn setTitleColor: _ menuItemTitleColors [titleColorIn Dex] forState: UIControlStateNormal];} NSInteger imageIndex = I; if (imageIndex> = menuBackImageCount) {imageIndex = menuBackImageCount-1; if (imageIndex <0) {imageIndex = 0 ;}} if (imageIndex> menuBackImageCount) {[btn labels: [UIImage imageNamed: _ menuItemBackImages [imageIndex] forState: UIControlStateNormal];} NSInteger colorIndex = I; if (colorIndex> = menuBackColorCount) {colorInd Ex = menuBackColorCount-1; if (colorIndex <0) {colorIndex = 0 ;}} if (colorIndex <menuBackColorCount) {btn. backgroundColor = _ menuItemBackColors [colorIndex];} if (I <menuItemNormalImageCount) {NSString * imageName = _ menuItemNormalImages [I]; if (imageName! = Nil & imageName. length) {[btn setImage: [UIImage imageNamed: imageName] forState: UIControlStateNormal];} if (I <strong) {NSString * selectedImageName = _ menuItemSelectedImages [I! = Nil & selectedImageName. length) {[btn setImage: [UIImage imageNamed: selectedImageName] forState: UIControlStateHighlighted] ;}} btn. titleLabel. minimumScaleFactor = 0.1; btn. titleLabel. adjustsFontSizeToFitWidth = YES; if (_ fontSize = 0.0) {_ fontSize = 18.0;} btn. titleLabel. font = [UIFont boldSystemFontOfSize: _ fontSize]; [btn addTarget: self action: @ selector (clickMenuItem :) forControlEvents: UICon TrolEventTouchUpInside]; [_ menuView addSubview: btn] ;}}// load xib-(void) awakeFromNib {[self initUI];}-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]; if (self) {} return self ;} // set the menu close status when scrolling the list-(void) setIsScrollClose {_ isScorllClose = NO;} // The following two methods are subclass implemented by the touch listening method-(void) startScrollvie WCell :( BOOL) state x :( CGFloat) moveX {}-(void) didEndScrollViewCell :( BOOL) state {} // click the menu item-(void) clickMenuItem :( UIButton *) sender {[self closeCellWithAnimation: YES];} // close multiple cell menus on tableview in batches-(BOOL) closeCellWithTableView :( UITableView *) tableView index :( NSInteger) index animation :( BOOL) B {NSArray * indexPathArr = [tableView indexPathsForVisibleRows]; BOOL handleResult = NO; for (NSIndexPath * indexPath In indexPathArr) {if (_ index! = IndexPath. row & index>-1) {WHC_MenuCell * cell = (WHC_MenuCell *) [tableView cellForRowAtIndexPath: indexPath]; [cell setIsScrollClose]; if ([cell closeCellWithAnimation: B]) {handleResult = YES ;}} else if (index <=-1) {WHC_MenuCell * cell = (WHC_MenuCell *) [tableView cellForRowAtIndexPath: indexPath]; if (_ index! = IndexPath. row) {[cell setIsScrollClose];} if ([cell closeCellWithAnimation: B]) {handleResult = YES ;}} return handleResult;} // close the cell menu-(BOOL) closeCellWithAnimation :( BOOL) B {BOOL isClose = NO; if (_ isOpen) {isClose = YES; if (B) {[UIView animateWithDuration: 0.2 animations: ^ {_ ContentView. transform = CGAffineTransformMakeTranslation (0.0, 0.0);} completion: ^ (BOOL finished) {_ isOpen = NO; [self didEndScrollViewCell: _ isOpen] ;}];} else {_ ContentView. transform = CGAffineTransformMakeTranslation (0.0, 0.0); _ isOpen = NO; [self didEndScrollViewCell: _ isOpen];} return isClose;}-(void) setSelected :( BOOL) selected animated :( BOOL) animated {[super setSelected: selected animated: animated]; // Configure the view for the selected state} // gesture processing-(void) handpolicangesture :( UIPanGestureRecognizer *) panGesure {switch (panGesure. state) {case UIGestureRecognizerStateBegan: {_ startX = _ ContentView. transform. tx; _ isScorllClose = [_ delegate WHC_MenuCell: self didPullCell: _ index];} break; case when: {if (_ isScorllClose & _ isOpen = NO) {return ;} CGFloat currentX = _ ContentView. transform. tx; CGFloat moveDistanceX = [panGesure translationInView: panGesure. view]. x; CGFloat velocityX = [panGesure velocityInView: panGesure. view]. x; CGFloat moveX = _ startX + moveDistanceX; if (velocityX> 0) {// right if (currentX> = inflow) {[panGesure setTranslation: CGPointMake (inflow, 0.0) inView: panGesure. view]; break ;}} else {if (currentX <-_ menuViewWidth) {moveX = currentX-0.4; [panGesure setTranslation: CGPointMake (moveX, 0.0) inView: panGesure. view] ;}}_ ContentView. transform = transform (moveX, 0.0); [self startScrollviewCell: _ isOpen x: moveDistanceX];} break; case when: {_ isScorllClose = NO; if (_ ContentView. transform. tx> 0.0) {[UIView animateWithDuration: 0.2 animations: ^ {_ ContentView. transform = transform (-finished, 0.0);} completion: ^ (BOOL finished) {_ isOpen = NO; [self didEndScrollViewCell: _ isOpen]; [UIView animateWithDuration: 0.2 animations: ^ {_ ContentView. transform = CGAffineTransformMakeTranslation (0.0, 0.0);}] ;}];} else if (_ ContentView. transform. tx <0) {CGFloat tx = fabsf (_ ContentView. transform. tx); if (tx <_ menuViewWidth/2.0 | (tx <_ menuViewWidth & _ isOpen) {[UIView animateWithDuration: 0.2 animations: ^ {_ ContentView. transform = CGAffineTransformMakeTranslation (0.0, 0.0);} completion: ^ (BOOL finished) {_ isOpen = NO; [self didEndScrollViewCell: _ isOpen];} else {[UIView animateWithDuration: 0.2 animations: ^ {_ ContentView. transform = CGAffineTransformMakeTranslation (-_ menuViewWidth, 0.0);} completion: ^ (BOOL finished) {_ isOpen = YES; [self didEndScrollViewCell: _ isOpen];}] ;}}} break; default: break; }}- (void) handleTapGestrue :( UITapGestureRecognizer *) tapGesture {}# pragma mark-transform-(BOOL) Restart :( UIGestureRecognizer *) else {if ([gestureRecognizer isKindOfClass: [writable class]) {return [_ delegate WHC_MenuCell: self didPullCell:-1];} else if ([gestureRecognizer isKindOfClass: [Classification class]) {UIPanGestureRecognizer * panGesture = (UIPanGestureRecognizer *) gestureRecognizer; CGPoint velocityPoint = [panGesture velocityInView: panGesture. view]; if (fabsf (velocityPoint. x)> fabsf (velocityPoint. y) {return YES;} else {_ isScorllClose = [_ delegate WHC_MenuCell: self didPullCell:-1]; return _ isScorllClose;} return NO;} @ end
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.