简体   繁体   English

在屏幕上滑动子视图

[英]Swipe Subview Off Screen

I have an image view as a subview and I'm trying to implement a swipe up gesture to fling it off screen. 我有一个图像视图作为子视图,我正在尝试实现向上滑动手势以将其从屏幕上弹出。 The swipe works but it doesn't animate. 滑动有效,但没有动画。

This is how I'm adding the subview: 这就是我添加子视图的方式:

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

And this is what I'm trying to do: 这就是我想要做的:

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

You were removing view before the animation ends... that's why its' happening. 您是在动画结束之前删除视图的。这就是它发生的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM