Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是CABASICanization';相对于初始位置的tos值?_Ios_Objective C_Core Animation_Cabasicanimation - Fatal编程技术网

Ios 是CABASICanization';相对于初始位置的tos值?

Ios 是CABASICanization';相对于初始位置的tos值?,ios,objective-c,core-animation,cabasicanimation,Ios,Objective C,Core Animation,Cabasicanimation,我有一个CALayer,我正在显式地将其设置为新位置的动画。我的toValue属性似乎仅与层的初始位置相关 CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"]; move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)]; move.toVa

我有一个
CALayer
,我正在显式地将其设置为新位置的动画。我的
toValue
属性似乎仅与层的初始位置相关

    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
    move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
    move.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.center.x, self.view.center.y)];
    move.duration = 1.0;
    [blueDot addAnimation:move forKey:@"myMoveAnimation"];
    blueDot.position = CGPointMake(self.view.center.x, self.view.center.y);
因此,此代码不会将图层重绘到视图的中心。似乎它真的在将层重新绘制为
CGPointMake(self.view.center.x+blueDot.position.x,self.view.center.y+blueDot.position.y)
,就像是将
self.view.center
的值添加到层的起始位置

toValue
属性的行为应该是这样的吗?将我的蓝点移动到视图中心最干净的方法是什么


编辑:

    -(void)viewDidLoad {
        [super viewDidLoad];

        blueDot = [CAShapeLayer layer];
        [blueDot setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.view.center.x - 60 - 5, 163.5, 10, 10)] CGPath]];
        [blueDot setStrokeColor: [UIColor blueColor]CGColor];
        [blueDot setFillColor:[UIColor blueColor]CGColor];
        [[self.view layer] addSublayer:blueDot];
    }

// 163.5 is a placeholder for another view's position value so it's more readable for you guys.

您可以在
视图
的superview的坐标系中将
设置为value
(因为使用了
中心
属性)。看起来这样应该行得通:

CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds))];
move.duration = 1.0;
[blueDot addAnimation:move forKey:@"myMoveAnimation"];
blueDot.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));

您可以在
视图
的superview的坐标系中将
设置为value
(因为使用了
中心
属性)。看起来这样应该行得通:

CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds))];
move.duration = 1.0;
[blueDot addAnimation:move forKey:@"myMoveAnimation"];
blueDot.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));

问题是您正在移动层的
,但是该
层的蓝点
路径
已在该层的
内被
self.view.center.x-60-5163.5
偏移,为该
CAShapeLayer
创建
路径时指定的坐标。因此,当您设置层的
位置
的动画时,蓝点仍将从新的
位置
偏移该量

修复方法是将
CAShapeLayer
路径定义在该层的左上角,例如

CAShapeLayer *blueDot = [CAShapeLayer layer];
blueDot.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(5, 5) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:true].CGPath;  // note that using arc often generates a better looking circle, probably not noticeable at this size, just probably good practice
blueDot.fillColor = [UIColor blueColor].CGColor;
现在,当您移动
位置时,蓝点的原点将移动到该位置


另外,我通常会创建一个单独的
UIView
,并将这个
CAShapeLayer
添加到该视图中。这样,您可以享受各种不同的功能:

  • 您可以欣赏
    UIView
    基于块的动画
  • 您可以使用
    center
    移动它,这样当您将其
    center
    移动到其superview的
    center
    时,它将真正居中(现在,如果您将图层的
    位置移动到
    self.view.center
    ,它实际上并不完全居中,因为它将是蓝点图层的左上角,位于中间)
  • 您可以使用UIKit Dynamics将其捕捉到位置或定义其他行为
  • 如果需要,可以使用自动布局约束指定放置
  • 您可以定义一个
    UIView
    子类,该子类是
    IBDesignable
    ,使用此点作为视图,以便您可以将其添加到故事板上并查看其外观;等等

问题是您正在移动层的
,但此
现金层
的蓝点
路径
已在该层的
内偏移了
self.view.center.x-60-5163.5
,即为该
现金层创建
路径
时指定的坐标ode>。因此,当您设置层的
位置
动画时,蓝点仍将从新的
位置
偏移该量

修复方法是将
CAShapeLayer
路径定义在该层的左上角,例如

CAShapeLayer *blueDot = [CAShapeLayer layer];
blueDot.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(5, 5) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:true].CGPath;  // note that using arc often generates a better looking circle, probably not noticeable at this size, just probably good practice
blueDot.fillColor = [UIColor blueColor].CGColor;
现在,当您移动
位置时,蓝点的原点将移动到该位置


另一方面,我通常会创建一个单独的
UIView
并将此
CAShapeLayer
添加到该视图中。这样,您可以享受各种不同的功能:

  • 您可以欣赏
    UIView
    基于块的动画
  • 您可以使用
    center
    移动它,这样当您将其
    center
    移动到其superview的
    center
    时,它将真正居中(现在,如果您将图层的
    位置移动到
    self.view.center
    ,它实际上并不完全居中,因为它将是蓝点图层的左上角,位于中间)
  • 您可以使用UIKit Dynamics将其捕捉到位置或定义其他行为
  • 如果需要,可以使用自动布局约束指定放置
  • 您可以定义一个
    UIView
    子类,该子类是
    IBDesignable
    ,使用此点作为视图,以便您可以将其添加到故事板上并查看其外观;等等

此外,如果您只想更改位置,似乎可以使用隐式动画来完成所有操作。@dperk层隐式动画不是这样做的–这是用于
UIView
动画。您想在
CATransaction
块内执行隐式动画。即
[CATransaction begin];[CATTransaction设置动画持续时间:1.0];[CATTransaction提交];
@originaluser2,对于支持隐式动画的层属性,您根本不需要CATTransaction开始/结束。您只需更改属性,并使用默认设置对更改进行动画设置。只有在需要更改默认动画设置(缓和、持续时间等)时,才需要CATTransaction调用隐式动画仅适用于层属性,而不适用于视图属性。这是关键。@Duncan确定您不需要
CATTransaction
如果您对默认设置满意,我只是将OP的显式动画转换为隐式动画:)此外,如果您只想更改位置,似乎可以使用隐式动画来完成所有操作。@dperk层隐式动画不是这样做的–这是用于
ui查看
动画。您想在
CATransaction
块内进行隐式动画。即
[CATransaction begin];[CATTransaction设置动画持续时间:1.0];[CATTransaction提交];
@originalu