我正在尝试创建一个UITableViewController,其中tableview上的加载动画应该使项目从侧面滑入。细胞的高度高于默认细胞,大约5-7倍。我想在android上看到像Google+加载动画这样的动画。 (卡动画)。
我想象的是继承TableView类,重写一个加载方法,当它们被引入显示器时显示新项目,然后使用弹簧动画同时旋转单元格,同时逐步更改x和y值
我在this answer中尝试了这段代码,但并没有让它发挥作用。对该答案的详细说明和解释也将非常有帮助。代码:
- (void)animate
{
[[self.tableView visibleCells] enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL *stop) {
[cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
[UIView animateWithDuration:1 animations:^{
[cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
}];
}];
}
任何帮助将不胜感激! 埃里克
答案 0 :(得分:3)
以下是您所描述的(我相信)的完整功能示例。我打开了一个空的单一视图项目,删除了storyboard中的视图控件,并简单地创建了一个UITableViewController并将其类设置为我在下面创建的UITableViewController子类的类。
头文件中没有添加任何内容,因此我将其排除在外
#import "weeeTables.h"
@interface weeeTables ()
@property (nonatomic, strong) NSMutableSet *shownIndexes;
@property (nonatomic, assign) CATransform3D initialTransform;
@end
shownIndexes将包含已经显示的视图的索引,因此我们不会重复向上滚动动画。 initialTransform属性是单元格初始状态的转换(在屏幕显示之前你想要它的位置)
@implementation weeeTables
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat rotationAngleDegrees = -30;
CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI/180);
CGPoint offsetPositioning = CGPointMake(-20, -20.0);
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, rotationAngleRadians, -50.0, 0.0, 1.0);
transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50.0);
_initialTransform = transform;
_shownIndexes = [NSMutableSet set];
}
在ViewDidLoad中,我们设置了初始变换的值,我稍微使用了值,并鼓励你做同样的事情来获得你想要的效果,但是细胞从左下角开始生成动画目前。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 385;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weeCell" forIndexPath:indexPath];
return cell;
}
上面的块中的所有内容对于UITableViewController子类来说都是非常典型的,并且与添加动画没有特别相关性,我只是设置一些静态数字来添加大量单元格来滚动,这里唯一的唯一值是单元格标识符,你可以看到有一个非常仔细考虑过的名字。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![self.shownIndexes containsObject:indexPath]) {
[self.shownIndexes addObject:indexPath];
UIView *weeeeCell = [cell contentView];
weeeeCell.layer.transform = self.initialTransform;
weeeeCell.layer.opacity = 0.8;
[UIView animateWithDuration:.65 delay:0.0 usingSpringWithDamping:.85 initialSpringVelocity:.8 options:0 animations:^{
weeeeCell.layer.transform = CATransform3DIdentity;
weeeeCell.layer.opacity = 1;
} completion:^(BOOL finished) {}];
}
}
@end
这是真正把它放在一起的人,首先我们检查我们要显示的单元格是否已经在shownIndexes列表中,如果不是我们添加它,那么创建一个局部变量for这个当前的单元格内容视图。
然后将该contentView上的变换设置为我们的初始变换(我们在ViewDidLoad中设置)。这会将单元格移动到我们的起始位置,然后才能让用户看到,然后我们将该变换设置为身份变换的动画。我也有不透明度,只是为了它。
希望它有所帮助!
答案 1 :(得分:1)
我会尝试使用tableView:willDisplayCell:forRowAtIndexPath:
您可以将UITableViewCell设置为初始屏幕外帧,然后异步初始化该单元格的动画。
我已对此进行了测试,并使用Xcode主/明细模板
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let frame = CGRectMake(-cell.frame.size.width, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)
cell.frame = frame
UIView.animateWithDuration(1, delay: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
let endFrame = CGRectMake(100, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)
cell.frame = endFrame
}) { (foo:Bool) -> Void in
//
}
}
您只需为不同的演示文稿案例添加特殊的大小写。当您有时再次返回视图时,或者在单元格从视图中滚动并再次返回之后,将调用此方法。