iOS 核心动画-锚点

本文深入探讨了iOS中Core Animation的锚点(anchorPoint)属性,解释了如何通过改变锚点实现动画效果。内容包括锚点的默认值、不同位置的锚点坐标以及如何通过用户的触摸事件动态调整锚点,从而实现对象的旋转动画。示例代码展示了如何根据用户点击屏幕的位置改变对象的锚点,并进行3D旋转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

锚点是CALayer 的一个属性


anchorPoint  锚点  以锚点为中心  执行动画(可以理解为    渔夫  固定船的点)

anchorPoint  默认的是(0.5,0.5) 在中间  锚点是一个比例

 anchorPoint 在左上角的时候 00

 

 anchorPoint 在右上角的时候 10

 

 anchorPoint 在左下角的时候 01

 

 anchorPoint 在右下角的时候 11


有关锚点使用(例子):

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

    CGPoint touchPoint = [touch locationInView:self.view];

    

    

//    通过 点击屏幕的x/屏幕的宽度    得到点击的点与屏幕的一个比例

    CGFloat x = (touchPoint.x)/CGRectGetWidth([UIScreen mainScreen].bounds);

//    通过 点击屏幕的y/屏幕的高度    得到点击的点与屏幕的一个比例

    CGFloat y = (touchPoint.y)/CGRectGetHeight([UIScreen mainScreen].bounds);

//    改变 ship 锚点

    ship.anchorPoint =CGPointMake(x, y);

    

     NSLog(@"锚点X:%f 锚点Y:%f",ship.anchorPoint.x,ship.anchorPoint.y);  //打印锚点的位置

    

    

    CGFloat cx = CGRectGetWidth(ship.bounds)*ship.anchorPoint.x;

    CGFloat cy = CGRectGetWidth(ship.bounds)*ship.anchorPoint.y;

    APLayer.position =CGPointMake(cx, cy);

    

    

//   角度值经计算转化为弧度值。要把角度值转化为弧度值,可以使用一个简单的公式Mπ/180

//    <#CGFloat tx#>, <#CGFloat ty#>, <#CGFloat tz#>  3个轴  0  1  把谁设置为1 就围绕它旋转

    ship.transform = CATransform3DMakeRotation(45*M_PI/18, 0, 0, 1);

    

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    ship.transform = CATransform3DIdentity;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值