我正在为我的应用程序定制一个UITableViewCell,一切都很好,因为我已经好几次了。
现在我想做一些动画和问题。
我会尽可能简短地解释一下:
我正在做一本字典应用程序。我有一个包含UITableView的视图和我的自定义单元格(NTNHistoryCell),此视图用于显示用户研究的历史记录。
#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
用户可以向左或向右滑动以删除条目。 在单元格上有两个重要的东西:作为UIImageView的背景用于在用户滑动时改变颜色,frontView作为UIView包含一些UILabel,frontView将在用户滑动时改变它的框架。如果用户使用翻译进行滑动,则该单元格将被删除(条目对应也将在数据库中删除)。
现在我想做动画让用户知道他们可以在每次加载tableview时刷一下单元格。我使用事件didEndDisplayingCell和下面的代码:
-(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];
}
}
所有代码都不顺利,你可以看到我的评论,我可以改变背景颜色,但是UILabel的文字和frontView的框架。
我打算向左移动然后向右移动前视图。
我哪里出错了?
以下是截图,以澄清我的文字(我没有足够的信誉发布图片):http://i.stack.imgur.com/dUddJ.png
编辑: 我解决了 请参阅@danypata回复中的评论。
答案 0 :(得分:1)
所以你需要的是检测表视图何时完成加载过程,然后检测第一个可见单元格的动画。所以这是我的方法:
-(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
}];
}
我不是百分之百确定这种方法会起作用,但你应该试一试。