检测按下哪个按钮? (然后行动)

时间:2010-10-14 14:54:31

标签: objective-c ipad if-statement uibutton programmatically-created

这是我关于SOF的第一篇文章。 我是objective-c的新程序员。 这是我正在处理的“问题”

我创建了2个UI按钮:一个用于从屏幕顶部拉出UIImageView,另一个用于将其推回。 我有动作的代码**但是我不知道如何将它与if语句联系起来if(按下按钮1)然后拉动其他视图(按下按钮2)然后再推回视图。

-(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 个答案:

答案 0 :(得分:3)

我不确定这是否适用于UIButtons,但对于常规Cocoa按钮,您可以像这样为发件人添加一个参数:

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

然后当你设置按钮动作时,你需要在选择器上添加一个冒号:@selector(buttonPressed1 :)

答案 1 :(得分:0)