Ios6 点击时为UICollectionViewCell设置动画
当用户点击单元格时,我想在Ios6 点击时为UICollectionViewCell设置动画,ios6,core-animation,uicollectionview,uicollectionviewcell,Ios6,Core Animation,Uicollectionview,Uicollectionviewcell,当用户点击单元格时,我想在UICollectionViewCell上启动一些动画。我的想法是在didSelectItemAtIndexPath中选择相应的单元格并触发动画。但是,这不起作用: -(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath { //为用户点击的单元格设置动画 ProductCollectionViewCell*单元格=[
UICollectionViewCell
上启动一些动画。我的想法是在didSelectItemAtIndexPath
中选择相应的单元格并触发动画。但是,这不起作用:
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
//为用户点击的单元格设置动画
ProductCollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“ProductReuseID”forIndexPath:indexPath];
[UIView animateWithDuration:5.0
延迟:0
选项:(UIViewAnimationOptionAllowUserInteraction)
动画:^{
NSLog(@“动画开始”);
[cell.layer setBackgroundColor:[UIColor color withred:180.0/255.0 green:238.0/255.0 blue:180.0/255.0 alpha:1.0].CGColor];
}
完成:^(布尔完成){
NSLog(@“动画结束”);
[cell.layer setBackgroundColor:[UIColor whiteColor].CGColor];
}
];
}
实际上,动画同时开始和结束(尽管animateWithDuration
设置为5)。下一次尝试是跳过动画并简单地设置不同的边框样式:
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
//为用户点击的单元格设置动画
ProductCollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“ProductReuseID”forIndexPath:indexPath];
[细胞层宽度:5.0f];
}
但是,这不会改变任何事情(可能是因为我必须手动重新绘制单元格?)
当用户点击UICollectionViewCell时,您知道如何设置其动画吗?
亲切问候,,
Christian看来您获取的单元格是错误的。发送
dequeueReusableCellWithReuseIdentifier:forIndexPath:
消息不会在第二个参数的indexPath处获取视图中正在使用的单元格,但会将以前使用但可重用的单元格出列;如果没有可用的可重用单元,将创建一个新单元。见下文参考文献1
替换:
ProductCollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“ProductReuseID”forIndexPath:indexPath];
与:
ProductCollectionViewCell*cell=[collectionView cellForItemAtIndexPath:indexPath];
在上面的代码中,应该为您提供适当的单元格
这是你的第一个例子,重写了
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath(NSIndexPath*)indexPath
{
//为用户点击的单元格设置动画
UICollectionViewCell*cell=[collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:5.0
延迟:0
选项:(UIViewAnimationOptionAllowUserInteraction)
动画:^{
NSLog(@“动画开始”);
[单元格背景颜色:[UIColor COLOR WITHRED:180.0/255.0绿色:238.0/255.0蓝色:180.0/255.0 alpha:1.0];
}
完成:^(布尔完成){
NSLog(@“动画结束”);
[cell setBackgroundColor:[UIColor whiteColor]];
}
];
}
参考资料:
- UIViewAnimationOptionCurveEaseIn
- UIViewAnimationOptionCurveEaseOut
- UIViewAnimationOptionAllowUserInteraction UICollectionViewDelegate-didSelectItemAtIndexPath方法
UICollectionViewCell *uviCollectionCell = [collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:0.4 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
NSLog(@"animation start");
CALayer *layer = uviCollectionCell.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 15.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
NSLog(@"animation end");
CALayer *layer = uviCollectionCell.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m24 = 0;
rotationAndPerspectiveTransform =CATransform3DRotate(rotationAndPerspectiveTransform, 0.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;
} completion:nil];
}
];