这是我在SOF上的第一篇文章。 我是Objective-C的新程序员。 继承人我正在处理的“问题”检测哪个按钮被按下? (然后行动)
我创建了2个UIbutton:一个从屏幕顶部拉UIImageView,另一个将其推回。 我有代码的行动**,但我不知道如何将它与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);
}
这也适用于UIButton。 – Eiko 2010-10-14 15:04:07
我认为这应该可行,但事实是我的button1是'未声明'。我试图在buttonPressed1功能中重新创建它,但也没有工作。 – Gaspard 2010-10-15 09:16:18
这是因为它被限制在它声明的方法范围内。为了从几个方法访问它,你需要声明它在类中的宽度。你可以通过在你的类的接口,在.h文件中声明它,或者如果它不需要在接口中可见,你可以在你的类中声明@implementation和第一种方法。 – Splashdust 2010-10-15 13:00:23