【问题标题】:UIImageView re-size and rotate UIImageView at same time?UIImageView 重新调整大小并同时旋转 UIImageView?
【发布时间】:2015-02-03 11:51:28
【问题描述】:

我的 UIViewController 中有一个 UIImageView。我在上面设置了一个图像。我做了如下轮换。

- (void) runSpinAnimationOnView:(UIImageView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

我称之为

[self runSpinAnimationOnView:_imageView duration:1.0 rotations:360 repeat:1.0];

现在我还想调整相同图像视图的大小,但各方面的比例相同,我该怎么做。

简单地说,我想旋转 UIImageView 并同时调整它的大小。

谢谢

【问题讨论】:

    标签: ios objective-c uiimageview autoresize autorotate


    【解决方案1】:

    您可以向图层添加多个动画。只需添加@"transform.scale" 动画即可

    - (void) runScaleAnimationOnView:(UIImageView*)view duration:(CGFloat)duration scale:(CGFloat)scale repeat:(float)repeat
    {
        CABasicAnimation* scaleAnimation;
        scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.toValue = [NSNumber numberWithFloat: scale];
        scaleAnimation.duration = duration;
        scaleAnimation.repeatCount = repeat;
    
        [view.layer addAnimation:rotationAnimation forKey:@"scaleAnimation"];
    }
    
    [self runSpinAnimationOnView:_imageView duration:1.0 rotations:360 repeat:1.0];
    [self runScaleAnimationOnView:_imageView duration:1.0 scale:0.5 repeat:1.0];
    

    请注意,您可能还需要调整@"position"

    【讨论】:

      【解决方案2】:

      CGAffineTransformConcat 将达到您的目的,它将两个变换合二为一并制作为单个动画。试试这个。

      [UIView animateWithDuration: 3.0f
                            delay: 0
                          options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                       animations:^{
      
                           UIView *yourViewToBeAnimated = YourViewToBeSetHere;
      
                           CGAffineTransform animationToRotate = CGAffineTransformMakeRotation(45);
      
                           CGAffineTransform animationToScale = CGAffineTransformMakeScale(2, 2);//Double the size
      
      
                           yourViewToBeAnimated.transform = CGAffineTransformConcat(animationToScale, animationToRotate);
      
      
                       }
                       completion:^(BOOL finished) {
      
      
      
      
                       }
       ];
      

      【讨论】:

        【解决方案3】:

        只要这样,就可以按比例制作CGRectMake,随心所欲。它只是显示它将如何调整大小的方式。

        - (void)rotateSpinningView
            {
        
                _imageView.frame = CGRectMake(_imageView.frame.origin.x+5,
                                              _imageView.frame.origin.y+5,
                                              _imageView.frame.size.width-10,
                                              _imageView.frame.size.height-10);
        
                _imageView.contentMode = UIViewContentModeScaleAspectFit;
                _imageView.clipsToBounds = YES;
        
        
                [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                    [_imageView setTransform:CGAffineTransformRotate(_imageView.transform, M_PI_2)];
                } completion:^(BOOL finished) {
                    if (finished && !CGAffineTransformEqualToTransform(_imageView.transform, CGAffineTransformIdentity)) {
                        [self rotateSpinningView];
                    }
                }];
            }
        
            - (IBAction)changeView:(UIButton *)sender {
        
                [self rotateSpinningView];
        
            }
        

        【讨论】:

        • 它适用于 M_PI,M_PI_2 ,不适用于 10,20,45 之类的东西
        猜你喜欢
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 2017-12-18
        • 1970-01-01
        相关资源
        最近更新 更多