2010-12-09 48 views
2

我想获取视图的位置,并且它正在进行动画并从一个位置移动到另一个位置 我尝试使用presentationLayer获取动作位置信息 并试图在UIView移动时打印出位置。为什么视图的presentationLayer对我无法正常工作?

这里是正在打印:

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;  

}

这是它的动画: -(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秒内以如此低的速度移动。

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

回答

0

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

您可以使用一个计时器,但它会启动一个方法,然后调用另一个方法来执行诊断。后者将使用performSelectorOnMainThreadmain线程上执行。

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?

相关问题