簡體   English   中英

iPhone - 嵌套在UIView中的UI按鈕不會響應該視圖的動畫后的觸摸

[英]iPhone - UIbutton nested in UIView does not respond to touch following animation of that view

我有一個帶有UIButton的UIView(視圖B)。

我將此視圖B添加到主視圖邊界之外的區域中的主視圖(視圖A),而不是使用UIView動畫為其設置動畫。

在UIView動畫完成后,View B現在位於View A之上,因此按鈕可見,按鈕不響應觸摸...我以前從未見過這個但是這是我用新的第一個應用程序iOS(iOS 5)。 有任何想法嗎?

提前致謝。

這是你描述的情況嗎? 因為它似乎工作正常。 您是否檢查過UIView上的userInteractionEnabled是否設置為YES?

- (void)buttonPressed:(UIButton*)button
{
    NSLog(@"button pressed");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 100, 100)];
    view.backgroundColor = [UIColor blackColor];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 10, 100, 20);
    [view addSubview:button];

    [self.view addSubview:view];

    [UIView animateWithDuration:0.5 animations:^{
        view.transform = CGAffineTransformMakeTranslation(0, 100);
    }];

    [view release];
}

我不確定這是否已經回答了所以我試了一下,只是為了記錄:

檢查按鈕是否不在任何超級視圖的框架之外。

我發現像這樣放在外面的按鈕可能不起作用。 來想一想,這很奇怪。 當我使用從下面動畫的按鈕制作視圖時,我遇到了這個問題。 在這下面,我有一個灰色的可觸摸視圖,允許取消。 唯一的問題是1)灰色區域我使用父視圖本身和2)我讓這個灰色區域縮小,因為子視圖動畫到位...因此,按鈕變得外面,它不起作用。

解決方案是將視圖保留為全尺寸,並將另一個添加為灰色,或者使第一個灰色而不縮小(我想避免這種情況的唯一原因是它會使一堆半透明層不是最佳的) 。 然后是按鈕頂部的視圖。 :)

希望這可以幫助。

如果你通過編程創建了Button,那么你必須這樣做: -

myButton.userInteractionEnabled =YES;

希望能幫助到你... :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM