繁体   English   中英

如何在触摸时旋转UIImageView

[英]How do I rotate UIImageView on touch

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

使它全屏显示的代码可以正常工作,但是如果我添加旋转效果,结果将非常不可预测,并且图像旋转,移到左侧,但不会全屏显示。

使用的代码是:

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

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

你可以试试这个代码

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

尝试改用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"];

将它们与animationgroup组合,例如:

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

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

暂无
暂无

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

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