2012-01-06 94 views
0

在我的程序中,我使用滾動視圖作爲背景視圖。然後,我在滾動視圖中添加幾個圖像。另外,我允許用戶觸摸圖像以觸發某些操作。但是,滾動視圖上附加的圖像在觸摸時無法檢測到。我在觸摸事件中使用以下代碼:如何讓圖像在滾動視圖上被觸摸?

UITouch * touch = [[event allTouches] anyObject]; 

我做錯了什麼?我怎麼解決這個問題?非常感謝你!

回答

3

您可以使用UIImageView來跟蹤觸摸,並且還設置myImageView.userInteractionEnabled = YES,因爲圖像瀏覽的默認值爲no。

如果您要檢測的圖像上的水龍頭,你可以這樣做:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesSingleTap:)]; 
     [singleTap setNumberOfTapsRequired:1]; 

     [imageView addGestureRecognizer:singleTap]; 

並實現您的選擇:

- (void)handlesSingleTap:(UIGestureRecognizer *)gestureRecognizer 
{ 
    //Handle touch 
} 
+0

我已經設置你說的東西。但是,當我觸摸圖像時沒有響應。我認爲模擬器只是檢測到我只觸摸滾動視圖,而不是圖像。另外,我試圖將圖像移動到滾動視圖之外,然後運行。它是如何來的? – 2012-01-06 13:16:13

+0

@PeterLam你可以發佈你的代碼?,我在滾動視圖上使用此代碼,它工作正常。 – 2012-01-06 13:20:08

+0

在.h文件中,我創建了 IBOutLet UIImageView * img1; @property(nonatomic,retain)IBOutLet UIImageView * img1;我創建了 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UITouch * touch = [[event allTouches] anyObject]; if([touch view] == img1){code is here} ViewDidLoad {super viewDidLoad]; img1.userInteractionEnabled = true; } 我檢查過這個程序不能運行的功能touchesBegan {} – 2012-01-06 16:33:10

相關問題