2016-08-29 20 views
0

私はCAShapeLayerを作成し、ベジエキュービックパス(addCurveToPoint:controlPoint1:controlPoint2:機能付き)を追加し、そのカーブの開始点を少し追加しました。その後、私はCAKeyFrameAnimationを作成し、その小さなビューをカーブの開始点からアニメーションの終点まで移動させました。アニメーション中にそのビューの中心点を取得したい。何か案は?アニメーション中にビューの座標を取得する方法は?

CGPoint start, p, p1,p2; 
start = CGPointMake(0, 150); 
p = CGPointMake(300, 150); 
p1 = CGPointMake(50, 225); 
p2 = CGPointMake(250, 75); 
[self.bezierPath moveToPoint:start]; 
[self.bezierPath addCurveToPoint:p 
        controlPoint1:p1 
        controlPoint2:p2]; 

NSLog(@"self bezierpath = %@",self.bezierPath); 
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = self.bezierPath.CGPath; 
shapeLayer.fillColor = [UIColor clearColor].CGColor; 
shapeLayer.strokeColor = [UIColor redColor].CGColor; 
shapeLayer.lineWidth = 3.f; 
[self.yellowView.layer addSublayer:shapeLayer]; 

self.sliderThumb = [[UIView alloc]initWithFrame:(CGRect){0,0,20,20}]; 
self.sliderThumb.backgroundColor = [UIColor blackColor]; 
self.sliderThumb.center = CGPointMake(0, 150); 
[self.yellowView addSubview:self.sliderThumb]; 
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; 
animation.keyPath = @"position"; 
animation.duration = 4.f; 
animation.path = self.bezierPath.CGPath; 
[self.sliderThumb.layer addAnimation:animation forKey:nil]; 
CALayer *layer = self.sliderThumb.layer.presentationLayer; 
NSLog(@"layer coordinates %f", layer.frame.origin.x); 

`

答えて

0

ビューの層のpresentationLayerは層が半ばアニメーションです捉えるCALayerです。

例えば

あなたはフレームによって更新フレームを取得したい場合は、CADisplayLinkを設定し、表示リンクのハンドラでpresentationLayerを調べる:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
animation.duration = 4.0f; 
animation.path = self.bezierPath.CGPath; 
animation.delegate = self;         // so we know when animation finished 
[self startDisplayLink];         // start display link 
[self.sliderThumb.layer addAnimation:animation forKey:nil]; 

付:

@property (nonatomic, weak) CADisplayLink *displayLink; 

そして:

// Called when animation stops (assuming you set the `delegate` for the animation). 

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { 
    [self stopDisplayLink:self.displayLink]; 
} 

/// Start the display link. 

- (void)startDisplayLink { 
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; 
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
    self.displayLink = displayLink; 
} 

/// Stop the display link. 
/// 
/// @parameter displayLink The display link to be stopped. 

- (void)stopDisplayLink:(CADisplayLink *)displayLink { 
    [displayLink invalidate]; 
} 

/// The display link handler. 
/// 
/// Called once for each frame of the animation. 
/// 
/// @parameter displayLink The display link being handled. 

- (void)handleDisplayLink:(CADisplayLink *)displayLink { 
    CALayer *layer = self.sliderThumb.layer.presentationLayer; 
    NSLog(@"layer coordinates %f", layer.frame.origin.x); 
} 
+0

答えに感謝します。あなたは、その曲線の座標を得る他の方法はありますか?私はスライダのようなものを作成したいが、カーブしたトラックを作りたい。 – passingnil

+0

http://stackoverflow.com/a/13667092/1271826を参照してください。 – Rob

+0

素晴らしい!ありがとうございました。 @Rob – passingnil

関連する問題