我一直在这里寻找一个没有成功的答案,所以现在我试一试自己。
我试图用不同的单元格制作一个包含图像的UicollectionView +一个带有单元格的动画。
我希望细胞对我的图像进行“随机翻转”(5种不同的图像)。
UIViewAnimationOptionTransitionFlipFromLeft
在一个5-6秒的循环中。
我现在的动画是这样的:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell.superview bringSubviewToFront:collectionView];
[UIView transitionWithView:cell
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[UICollectionViewCell commitAnimations];
cell.transform = CGAffineTransformMakeRotation(0.0);
}
completion:^(BOOL finished) {}];
}我知道我不应该使用didSelectItemAtIndexPath,但我使用它只是为了看看动画是否正确。
如果你查看这段视频,你可以看到我的意思,在windows 8手机上。Youtube视频
发布于 2013-08-28 19:30:53
好吧,我对这个想法很感兴趣,所以我给一些代码做了原型。我需要专门为您的需要量身定做,但这可能是一个良好的开端。首先,您需要对UICollectionViewCell进行子类,以便将IBOutlet连接到单元格内的imageView。然后,您可以引用我的代码片段让您开始。
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageList = @[@"img1.png", @"img2.png", @"img3.png", @"img4.png"];
}
// This assumes your images are inside your Bundle.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:@selector(updateCells:)
userInfo:nil
repeats:YES];
}
- (void)updateCells:(NSTimer *)timer
{
NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
for (NSIndexPath *indexPath in visibleIndexPaths) {
SubclassCollectionViewCell *cell = (SubclassCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
[UIView transitionWithView:cell
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
cell.imageView.image = [self randomImage];
} completion:nil];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
return cell;
}
- (UIImage *)randomImage
{
NSInteger randomNumber = arc4random() % [self.imageList count];
return [UIImage imageNamed:[self.imageList objectAtIndex:randomNumber]];
}更新:
如果一次只希望一个单元格随机翻转,则需要在updateCells方法中取出updateCells循环。相反,尝试如下:
- (void)updateCells:(NSTimer *)timer
{
NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
NSInteger randomIndex = arc4random() % [visibleIndexPaths count];
NSindexPath *randomIndexPath = [NSIndexPath indexPathForItem:randomIndex inSection:0];
SubclassCollectionViewCell *cell = (SubclassCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
[UIView transitionWithView:cell
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
cell.imageView.image = [self randomImage];
}
completion:nil
];
}发布于 2013-09-02 10:21:11
编辑后的帖子:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
int interval = 5+(rand() % 6);
int section = 0;
int row = (arc4random() % self.cellList.count);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CollectionCell *cell = (CollectionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(updateCells:)
userInfo:cell
repeats:NO];
NSLog(@"seconds: %d", interval);
NSLog(@"row: %d", row);
}
- (void)updateCells:(NSTimer *)timer{
CollectionCell* cell = [timer userInfo];
[UIView transitionWithView:cell
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
cell.collectionImageView.image = [self randomImage];
} completion:nil];
int interval = 5+(rand() % 5);
NSLog(@"speed: %d", interval);
int section = 0;
int row = (arc4random() % self.cellList.count);
NSLog(@"row: %d", row);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CollectionCell *newCell = (CollectionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(updateCells:)
userInfo:newCell
repeats:NO];}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.cellList.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect frame = self.collectionView.frame;
[collectionView setFrame:frame];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseID" forIndexPath:indexPath];
[collectionView setNeedsDisplay];
[self cellTitleAndBackground:cell indexPath:indexPath];
return cell;
}
- (void)cellTitleAndBackground:(CollectionCell *)cell indexPath:(NSIndexPath *)indexPath {
// Get title
NSString *name = [[NSString alloc] initWithFormat:@"%@", self.cellList[indexPath.row]];
// Create title background
UILabel *titleBackground = [[UILabel alloc] initWithFrame:CGRectMake(5, 70, 70, 30)];
titleBackground.backgroundColor = [UIColor blackColor];
titleBackground.alpha = 0.6f;
titleBackground.tag = 70;
[self removeReusedLabel:cell tag:70];
[cell addSubview:titleBackground];
// Create titleLabel
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 70, 70, 30)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.text = name;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.tag = 72;
[self removeReusedLabel:cell tag:72];
[cell addSubview:titleLabel];
}
-(void)removeReusedLabel:(CollectionCell *)cell tag:(int)tag {
UILabel *foundLabelBackground = (UILabel *)[cell viewWithTag:tag];
if (foundLabelBackground) [foundLabelBackground removeFromSuperview];
}
- (UIImage *)randomImage
{
// Random image for cells
NSInteger randomNumber = arc4random() % [self.imageList count];
return [UIImage imageNamed:[self.imageList objectAtIndex:randomNumber]];
}https://stackoverflow.com/questions/18488805
复制相似问题