Reputation: 3428
I'm customizing an UITableViewCell for my app, everything goes fine as I've done it several times.
Now I want to do some animation and problems come.
I'll explain it as short as possible:
I'm doing an dictionary app. I have a view which contain a UITableView with my custom cell (NTNHistoryCell), this view is for displaying history of user's researches.
#import <UIKit/UIKit.h>
#import "HistoryItem.h"
@protocol NTNHistoryCellDelegate;
@interface NTNHistoryCell : UITableViewCell<UIGestureRecognizerDelegate>
@property (strong, nonatomic) HistoryItem *historyItem;
@property (strong, nonatomic) IBOutlet UILabel *lblWord;
@property (strong, nonatomic) IBOutlet UILabel *lblDictName;
@property (strong, nonatomic) IBOutlet UILabel *lblTimeStamp;
//variables for editing
@property (strong, nonatomic) IBOutlet UIImageView *imgDeleteBackground;
@property (strong, nonatomic) IBOutlet UIView *frontView;
@property (strong, nonatomic) UIImageView *imgIconDelete;
@property (strong, nonatomic) id<NTNHistoryCellDelegate> delegate;
-(void)initLabels;
@end
@protocol NTNHistoryCellDelegate <NSObject>
@optional
-(void)historyCellIsDeleted:(NTNHistoryCell*)historyCell;
@end
User can swipe left or right to delete the entry. There a two important things on the cell: a background as an UIImageView for changing color while user swipe, a frontView as UIView which contain some UILabels, frontView will change it's frame while user swipe. If the user do a swipe with a translation, the cell will be deleted (the entry correspond is deleted in the database too).
Now I want to do animation to let user know that they can swipe the cell by everytime the tableview is loaded. I use the event didEndDisplayingCell with the code below:
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//do cell animation to inform user that the cell can be swiped to delete
//only for the 1st row
if(indexPath.row == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
{
NTNHistoryCell *cell = (NTNHistoryCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
CGRect originalFrame = cell.frontView.frame;
[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:1.0];
//move left
//not working!
cell.frontView.frame = CGRectMake(-originalFrame.size.width, originalFrame.origin.y, originalFrame.size.width, originalFrame.size.height);
cell.lblWord.text = @"text change test";//not working!
cell.lblDictName.text = @"text change test";//not working!
cell.frontView.backgroundColor = [UIColor greenColor];//work fine!
[UIView commitAnimations];
}
}
It's not going well with all the code, you can see my comment, I can change the background color but the text of UILabel and the frame of frontView.
I intend to move left then move right the frontView.
Where did I go wrong?
Here is the screenshot in order to clarify my words (I haven't got enough reputation to post image): https://i.sstatic.net/dUddJ.png
EDIT: I solved it. See my comment in @danypata reply.
Upvotes: 2
Views: 164
Reputation: 10175
So what you need is to detect when the table view has complete the loading process and after that do an animation for the first visible cell. So here is my approach:
-(void)viewDidLoad {
[super viewDidLoad];
shouldAnimate = YES;
}
//delegate method
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{ if(shouldAnimate) {
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//now the table view completes the loading process
[self perforAnimation];
}
}
-(void)performAnimation {
shouldAnimate = NO;
NSIndexPath *firstCelPath = [[yourTableView indexPathsForVisibleCells] objectAtIndex:0]
YourCustomCell *cell = [yourTableView cellForRowAtIndexPath:firstCellPath];
[UIView animateWithDuration:0.3 animations:^{
//do your changes
}];
}
I'm not 100% sure that this approach will work but you should give it a try.
Upvotes: 1