gpt4 book ai didi

ios - 将 subview 滑出屏幕

转载 作者:行者123 更新时间:2023-11-29 02:51:36 24 4
gpt4 key购买 nike

我有一个 ImageView 作为 subview ,我正在尝试实现向上滑动手势以将其从屏幕上弹出。滑动有效,但没有动画效果。

这就是我添加 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];
}

这就是我想要做的:

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

您在动画结束前移除 View ...这就是它发生的原因。

关于ios - 将 subview 滑出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24430452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com