ios中发生UISwipeGestureRecognizer时的UIImageView动画

Posted

技术标签:

【中文标题】ios中发生UISwipeGestureRecognizer时的UIImageView动画【英文标题】:UIImageView animation when UISwipeGestureRecogniser is occured in ios 【发布时间】:2014-05-29 09:13:18 【问题描述】:

我的场景是,我有一个数组中的图像集合。在 UIScrollView 中动态创建 UIImageView。代码如下所示。

- (void)createImage

    UIImageView *imgFashion;

    CGFloat newHeight = 0.0;
    int xPos = 10;
    int yPosL = 10;
    int yPosR = 10;

    btnScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 83, 320, 485)];

    for (int i=0; i<[buttonImageArr count];i++)
    

        UIImage *img = [buttonImageArr objectAtIndex:i];
        originalImageWidth = img.size.width;
        originalImageHeight = img.size.height;

        newHeight = originalImageHeight * (150/originalImageWidth);
        if (xPos == 10)
        

            imgFashion=[[UIImageView alloc]initWithFrame:CGRectMake(xPos, yPosL, 150, newHeight)];
            xPos = 160;
            yPosL = yPosL + newHeight;

        
        else
        
            imgFashion=[[UIImageView alloc]initWithFrame:CGRectMake(xPos, yPosR, 150, newHeight)];
            imgFashion.tag=i;
            xPos = 10;
            yPosR +=newHeight;
        
        imgFashion.tag=i;
        imgFashion.image = [buttonImageArr objectAtIndex:i];
        imgFashion.userInteractionEnabled = YES;
        [btnScroller addSubview:imgFashion];
        imgFashion.layer.borderWidth = 3.0;
        imgFashion.layer.borderColor = [UIColor whiteColor].CGColor;
        UISwipeGestureRecognizer *profile_SwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes_profile:)];

        profile_SwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        profile_SwipeGestureRecognizer.numberOfTouchesRequired = 1;
        profile_SwipeGestureRecognizer.enabled = YES;
        [imgFashion addGestureRecognizer:profile_SwipeGestureRecognizer];
        btnScroller.scrollEnabled=YES;
    

    btnScroller.contentSize=CGSizeMake(xPos, yPosR+newHeight);
    [self.view addSubview:btnScroller];


现在我将滑动任何图像的顶部,我想从视图中删除该图像。我为此功能编写代码。

- (void)handleSwipes_profile:(UISwipeGestureRecognizer *)paramSender

    if (paramSender.direction == UISwipeGestureRecognizerDirectionLeft)
    
        [paramSender.view removeFromSuperview];
        selectedBtn = paramSender.view.tag;
        [buttonImageArr removeObjectAtIndex:selectedBtn];
        [btnScroller removeFromSuperview];
        btnScroller = nil;
        [self createImage];
        [self.view setNeedsDisplay];
    

我的问题是,我想在从视图中删除之前旋转图像。

【问题讨论】:

【参考方案1】:

您可以使用 UIView 的动画方法和 UIViewAnimationOptionCurveLinear 等选项来实现这一点,重复次数如下所示

- (void)handleSwipes_profile:(UISwipeGestureRecognizer *)paramSender

    if (paramSender.direction == UISwipeGestureRecognizerDirectionLeft)
    
        [UIView animateWithDuration: 0.2f
                              delay: 0.0f
                            options: UIViewAnimationOptionCurveLinear
                         animations: ^
                             [UIView setAnimationRepeatCount:3];
                             paramSender.view.transform = CGAffineTransformRotate(UserNameField.transform, M_PI );
                         
                         completion: ^(BOOL finished) 
                             NSLog(@"finished");
                             if (finished) 
                                 [paramSender.view removeFromSuperview];
                                 selectedBtn = paramSender.view.tag;
                                 [buttonImageArr removeObjectAtIndex:selectedBtn];
                                 [btnScroller removeFromSuperview];
                                 btnScroller = nil;
                                 [self createImage];
                                 [self.view setNeedsDisplay];

                             
                         ];
    



如果您需要更多选项,请查看此问题UIView Infinite 360 degree rotation animation?

【讨论】:

您好,Vignesh,感谢您的回答。它的工作非常好。我的期望。【参考方案2】:

你可以像这样使用'-animationWithDuration'方法

[UIView animateWithDuration:1.0 animations:^

    // perform your rotation here
    [_yourImageView setTransform:CGAffineTransformMakeRotation(180)];

 completion:^(BOOL finished) 

    [paramSender.view removeFromSuperview];
    selectedBtn = paramSender.view.tag;
    [buttonImageArr removeObjectAtIndex:selectedBtn];
    [btnScroller removeFromSuperview];
    btnScroller = nil;
    [self createImage];
    [self.view setNeedsDisplay];
];

【讨论】:

以上是关于ios中发生UISwipeGestureRecognizer时的UIImageView动画的主要内容,如果未能解决你的问题,请参考以下文章

视图控制器的 NSMutableArray

如何在视图透明的同时接收滑动手势?

iOS8 扩展 - 在 iOS 5/6/7 上会发生啥?

iOS 旋转动画在 CAAnimationGroup 中发生不顺畅

在 iOS 中删除内存映射文件 - 幕后发生了啥?

ios中发生UISwipeGestureRecognizer时的UIImageView动画