以锚点为基础进行旋转

define PI 3.14159265

-(void)rotation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue   = [NSNumber numberWithFloat:-PI/4];
    animation.duration  = 1.0f;
    [view.layer addAnimation:animation forKey:nil];
}


这段代码可以让这个view逆时针旋转45度 但是以为这个view的中心点为中心旋转的,如果我想以这个view的左下角的点为中心旋转,应该是添加什么样的代码呢?


1 个回答

设置一下Anchor Points即可。

下图中,上半部分的anchorPoint为(0.5, 0.5),即anchorPoint为layer的中心。下半部分的anchorPoint为(0.0, 0.0),即anchorPoint为layer的左上角。同理我们可知,anchorPoint如果为(1.0, 1.0)的化就代表右下角。(注意iOS与OS X坐标系的不同)

下面这张图更清晰的解释了Anchor Points对layer的transformations变化的影响:

对于iOS,可以总结anchorPoint值对应layer的位置如下图:


对于你的问题,只需要设置view.layer的anchorPoint为(0.0, 1.0)即可以左下角为中心旋转  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值