2011-03-15 35 views
2

使用UIButton,您可以看到用戶單擊了該按鈕(圖像變爲灰色)。(iphone)如何顯示imageview被點擊爲uibutton?

我想在UIImageView中的行爲,並想知道如何做到這一點。
另一種可能的方法是使用UIButton,讓它在視覺上反應(變成灰色)
但假裝touch沒有發生(因爲我需要將touch事件傳遞給superview或nextresponder)。

謝謝。

  • 編輯

其實我結束了繼承的UIButton和 實施的touchesBegan /移動/結束了highlightning

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event 
{ 
    [super touchesBegan: touches withEvent: event]; 
    [self.nextResponder touchesBegan: touches withEvent: event]; 
} 

第一的touchesBegan,
第二個用於妥善處理我所需要的。

不知道叫兩個touchesBegan會好..

回答

1

我得到了你的問題的答案。

通過Interface Builder將UIImageView放到您的視圖中,並將其設置爲.h中的IBOutlet並將其命名爲「img」。和寫這個代碼在.M

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *touch = [[event allTouches] anyObject]; 
if (CGRectContainsPoint([img frame], [touch locationInView:self.view])) { 

    img.alpha = 0.5; 
    [UIView beginAnimations:@"frame" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

    img.alpha = 1.0; 

    [UIView commitAnimations]; 

    NSLog(@"Image Touched"); 

} 
} 
1

有一個屬性,名爲highlightedImage,在UIImageView的。通過設置此屬性,UIImageView將在觸摸時更改爲此圖像。

+0

啊,我看到的是,如果我不肯定將是巨大的必須爲高亮顯示的狀態提供另一個圖像。 – eugene 2011-03-15 10:42:12

+0

如果我必須走這條路線,我應該如何創建突出顯示的圖像? (如在選擇的按鈕中出現)改變alpha? – eugene 2011-03-15 11:34:53

+0

一般來說,我會添加一個CATransition動畫效果來產生淡出動畫。 – AechoLiu 2011-03-16 03:44:54

0

你的第二個選項聽起來最好。只需使用UIButton並設置圖像或背景圖像。如果你繼承的UIButton(這是反過來的UIView的子類),你可以捕捉所有觸摸事件,並將它們傳遞到像這樣基本的觀點:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    // returning nil will ignore touches on this view 
    return nil; 
} 
+0

它會不會在視覺上改變圖像? – eugene 2011-03-15 11:33:58