如何在不跳过的情况下在特定锚点旋转图层? [英] How to rotate an layer at a specific anchor point without a skip?

查看:82
本文介绍了如何在不跳过的情况下在特定锚点旋转图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转一个图像在左上角而不是在中心的图层。根据文档,我将 anchorPoint 属性设置为[0,1]。在我的示例中,该视图旋转了50°,但是在开始制作动画之前,该视图跳到了屏幕上的另一个点。

I want to rotate a layer with an image at the top left corner, and not the center. According to the docs I set the anchorPoint property to [0, 1]. The view rotates in my example by 50°, but before it starts to animate, the view jumps to another point at the screen.

self.shakeTag.layer.anchorPoint = CGPointMake(0.0f, 1.0f);
[UIView beginAnimations:@"rotate" context:nil];
[self.shakeTag.layer setTransform:
  CATransform3DRotate(CATransform3DIdentity,
  radians(50.0), 0.0f, 0.0f, 1.0f)];
[UIView commitAnimations];

radians()的定义如下:

static inline double radians (double degrees) {return degrees * M_PI/180;}

当我使用大小为4倍且透明像素很多的图像时,可以在默认锚点[0.5 ,0.5],但我不想浪费可见像素的空间。有什么想法可以防止图层在旋转之前发生跳动?

When I use an image that is 4 times the size and has a lot of transparent pixels, I can rotate it at the default anchor point [0.5, 0.5], but I don’t want to waste the space for invisible pixels. Any ideas how I can prevent the layer from jumping before the rotation takes place?

推荐答案

更改锚点会影响您的位置视图。如果您更改了锚点并且想要将视图保持在当前位置,则需要更改视图的位置。使用类似这样的内容(从此处获取:层位置在(核心)动画)来设置锚点并补偿位置变化:

Changing the anchor point affects the positioning of your view. You'll need to change the view's position if you change the anchor point and if you want to keep your view where it currently is. Use something like this (taken from here: Layer Position jumps at start of (Core) Animation) to set your anchor point and compensate position changes:

-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

    CGPoint position = view.layer.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

也请参见此处以获取更多详细信息:更改CALayer的anchorPoint会移动视图

Also see here for more details: Changing my CALayer's anchorPoint moves the view

这篇关于如何在不跳过的情况下在特定锚点旋转图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆