单击/不单击时淡入淡出按钮

时间:2014-07-28 01:45:53

标签: ios uibutton nstimer

我试图让一个按钮移动到我视图中的随机位置。但是,点按下时只会随机移动。如果没有按下,我也希望它淡入不同的位置。我使用CGRect因为我需要点保持在特定视图的范围内。

-(IBAction)randomRed:(id)sender
{
    [self redDot];

    CGRect senderFrame = [sender frame];
    CGRect superBounds = [[sender superview ] bounds];
    senderFrame.origin.x = (superBounds.size.width - senderFrame.size.width) * drand48();
    senderFrame.origin.y = (superBounds.size.height - senderFrame.size.height) * drand48();
    [sender setFrame:senderFrame];

    counter = counter - 5;
    scoreLabel.text = [NSString stringWithFormat:@"Points %i", counter];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Tapadot Red Dot Buzzer Short" ofType:@"mp3"];
    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    sound.numberOfLoops = 1;
    [sound play];

    [self subtractOne];
    [self performSelector:@selector(showRedDot) withObject:nil afterDelay:1.0];
}

-(void)startRedDot
{
    if (counter ==0)
        redDot = [NSTimer scheduledTimerWithTimeInterval:4.0
                                                  target:self
                                                selector:@selector(showRedDot)
                                                userInfo:nil
                                                 repeats:YES];
}

-(void)showRedDot
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [redButton setAlpha:1];
    [UIView commitAnimations];

    [self performSelector:@selector(redDot) withObject:nil afterDelay:1.0];
}

-(void)redDot
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [redButton setAlpha:0];
    [UIView commitAnimations];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUpGame];
    [self sizing];
}

1 个答案:

答案 0 :(得分:0)

目前,只有在按下按钮时才会调用随机定位代码。

要使其移动独立于按钮按下,我们应该将它放在自己的方法中,该方法可以在设定的持续时间后调用自己。

首先,我们需要获取您要移动的按钮实例的引用,以便我们可以对其进行操作。

然后让我们将移动代码分成自己的方法。

- (void) moveButtonRandomly {
    CGSize limits;
    CGPoint newPosition;

    // Get limits
    limits.width = self.view.frame.size.width - button.frame.size.width;
    limits.height = self.view.frame.size.height - button.frame.size.height;

    // Calculate new Position
    newPosition = (CGPoint){ limits.width * drand48(), limits.height * drand48() };

    // Set new frame
    button.frame = (CGRect){ newPosition, button.frame.size };
}

将startRedDot更改为此,将构造并启动计时器。

- (void) startRedDot {
    redDotTimer = [NSTimer scheduledTimerWithTimeInterval:4.0
                                              target:self
                                            selector:@selector(moveButtonRandomly)
                                            userInfo:nil
                                             repeats:YES];
    [redDotTimer fire];
}

添加动画非常简单,只需创建一个调用两个动画的新方法;一个淡出按钮并完成移动按钮的按钮,然后触发下一个动画以淡化它。

- (void) moveButtonWithAnimation {
    CGFloat fadeDurration;
    if ( !button )
        return; // only invoke the button if it exists
    fadeDurration = 0.4;

    //Fade Out
    [UIView animateWithDuration: fadeDurration animations:^{
        button.alpha = 0.0f;

    } completion:^(BOOL finished) {
        // Move the button while it is not visible
        [self moveButtonRandomly];

        // Fade in
        [UIView animateWithDuration: fadeDurration animations:^{
            button.alpha = 1.0f;
        }];
    }];
}

最后将计时器编辑器编辑为动画方法。

- (void) startRedDot {
    redDotTimer = [NSTimer scheduledTimerWithTimeInterval:4.0
                                              target:self
                                            selector:@selector(moveButtonWithAnimation)
                                            userInfo:nil
                                             repeats:YES];
    [redDotTimer fire];
}