Click an animated effect with a single cell height change
Effect
Description
1. When you click on a single cell, it is cumbersome to expand and scale animations, and friends who have done the relevant requirements must know the pits
2. This example only provides a solution for simplifying operations by encapsulating the cell height into model
Source
Https://github.com/YouXianMing/TableViewTapAnimation
////Model.h//tableviewtapanimation////Created by youxianming on 15/9/18.//copyright©2015 year youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@interfaceModel:nsobject@property (nonatomic) cgfloat normalheight; @property (nonatomic) cgfloat expendheight; @property (No natomic) BOOL expend;+(Instancetype) Modelwithnormalheight: (cgfloat) normalheight expendheight: (cgfloat) Expendheight expend: (BOOL) expend;@end
////MODEL.M//tableviewtapanimation////Created by youxianming on 15/9/18.//copyright©2015 year youxianming. All rights reserved.//#import "Model.h"@implementationModel+(Instancetype) Modelwithnormalheight: (cgfloat) normalheight expendheight: (cgfloat) Expendheight expend: (BOOL) Expend {Model*model =[[Model alloc] init]; Model.normalheight=Normalheight; Model.expendheight=Expendheight; Model.expend=expend; returnmodel;}@end
////InfoCell.h//tableviewtapanimation////Created by youxianming on 15/9/18.//copyright©2015 year youxianming. All rights reserved.//#import<UIKit/UIKit.h>#import "Model.h"@interfaceInfocell:uitableviewcell@property (nonatomic, weak) Nsindexpath*Indexpath, @property (nonatomic, weak) UITableView*TableView;- (void) LoadData: (ID) data;@end
////INFOCELL.M//tableviewtapanimation////Created by youxianming on 15/9/18.//copyright©2015 year youxianming. All rights reserved.//#import "InfoCell.h"@interfaceInfocell () @property (nonatomic, strong) UIButton*button; @property (nonatomic, weak) Model*model; @property (nonatomic, strong) UIView*Lineview, @property (nonatomic, strong) UILabel*Infolabel;@end@implementationInfocell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier {if(self =[Super Initwithstyle:style Reuseidentifier:reuseidentifier]) {[Self setup]; } returnSelf ;}- (void) Setup {Self.selectionstyle=Uitableviewcellselectionstylenone; Self.button= [[UIButton alloc] Initwithframe:cgrectmake (0,0, the, -)]; [Self.button addtarget:self Action: @selector (Buttonevent) forcontrolevents:uicontroleventtouchupinside]; [Self AddSubview:self.button]; Self.lineview= [[UIView alloc] Initwithframe:cgrectmake (0,49.5, the,0.5f)]; Self.lineView.backgroundColor= [[Uicolor Redcolor] Colorwithalphacomponent:0.5f]; [Self addSubview:self.lineView]; Self.infolabel= [[UILabel alloc] Initwithframe:cgrectmake (Ten,0, -, -)]; Self.infoLabel.text=@"Demo"; [Self AddSubview:self.infoLabel];}- (void) buttonevent {if(Self.model.expend = =YES) {Self.model.expend=NO; [Self.tableview beginupdates]; [Self.tableview endupdates]; [Self normalstatewithanimated:yes]; } Else{self.model.expend=YES; [Self.tableview beginupdates]; [Self.tableview endupdates]; [Self expendstatewithanimated:yes]; }}- (void) LoadData: (ID) Data {Self.model=data; if(Self.model.expend = =YES) {Self.lineView.frame= CGRectMake (0,99.5f, the,0.5f); Self.infoLabel.frame= CGRectMake ( -,0, -, -); } Else{self.lineView.frame= CGRectMake (0,49.5, the,0.5f); Self.infoLabel.frame= CGRectMake (Ten,0, -, -); }}- (void) normalstatewithanimated: (BOOL) Animated {if(Animated = =YES) {[UIView animatewithduration:0.35fanimations:^{self.lineView.frame= CGRectMake (0,49.5, the,0.5f); Self.infoLabel.frame= CGRectMake (Ten,0, -, -); }]; } Else{self.lineView.frame= CGRectMake (0,49.5, the,0.5f); Self.infoLabel.frame= CGRectMake (Ten,0, -, -); }}- (void) expendstatewithanimated: (BOOL) Animated {if(Animated = =YES) {[UIView animatewithduration:0.35fanimations:^{self.lineView.frame= CGRectMake (0,99.5f, the,0.5f); Self.infoLabel.frame= CGRectMake ( -,0, -, -); }]; } Else{self.lineView.frame= CGRectMake (0,99.5f, the,0.5f); Self.infoLabel.frame= CGRectMake ( -,0, -, -); }}@end
////VIEWCONTROLLER.M//tableviewtapanimation////Created by youxianming on 15/9/18.//copyright©2015 year youxianming. All rights reserved.//#import "ViewController.h"#import "InfoCell.h"#import "Model.h"#defineInfo_cell @ "Info_cell"@interfaceViewcontroller () <uitableviewdelegate, uitableviewdatasource>@property (nonatomic, strong) Nsmutablearray*Datasarray, @property (nonatomic, strong) UITableView*TableView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.datasarray=[Nsmutablearray array]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:no]]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:yes]]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:yes]]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:yes]]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:yes]]; [Self.datasarray Addobject:[model modelwithnormalheight: -. F Expendheight: -. f Expend:yes]]; Self.tableview=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; Self.tableview.Delegate=Self ; Self.tableView.dataSource=Self ; Self.tableView.separatorStyle=Uitableviewcellseparatorstylenone; [Self.tableview Registerclass:[infocellclass] Forcellreuseidentifier:info_cell]; [Self.view AddSubview:self.tableView];}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return_datasarray.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Infocell*cell =[TableView Dequeuereusablecellwithidentifier:info_cell]; Cell.indexpath=Indexpath; Cell.tableview=TableView; [Cell Loaddata:_datasarray[indexpath.row]]; returncell;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {Model*model =_datasarray[indexpath.row]; if(model.expend) {returnModel.expendheight; } Else { returnModel.normalheight; }}@end
Details
Click an animated effect with a single cell height change