为什么这个按钮没有响应?

时间:2011-08-19 09:21:27

标签: iphone objective-c

UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 300.0, 43.0, 43.0)];
    [imageView setBackgroundColor:[UIColor clearColor]];
    [imageView setImage:[UIImage imageNamed:@"test.png"]];
    [self.view addSubview:imageView];

[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration: 0.2f
                     delay: 0.0f
                   options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                animations: ^(void){[imageView setFrame:CGRectMake(0.0, 350.0, 43.0, 43.0)];}
                completion: nil];


UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 0, 100, 30)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[self.view addSubview:button];

4 个答案:

答案 0 :(得分:0)

在按钮定义中进行以下更改:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

你的方法应该如下:

-(IBAction)buttonHandle:(id)sender
{
}

我希望它会有所帮助。

答案 1 :(得分:0)

尝试修改animateWithDuration:见下文

[UIView animateWithDuration: 0.2f
                         delay: 0.0f
                       options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                    animations: ^(void){[imageView setFrame:CGRectMake(0.0, 350.0, 43.0, 43.0)];}
                    completion: nil];

在options属性中,我添加了值UIViewAnimationOptionAllowUserInteraction

答案 2 :(得分:0)

如果您无法触摸按钮(如果点击不起作用),那么

编写代码

[self.view bringSubviewToFront:button];

在方法结束时。

答案 3 :(得分:0)

我想你想对按钮执行操作,所以试试这段代码。

      UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button setFrame:CGRectMake(100, 0, 100, 30)];
      [button setTitle:@"Button" forState:UIControlStateNormal];
  [button addTarget:self            action:@selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];

      [self.view addSubview:button];

-(IBAction)buttonPressed:(id)sender
{
    //put code here you want to perform action on button
}