使用CAKeyframeAnimation和CGMutablePathRef移动自定义UIView

时间:2014-11-05 13:58:11

标签: ios objective-c uiview uibutton cakeyframeanimation

我有一个uiview对象,它有自己的类和.xib文件(= GroupOnScreen)。

UIView拥有两个UIImageViews和一个UIButton。

在我的ViewController中,在屏幕上创建和绘制了几个这样的UIView:

- (void)viewDidLoad
{
    ...
    GroupOnScreen *group = [[GroupOnScreen alloc] initWithFrame:CGRectMake(462, 126, 100, 100)];
    ...
    group.delegate = self;
    [self.view insertSubview:group belowSubview:_BlackView];
    [groupDictionary setObject:group forKey:someKey];
    ...
}

之后,它们被放置在另一个带有动画的UIImageView周围的圆圈中:

- (void)moveGroupsAroundTheImage
{
     int numberOfGroups = (int)[[groupDictionary allKeys] count];

     int centerX = self.view.bounds.size.width/2;
     int centerY = self.view.bounds.size.height/2;

     float startAngle = degreesToRadians(startAngleDegree);
     float stopAngle = 0;

     for (id key in groupDictionary) {

         GroupOnScreen *currentGroup = [groupDictionary objectForKey:key];

         int keyValue = [key intValue];

         stopAngle = -90 + (30 * keyValue + (-15 * numberOfGroups + 15));
         stopAngle = degreesToRadians(stopAngle);

         CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

         pathAnimation.calculationMode = kCAAnimationPaced;
         pathAnimation.fillMode = kCAFillModeForwards;
         pathAnimation.removedOnCompletion = NO;
         pathAnimation.duration = 0.4;
         pathAnimation.repeatCount = 0;

         CGMutablePathRef path = CGPathCreateMutable();

         if (keyValue + 1 > (numberOfGroups / 2)) {
             CGPathAddArc(path, NULL, centerX, centerY, 205, startAngle, stopAngle, NO);
         } else {
             CGPathAddArc(path, NULL, centerX, centerY, 205, startAngle, stopAngle, YES);
         }

         pathAnimation.path = path;
         CGPathRelease(path);

         [currentGroup.layer addAnimation:pathAnimation forKey:@"moveTheObject"];
      }
 }

这完美无缺。所有具有按钮和图像的组都以圆圈移动,但我无法单击任何这些按钮。按钮的可点击部分以某种方式停留在实例化的位置。这意味着:我可以看到按钮随着动画的移动而移动,但我只能点击它们被创建的那个......这是屏幕上某处的空白点。

为什么单击按钮的#34;空间"不动了?

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我通过添加:

来实现
- (void)moveGroupsAroundTheImage
{
    ...
    pathAnimation.delegate = self;
    ...
}

和此:

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
    for (id key in groupDictionary) {
        GroupOnScreen *currentGroup = [groupDictionary objectForKey:key];
        currentGroup.frame = [[currentGroup.layer presentationLayer] frame];
    }
}