0

我有一个要求,我必须在网格视图中将图像视图从当前位置动画化到全屏。我将图像制作成网格视图。

- (void)viewDidLoad
{
    [super viewDidLoad];

    imagesArray = [[NSMutableArray alloc] init];        
    for (int i=1; i<11; i++) {

        [imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png", i]]];
        NSLog(@"imagesArray:%@", imagesArray);               
    }

    float horizontal = 20.0;
    float vertical = 20.0;

    for(int i=0; i<[imagesArray count]; i++)
    {
        if((i%3) == 0 && i!=0)
        {
            horizontal = 20.0;
            vertical = vertical + 220.00 + 20.0;
        }

        imagesView = [[UIImageView alloc] initWithFrame:CGRectMake(horizontal, vertical, 310.0, 220.00)];
        imagesView.userInteractionEnabled = YES;
        [imagesView setImage:[imagesArray objectAtIndex:i]];
        imagesView.tag = i;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;

        [imagesView addGestureRecognizer:tap];

        [self.view addSubview:imagesView];
        horizontal = horizontal + 310.0 + 20.0;
      }

}

在这里,我创建了带有图像的网格视图,就像我有 10 个图像一样。我制作了 3x3 网格视图。

接下来我向它添加了点击手势,我通过使用标签调用来获取图像视图位置

-(void)handleTap:(UITapGestureRecognizer *)recognizer
{
        UIView *piece = recognizer.view;

        [UIView animateWithDuration:1.0 animations:^{
            piece.frame = CGRectMake(piece.frame.origin.x, piece.frame.origin.y, 1000, 700);

        } completion:^(BOOL finished) {

    }];

    } 

你能告诉我如何通过点击它使图像视图从当前位置动画到全屏。

4

2 回答 2

1

由于您正在向self.view视图控制器添加视图,因此您可以假设它self.view占据了设备的整个屏幕。因此,您可以对其边界进行动画处理:

[UIView animateWithDuration:1.0 animations:^{
    piece.frame = self.view.bounds;
} completion:nil];

请注意,如果piece必须有一个跨越整个屏幕的父视图,这样您的内部视图就不会在动画期间被剪裁。

于 2013-06-27T06:00:47.243 回答
0

此代码将帮助您添加动画效果,例如缩放您期望的图像视图。动画结束后,您需要自定义显示全屏图像视图。

-(void)addZoomAnimationToZoomViewController:(UIImageView *)zoomView
{
    CABasicAnimation *zoomAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [zoomAnimation setDuration:0.8];
    [zoomAnimation setRepeatCount:1];
    [zoomAnimation setFromValue: [NSNumber numberWithFloat:0.1]];
    [zoomAnimation setToValue: [NSNumber numberWithFloat:5.0] ];
    [zoomAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [[zoomView layer] addAnimation:zoomAnimation forKey:@"zoom"];
}
于 2013-06-27T06:12:26.403 回答