代码之家  ›  专栏  ›  技术社区  ›  Smikey

是否可以成功设置移动按钮的动画?

  •  2
  • Smikey  · 技术社区  · 14 年前

    [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:5.0];
        CGAffineTransform newTransform = CGAffineTransformMakeScale(3, 3);
        bubble.transform = CGAffineTransformTranslate(newTransform, 0, -460);
        [UIView commitAnimations];
    

    因此,我在一些示例代码的帮助下尝试了核心动画(路径并不重要,只是一个示例):

    CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
        CGPathAddCurveToPoint(thePath,NULL,
                              15.f,250.0f,
                              295.0f,250.0f,
                              295.0f,15.0f);
        CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
        theAnimation.path=thePath;
        CAAnimationGroup *theGroup = [CAAnimationGroup animation];
        theGroup.animations=[NSArray arrayWithObject:theAnimation];
        theGroup.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        theGroup.duration=15.0;
        CFRelease(thePath);
        [bubble.layer addAnimation:theGroup forKey:@"animatePosition"];
    

    有人能建议另一种方法吗?我是否应该为UIImageViews设置动画,并使其重新显示以进行触摸?还是我会有同样的问题?这让我困惑了好几天,所以非常感谢您的帮助。

    谢谢:)

    迈克尔

    4 回复  |  直到 14 年前
        1
  •  1
  •   Engin Kurutepe mick80234    14 年前

    对于UIKit和CoreAnimation来说,这听起来有点太复杂了。你好像在开发一个游戏。为什么不使用一个全局动画定时器,比如说~60 fps,然后用Quartz2D绘制呢?然后,对于每次触摸,您可以快速检查石英刷新之间的任何点击。

        2
  •  3
  •   Tim Rupe    14 年前

    当设置GUI元素(如UIButton)的动画时,实际位置仅在动画完成时更改。这意味着动画期间的任何触摸事件将仅在按钮的起始点起作用。您可以通过访问按钮的presentationLayer来获取按钮的当前显示位置。您可能需要实现自己的事件处理,查看用户触摸屏幕时的位置,并将其与从presentationLayer获得的边界进行比较。

        3
  •  3
  •   Smikey    14 年前

    - (void)startMinigame {
        makeBubbles = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(generateRandomBubble) userInfo:nil repeats:YES];
        floatBubbles = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(floatBubbles) userInfo:nil repeats:YES];
    }
    
    - (void)generateRandomBubble {
        //Get a random image for the bubble
        int randomIndex = arc4random()%kNumberBubbles;
        UIImage *bubbleImage = [bubbleImages objectAtIndex:randomIndex];
    
        //Create a new bubble object and retrieve the UIButton
        Bubble *bubble = [[Bubble alloc]initWithImage:bubbleImage andIndex:randomIndex];
        UIButton *bubbleButton = bubble.bubbleButton;
    
        //Configure the button
        [bubbleButton addTarget:self action:@selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];
    
        //Add the bubble to the scene
        [self.view insertSubview:bubbleButton belowSubview:bathTub];
        //Add the bubble to an array of currently active bubbles:
        [self.bubbles addObject:bubble];
        [bubble release];
    }
    
    - (void)floatBubbles {
        //Move every active bubble's frame according to its speed. 
        for (int i = 0; i < [bubbles count]; ++i) {
            Bubble *bubble = [bubbles objectAtIndex:i];
            int bubbleSpeed = bubble.speed;
            CGRect oldFrame = bubble.bubbleButton.frame;
            CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y - bubbleSpeed, oldFrame.size.width+0.1, oldFrame.size.height+0.1);
            bubble.bubbleButton.frame = newFrame;
            if (bubble.bubbleButton.frame.origin.y < -100.0) {
                [bubbles removeObject:bubble];
                [bubble.bubbleButton removeFromSuperview];
            }
        }
    }
    
        4
  •  1
  •   tadejsv    14 年前

    尝试设置uiview的frame属性的动画。