简体   繁体   English

将多个变换应用于UIView / CALayer

[英]Applying multiple transforms to a UIView / CALayer

Is there any problem applying multiple transforms to a UIView and it's corresponding CALayer? 将多个变换应用于UIView并且它是相应的CALayer是否有任何问题?

Specifically, can you "mix and match" CATransform3Ds with CGAffineTransforms without running into issues? 具体来说,您是否可以“混合和匹配”CATransform3D与CGAffineTransforms而不会遇到问题?

Also are there any problems with setting some transforms directly while animating another transform change simultaneously? 在动画另一个变换同时动画时,是否还有直接设置变换的问题?

Are there any "rules" for how this should be done, or any design patterns for this? 是否有任何“规则”应该如何做,或任何设计模式?

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. 我意识到这并不能完全回答(或接近),但如果你只使用CGAffineTransforms,你可以使用CGAffineTransformConcat()来组合多个变换。

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. 当你也进行层变换时,我不知道这是如何工作的。

pix0r is right but here is some more info on this. pix0r是对的,但这里有更多信息。 The official docs for CGAffineTransformConcat() . 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);

Syntax has changed slightly with Swift 3 & 4. Adaptation of @whitehawk's answer: 使用Swift 3和4语法略有改变。@ whitehawk的答案适应:

// 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)

I succeed to translate and rotate an imageView in the same time, this way: 我成功地在同一时间翻译和旋转imageView,这样:

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

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

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