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 Phone で私が何を意味するかがわかります。ユーチューブ動画

4

2 に答える 2

0

わかりました、このアイデアに興味があったので、いくつかのコードのプロトタイプを作成しました。私はあなたのニーズに合わせて特別に調整する必要がありますが、それは良い出発点になる可能性があります. まず、セル内の imageViewUICollectionViewCellに接続するために、サブクラス化する必要があります。IBOutlet次に、私のコード スニペットを参照して、作業を開始できます。

- (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]];
}

アップデート:

一度に 1 つのセルだけをランダムに反転させたい場合は、メソッド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 に答える