Is it possible to successful animate a moving UIButton?

前端 未结 4 1994
挽巷
挽巷 2020-12-18 13:11

I\'m trying to animate a button which moves around the screen. At any point, the user can push the button. But the button doesn\'t respond to touches. I\'ve tried an animati

相关标签:
4条回答
  • 2020-12-18 13:39

    Well, for anyone interested, here's my solution which works perfectly well:

    - (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];
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 13:41

    When animating a GUI element like the UIButton, the actual position only changes when the animation is done. This means any touch events during the animation will only work at the starting point of the button. You can get the current displayed position of the button by accessing it's presentationLayer. You'll probably need to implement your own event handling, look at the position when the user touches the screen, and compare that to the bounds you get from the presentationLayer.

    0 讨论(0)
  • 2020-12-18 13:54

    This sounds a bit too complicated for UIKit and CoreAnimation. It seems like you're developing something like a game. Why not use a global animation timer for let's say ~60 fps and do your drawing in Quartz2D? Then for each touch you could quickly check any hits between Quartz refreshes.

    0 讨论(0)
  • 2020-12-18 14:00

    Try animating the uiview's frame property.

    0 讨论(0)
提交回复
热议问题