2

我一直在这里寻找答案,但没有成功,所以现在我自己试试。

我试图用包含图像的不同单元格 + 带有单元格的动画制作一个 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 视频

4

2 回答 2

0

好的,我对这个想法很感兴趣,所以我制作了一些代码的原型。我需要专门针对您的需求量身定制,但这可能是一个好的开始。首先,您需要对您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]];
}

更新:

如果您一次只希望一个单元格随机翻转,则需要在方法中取出for循环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-08-28T19:30:53.773 回答
0

编辑帖子:

- (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]];
}
于 2013-09-02T10:21:11.223 回答