1
我已經使用此代碼沿着沿着兩個按鈕的固定自定義路徑路徑使用它們的x座標x &來設置動畫。 現在我想知道每條路線在旋轉或動畫時的圓的確切位置,我怎麼知道這一點?如何知道在自定義路徑上旋轉的圓的當前位置?
- (void) animateCicleAlongPath:(NSInteger)pt1:(NSInteger)pt2
{
UIButton *animatebtn1 = (UIButton *)[self.view viewWithTag:pt1];
UIButton *animatebtn2 = (UIButton *)[self.view viewWithTag:pt2];
NSLog(@"%@",animatebtn1);
NSLog(@"%@",animatebtn2);
NSLog(@"%@",NSStringFromCGRect(circleView.frame));
int a1;
int a2;
int b1;
int b2;
a1=animatebtn1.frame.origin.x;
a2=animatebtn1.frame.origin.y;
b1=animatebtn2.frame.origin.x;
b2=animatebtn2.frame.origin.y;
pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[UIView setAnimationDidStopSelector:@selector(animationDidStart:)];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = YES;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, b1, b2);
NSLog(@"%@",[finalpath description]);
for (int i=0; i<finalpath.count; i++) {
int j=[[finalpath objectAtIndex:i]intValue];
UIButton *animatebtn = (UIButton *)[self.view viewWithTag:j];
int p1=animatebtn.frame.origin.x;
int p2=animatebtn.frame.origin.y;
CGPathAddLineToPoint(curvedPath, NULL, p1,p2);
}
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
UIGraphicsBeginImageContext(CGSizeMake(20,20));
// CGContextRef ctx = UIGraphicsGetCurrentContext();
//Set context variables
CGContextSetLineWidth(context, 1.5);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
//Draw a circle - and paint it with a different outline (white) and fill color (green)
CGContextAddEllipseInRect(context, CGRectMake(1, 1, 18, 18));
CGContextDrawPath(context, kCGPathFillStroke);
UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
circleView = [[UIImageView alloc] initWithImage:circle];
[imageScrollView addSubview:circleView];
[imageScrollView bringSubviewToFront:circleView];
[circleView setHidden:NO];
circleView.alpha=1;
pathAnimation.speed=0.5;
pathAnimation.duration=10;
circleView.animationDuration=10;
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
[circleView release];
}
Thanx,但我仍然無法獲得此 –