CABasicAnimation - transform scale keep in center

前端 未结 4 1287
南笙
南笙 2021-02-07 02:27

Trying to animatie an ellipse masked on a UIView to be scale transformed remaining in the center position.

I have found CALayer - CABasicAnimation not scaling around cen

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 02:56

    You can create a scale around the center of the object instead of (0, 0) like this:

    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D tr = CATransform3DIdentity;
    tr = CATransform3DTranslate(tr, self.bounds.size.width/2, self.bounds.size.height/2, 0);
    tr = CATransform3DScale(tr, 3, 3, 1);
    tr = CATransform3DTranslate(tr, -self.bounds.size.width/2, -self.bounds.size.height/2, 0);
    scale.toValue = [NSValue valueWithCATransform3D:tr];
    

    So we start out with the "identity" transform (which means 'no transform'). Then we translate (move) to the center of the object. Then we scale. Then we move back to origo so we're back where we started (we only wanted to affect the scale operation).

提交回复
热议问题