为什么视图的presentationLayer对我不起作用?

时间:2010-12-09 06:21:47

标签: iphone core-animation quartz-2d

我希望在视图被动画化并从一个位置移动到另一个位置时获取视图的位置 我尝试使用presentationLayer来获取动作位置信息, 并试图在UIView移动时打印出位置。

以下是打印出来的内容:

2010-12-08 16:56:50.496 Exp[31219:207] point: NSPoint: {45, 45}

2010-12-08 16:56:50.696 Exp[31219:207] point: NSPoint: {45, 45}

2010-12-08 16:56:50.896 Exp[31219:207] point: NSPoint: {45, 45}

2010-12-08 16:56:51.096 Exp[31219:207] point: NSPoint: {45, 45}

2010-12-08 16:56:51.296 Exp[31219:207] point: NSPoint: {45, 45}

2010-12-08 16:56:51.496 Exp[31219:207] point: NSPoint: {245, 245} <<----------------------------------------

2010-12-08 16:56:51.696 Exp[31219:207] point: NSPoint: {245, 245}

2010-12-08 16:56:51.896 Exp[31219:207] point: NSPoint: {245, 245}

2010-12-08 16:56:52.096 Exp[31219:207] point: NSPoint: {245, 245}

注意箭头,直接打印出端点而不显示路径上的点。有人可以帮忙吗?

下面是我的代码:

以下是打印方式:

-(void)print

{ NSLog([NSMutableString stringWithFormat:@"point: %@",[NSValue valueWithCGPoint:((CALayer *)[bomb.layer presentationLayer]).position]]);
}

我用计时器重复打印动画视图“炸弹”

-(id)initWithCoder:(NSCoder *)aDecoder

{ if (self=[super initWithCoder:aDecoder]) {

      bomb = [[BombView alloc]init];
      bomb.frame = CGRectMake(30, 30, 30, 30);
      bomb.backgroundColor = [UIColor yellowColor];

      [self addSubview:bomb];

      timer = [NSTimer timerWithTimeInterval:PRINTINTERVAL // defined to be 0.2

                                         target: self

                                           selector: @selector(print)

                                           userInfo: nil

                                            repeats: YES];               

      [[NSRunLoop currentRunLoop] addTimer:timer forMode:      NSDefaultRunLoopMode];

 }

 return self;     

bomb = [[BombView alloc]init]; bomb.frame = CGRectMake(30, 30, 30, 30); bomb.backgroundColor = [UIColor yellowColor]; [self addSubview:bomb]; timer = [NSTimer timerWithTimeInterval:PRINTINTERVAL // defined to be 0.2 target: self selector: @selector(print) userInfo: nil repeats: YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode]; } return self;

以下是它的动画效果: }

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

{

 CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];

 animation.duration = 10.0;

 animation.delegate = bomb;

 [bomb.layer addAnimation:animation forKey:@"animatePosition"];

 bomb.center = CGPointMake(bomb.center.x+200, bomb.center.y+200);

当我点击时,“炸弹”将移动,打印出来的信息应该逐渐改变。因为它在10秒内以如此低的速度移动。

移动正常,但打印不正确。

1 个答案:

答案 0 :(得分:0)

可能是因为您使用NSTimer来打印诊断程序。这将执行但可能不会在main线程上执行,从而给您不正确的结果。

您可以使用计时器,而是让它触发一个方法,该方法又调用另一个方法来进行诊断。后者将使用mainperformSelectorOnMainThread线程上执行。

timer = [NSTimer timerWithTimeInterval:PRINTINTERVAL // defined to be 0.2

                                         target: self

                                           selector: @selector(timedPrint)

                                           userInfo: nil

                                            repeats: YES]; 

然后:

- (void)timedPrint
{
[self performSelectorOnMainThread:@selector(print)];
}

或者,使用currentRunLoop使用mainRunLoop

如果这不能解析查询,那么可能是因为Core Animation架构中有三个不同的层;模型,演示文稿和渲染。因此,渲染层可能已更改 - 如您所见 - 但presentationLayer尚未更新。

关于这个问题,有很多相关的问题。例如:

Does the -presentationLayer tell me the in-flight values of an view while it is beeing animated?

Why does one have to use CALayer's presentationLayer for hit-testing?