views:

54

answers:

2

Hi guys! That's my very first post on SOF. I'm a new programmer at objective-c. Heres the "problem" i'm dealing with

I created 2 UIbutton : one to pull an UIImageView from the top of the screen and the other to push it back. I have the code for the action** but i dont know how to relate it to an if-statement if(button1 pressed) then pull view else(button2 pressed) then push view back.

-(void)viewDidLoad{
{super viewDidLoad]

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button1.frame = CGRectMake(200, 0, 90, 30);
[button1 addTarget:self action:@selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button2.frame = CGRectMake(400, 0, 90, 30);
[button2 addTarget:self action:@selector(buttonPressed2) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:button2];
}


**

-(void)buttonPressed1 {

 double speed = 1 / round(random() % 100) + 1.0;

 UIImageView *banetteView2 = [[UIImageView alloc] initWithImage:banetteImage];
 banetteView2.frame = CGRectMake(100, -740, 568, 790);
 banetteView2.opaque = NO;
 [self.view addSubview:banetteView2];

 [UIView beginAnimations:nil context:banetteView2];
 [UIView setAnimationDuration: 2*speed ];

 //banetteView2.frame = CGRectMake(100, -1, 568, 790);

 UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage];
 banetteView.frame = CGRectMake(100, -740, 568, 790);
 banetteView.opaque = NO;
 banetteView.hidden = YES;
 [self.view addSubview:banetteView];


 [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
 [UIView setAnimationDelegate:self];
 [UIView commitAnimations];


}

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

 UIImageView *banetteView2 = context;
 [banetteView2 release];


 double speed = 1 / round(random() % 100) + 1.0;
 banetteView2.frame = CGRectMake(100, 0, 568, 790);
 banetteView2.opaque = NO;
 [self.view addSubview:banetteView2];

 [UIView beginAnimations:nil context:banetteView2];
 [UIView setAnimationDuration: 2*speed ];

 //banetteView2.frame = CGRectMake(100, -740, 568, 790);

}
+2  A: 

I'm not sure if this works for UIButtons, but for regular Cocoa buttons you would add a param for sender like this:

-(void)buttonPressed1:(id)sender
{
    // And now you can check which button is the sender
    if(sender==button1)
        // Do stuff....
}

And then ofcourse you would need to add a colon on the selector when you set the button action: @selector(buttonPressed1:)

Splashdust
This works for UIButton as well.
Eiko
I think this should work, but the fact is that my button1 is 'undeclared'. I've tried to recreate it in buttonPressed1 fonction but didn't work either.
Gaspard
That is because it is confined to the scope of the method it is declared in. In order to access it from several methods you need to declare it class wide. You can do this either by declaring it in your class's interface, in the .h file, or if it doesn't need to be visible in the interface, you can declare it in your class outside of a method, between @implementation and the first method for instance.
Splashdust
I found by adapting this codehttp://mobile.tutsplus.com/tutorials/iphone/iphone-sdk-learning-about-touch-events-basic-game-animation/
Gaspard