Applying multiple transforms to a UIView / CALayer

前端 未结 4 510
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 17:29

Is there any problem applying multiple transforms to a UIView and it\'s corresponding CALayer?

Specifically, can you \"mix and match\" CATransform3Ds with CGAffineTr

相关标签:
4条回答
  • 2020-12-05 17:54

    pix0r is right but here is some more info on this. The official docs for CGAffineTransformConcat().

    Also, here is a quick example:

    // Rotate 45 degrees
    CGAffineTransform rotate = CGAffineTransformMakeRotation(45*(M_PI/180));
    // Move to the left
    CGAffineTransform translate = CGAffineTransformMakeTranslation(-50,0);
    // Apply them to a view
    self.view.transform = CGAffineTransformConcat(translate, rotate);
    
    0 讨论(0)
  • 2020-12-05 18:12

    Syntax has changed slightly with Swift 3 & 4. Adaptation of @whitehawk's answer:

    // Rotate 45 degrees
    var rotate = CGAffineTransform(rotationAngle: 45 * (.pi / 180))
    // Move to the left
    var translate = CGAffineTransform(translationX: -50, y: 0)
    // Apply them to a view
    self.view.transform = translate.concatenating(rotate)
    
    0 讨论(0)
  • 2020-12-05 18:14

    I realize this doesn't answer the question entirely (or come close), but if you're only working with CGAffineTransforms you can use CGAffineTransformConcat() to combine multiple transforms.

    This will work just fine when some transforms are animated and others are not, as long as you concat the transformations properly. I don't know how this works when you're also doing layer transforms.

    0 讨论(0)
  • 2020-12-05 18:18

    I succeed to translate and rotate an imageView in the same time, this way:

    float scaleFactor_x   = 2.8;
    float scaleFactor_y   = 2.45;
    imgBigBallBasic.frame = CGRectMake(112, 20, 100, 100);
    CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(scaleFactor_x, scaleFactor_y);
    CGAffineTransform translateTrans  = CGAffineTransformMakeTranslation(0, 55);
    imgBigBallBasic.contentMode = UIViewContentModeScaleAspectFit;
    imgBigBallBasic.transform = CGAffineTransformConcat(translateTrans, scaleTrans);
    imgBigBallBasic.frame = CGRectMake(112, 20, 100, 100);
    imgBigBallBasic.center = [[imgBigBallBasic window] center];
    [UIView commitAnimations];
    
    0 讨论(0)
提交回复
热议问题