0

我想在视图出现时将两个按钮添加到出现在父视图上的子视图中。按钮已正确初始化并出现,但按下时它们没有反应。有人知道为什么吗?这是我的代码:

-(void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor blackColor];
    //Animation View
    imageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 499, 320, 0)];
    imageView.image = [UIImage imageNamed:@"trans.png"];
    [imageView setClipsToBounds:YES];
    [imageView setContentMode:UIViewContentModeBottom];
    [self.view addSubview:imageView];


    [UIView animateWithDuration:1.0f
                          delay:0.5f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^(void) {
                         imageView.frame = CGRectMake(0, 92, 320, 320);

                     }
                     completion:NULL];
    [self.view bringSubviewToFront:imageView];
    // Animation View Buttons
    //1 Amazon
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *button=[UIImage imageNamed:@"button.png"];
    [button1 setImage:button forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 290, 80, 30);
    button1.backgroundColor=[UIColor whiteColor];
    button1.showsTouchWhenHighlighted=YES;

    [button1 addTarget:self
                action:@selector(openAmazon:)
     forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:button1];

    //2 iTunes
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setImage:button forState:UIControlStateNormal];
    button2.frame = CGRectMake(82, 290, 80, 30);
    button2.backgroundColor=[UIColor whiteColor];
    button2.showsTouchWhenHighlighted=YES;

    [button2 addTarget:self
     action:@selector(openiTunes:)
     forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:button2];



}
4

3 回答 3

5

您需要使用选项 UIViewAnimationOptionAllowUserInteraction

...
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
...
于 2012-12-05T11:18:12.133 回答
1

确保userInteractionEnabled在 imageView 上设置属性:

[imageView setUserInteractionEnabled:YES];
于 2012-12-05T11:04:14.777 回答
1
[self.view bringSubviewToFront:imageView];

您在增加其框架后将图像视图置于前面,这会阻止按钮操作

于 2012-12-05T11:12:27.617 回答