I am trying to get an image to go full screen, and if the image is landscape then rotate it as it grows.

The code to make it fullscreen works ok, but if I add in the rotation the results are rather unpredictable and the image rotates, moves to the left hand side but does not grow fullscreen.

Code used is:

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

Not sure what I am missing here, but assuming maybe something to do with rotation and the image view bounds?

有帮助吗?

解决方案

Try to use CABasicAnimation instead, combine these examples:

Size example:

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

Rotation example:

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"];

Combine them with an animationgroup, example:

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

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

其他提示

Can you try this code

[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;
            }
        ];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top