天天看点

iOS 核心动画-锚点

锚点是CALayer 的一个属性

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

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

 anchorPoint 在左上角的时候 (0,0)

 anchorPoint 在右上角的时候 (1,0)

 anchorPoint 在左下角的时候 (0,1)

 anchorPoint 在右下角的时候 (1,1)

有关锚点使用(例子):

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

}