滑动子视图关闭屏幕

时间:2014-06-26 12:23:08

标签: ios objective-c animation

我有一个图像视图作为子视图,我正在尝试实现一个向上滑动手势将其从屏幕上移开。滑动有效,但没有动画。

这就是我添加子视图的方式:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    InstagramMedia *media = mediaArray[indexPath.row];

    for (int i = 0; i < mediaArray.count; i++)
    {
        InstagramMedia *instagramMedia = [mediaArray objectAtIndex:i];
        [imageArray addObject:instagramMedia.standardResolutionImageURL];
    }

    largeImageArray = imageArray;
    imageIndex = indexPath.row;

    UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
    CGRect cellRect = attributes.frame;

    blurredView = [[UIToolbar alloc]initWithFrame:self.view.bounds];
    [blurredView setBarStyle:UIBarStyleBlackOpaque];
    [self.view addSubview:blurredView];

    imageView = [[UIImageView alloc]initWithFrame:cellRect];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.userInteractionEnabled = YES;
    [imageView setImageWithURL:media.standardResolutionImageURL placeholderImage:[UIImage imageNamed:@"placeholder"]];
    [self.view addSubview:imageView];
    CGRect finalFrame = CGRectMake(0, 50, 320, 301);
    [UIView animateWithDuration:0.5 animations:^{
        imageView.frame = finalFrame;
    }];

    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)];
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeRecognizer];

    UISwipeGestureRecognizer *swipeRightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)];
    swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRightRecognizer];

    swipeUpRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
    swipeUpRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUpRecognizer];
}

这就是我要做的事情:

- (IBAction)swipeUp:(id)sender
{
    CGRect finalFrame = CGRectMake(0, -100, 320, 301);
    [UIView animateWithDuration:0.5 animations:^{
        imageView.frame = finalFrame;
        [imageView removeFromSuperview];
        [blurredView removeFromSuperview];
        [self.view removeGestureRecognizer:swipeUpRecognizer];
    }];
}

1 个答案:

答案 0 :(得分:1)

    - (IBAction)swipeUp:(id)sender
    {
        CGRect finalFrame = CGRectMake(0, -100, 320, 301);
        [UIView animateWithDuration:0.5 animations:^{
            imageView.frame = finalFrame;

        } completion:^(BOOL finished) {
 [imageView removeFromSuperview];
            [blurredView removeFromSuperview];
            [self.view removeGestureRecognizer:swipeUpRecognizer];
}];
    }

你在动画结束之前删除了视图......这就是它发生的原因。