我移动一个带有动画的按钮,如果我在 UIVIewController 中使用它,一切都可以正常工作,如果我尝试用这个动画子类化 UIView 我看不到它,按钮出现在终点但我看不到它移动。错误在哪里?
这是我的代码:
- (void) moveButton:(UIButton *)button fromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint {
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
NSMutableArray *timings = [NSMutableArray array];
NSMutableArray *keytimes = [NSMutableArray array];
// Start
[timings addObject:[self getTiming:kCAMediaTimingFunctionEaseIn]];
[keytimes addObject:[NSNumber numberWithFloat:0.0]];
// Bounce
[timings addObject:[self getTiming:kCAMediaTimingFunctionEaseIn]];
[keytimes addObject:[NSNumber numberWithFloat:1.0]];
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.duration = 2;
animation.path = thePath;
animation.timingFunctions = timings;
animation.keyTimes = keytimes;
[[button layer] addAnimation:animation forKey:nil];
[[button layer] setPosition:CGPointMake(endPoint.x,endPoint.y)];
}