如何在触摸时旋转UIImageView

时间:2014-05-06 12:12:44

标签: ios uiimageview

我正在尝试将图像全屏显示,如果图像是横向图像,则随着图像的增长将其旋转。

使其成为全屏的代码工作正常,但是如果我添加旋转结果是相当不可预测的并且图像旋转,则移动到左侧但不会全屏显示。

使用的代码是:

        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            //save previous frame
            previousFrame = imgView.frame;

            [imgView setFrame:[[UIScreen mainScreen] bounds]];
            imgView.backgroundColor = [UIColor blackColor];
            [imgView.layer setBorderWidth: 0.0];
            if(imgView.image.size.width > imgView.image.size.height)
                imgView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
            }
            completion:^(BOOL finished){
               isFullScreen = YES;
            }
        ];

不确定我在这里缺少什么,但假设可能与旋转和图像视图边界有关?

2 个答案:

答案 0 :(得分:0)

你能试试这段代码吗?

[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            //save previous frame
            previousFrame = imgView.frame;

            [imgView setFrame:[[UIScreen mainScreen] bounds]];
            imgView.backgroundColor = [UIColor blackColor];
            [imgView.layer setBorderWidth: 0.0];
            }
            completion:^(BOOL finished){
               if(imgView.image.size.width > imgView.image.size.height)
                imgView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
               isFullScreen = YES;
            }
        ];

答案 1 :(得分:0)

尝试使用CABasicAnimation,结合以下示例:

尺寸示例:

-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
    CGRect oldBounds = layer.bounds;
    CGRect newBounds = oldBounds;
    newBounds.size = size;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation.fromValue = [NSValue valueWithCGRect:oldBounds];
    animation.toValue = [NSValue valueWithCGRect:newBounds];
    animation.duration = animationspeed;
    progressLayer.anchorPoint = CGPointMake(0, progressLayer.anchorPoint.y);
    layer.bounds = newBounds;

    [layer addAnimation:animation forKey:@"bounds"];
}

轮换示例:

CABasicAnimation *animation = [CABasicAnimation    animationWithKeyPath:@"transform.rotation.z"];
   animation.duration = 5;
  animation.additive = YES;
  animation.removedOnCompletion = NO;
  animation.fillMode = kCAFillModeForwards;
  animation.fromValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(0)];
  animation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(90)];
 [self.imgView.layer addAnimation:animation forKey:@"90rotation"];

将它们与动画组合并,例如:

// Animation group
CAAnimationGroup* group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:colorAnim, widthAnim, nil];
group.duration = 5.0;

[myLayer addAnimation:group forKey:@"BorderChanges"];