Button Doesnt work when Animated

Discussion in 'iOS Programming' started by mandude, Jan 25, 2010.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    please help someone. i have a simple UIButton that was doing its job fine until i animated it to go from left to right on the screen. when i try to press the button while it's moving nothing happens. : [



    h. file

    Code:
    
    @interface.... {
    UIButton *button;
    }
    @property (nonatomic, retain) IBOutlet UIButton *button;
    -(IBAction)action;
    @end
    
    m. file

    Code:
    // the button is placed in interface builder at an x coordinate of  -50.
    
    -(void)viewDidLoad {
    [UIView beginAnimations:@"ButtonMove" context:nil];
    	[UIView setAnimationDuration:6.0];
    	[UIView setAnimationRepeatCount:10];
    	CGPoint center = Button.center;
    	if (center.x < 10) {
    		center.x += 540.0f;
    	}
    	stickButton.center = center;
    	[UIView commitAnimations];
    
    
     
  2. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #2
    I've never tried to click a moving button. That might be undefined behavior.

    Is there a reason it has to be a button (as opposed to a view?)
     
  3. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #3
    well actually i want the "button" to be a stick figure walking along the screen left to right. and when the user taps the stick figure (button) a label appears. so #1 how in the world can i make it look like a stick figure is walking and able to make the stick figure able to be tapped like a button and have an IBAction connected to it. please don't be too harsh i'm no expert (as you can see)
     
  4. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    Ah, I see. I would tend to do this by creating a custom view (ManView) and implement a custom drawRect function to draw the man. You can also implement touch event handlers that call back to a controller to display a message (the event handler could check if the touch coordinates match the current position of the man or not).
     
  5. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #5
    ahh i sort of understand you're logic... make a new view and have that keep drawing up different rects of him moving... and everytime i touch i can check to see if the coordinates of my touch match the coordinates of the man... then BAM yet i dont really know how to get that idea into code... you've given much help and i dont want to ask u how to do this but im not sure how the code would work for this : /
     
  6. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #6
    Well, I'd start by, in xcode, creating a new file, derived from UIView. If you are drawing the man by swapping bitmaps, have your new class have an array of CGImageRefs (you can get them from UIImages by calling the .CGImage function). Maybe have an integer called currentFrame or something, and a function like -(void)incrementFrame {currentFrame++;if (currentFrame>maxFrame) currentFrame=0;}

    Then, in your drawRect, use the coregraphics functions to draw the cgimage corresponding to currentFrame on your cgcontext.

    In your main controller class you can have an nstimer that calls incrementFrame at regular intervals. You can either make your view small and use the nstimer to move it, or make the view big and use the nstimer to set the x,y of the cgimage.
     
  7. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
  8. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #8
    No problem. Feel free to PM me if you get stuck.
     
  9. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #9
    I'll be sure to do so. Thanks a bunch this forum could use more nice dudeS like u. I'll PM u if... When something goes wrong lol
     

Share This Page