2011-10-12 17 views
4

我有一個帶有PNG的ImageView,我想這樣做:當有人觸摸這個imageview時,它的alpha變化爲0.0,有可能嗎? (全部沒有按鈕)iOS:觸摸Imageview時發生的事件

+1

爲什麼要消除按鈕?您可以修改它們的屬性,使它們看起來完全像圖像,同時保留使用按鈕獲得的所有功能。 –

+0

[我如何檢測UIImageView的觸摸事件](http:// stackoverflow。COM /問題/ 855095 /如何-可以-I-檢測最觸摸事件的-AN-的UIImageView) – vikingosegundo

回答

5

是的,這是可能的。例如,你可以做到這一點下面的步驟:

  1. 集圖像視圖的userInteractionEnabled屬性是 - 所以它會接收觸摸事件
  2. 添加UITapGestureRecongnizer它
  3. 手勢處理程序中設置視圖的alpha爲0.0,你可以做到這一點的動畫,以及:

    [UIView animateWithDuration:0.5 animations:^(void){ 
         imageView.alpha = 0.0f; 
        }]; 
    
14

可以使用UITapGestureRecognizer加到UIImageView通過addGestureRecognizer

片段:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
[iv addGestureRecognizer:singleTap]; 
[iv setUserInteractionEnabled:YES]; 

- (void)imageTaped:(UIGestureRecognizer *)gestureRecognizer { 
    [UIView animateWithDuration:0.5 animations:^(void){ 
     imageView.alpha = 0.0f; 
    }]; 
} 
1

斯威夫特

的代碼對我來說,我實現了雙擊手勢的圖像點擊

1 - 通過拖動將圖像鏈接到ViewController d刪除

@IBOutlet weak var imgCapa: UIImageView! 

2 - 實例UITapGestureRecognizer在viewDidLoad方法:

override func viewDidLoad() { 
    super.viewDidLoad() 

    //instance the UITapGestureRecognizer and inform the method for the action "imageTapped" 
    var tap = UITapGestureRecognizer(target: self, action: "imageTapped") 

    //define quantity of taps 
    tap.numberOfTapsRequired = 1 
    tap.numberOfTouchesRequired = 1 

    //set the image to the gesture 
    imgCapa.addGestureRecognizer(tap) 
} 

3 - 創建做你想要當圖像點擊

func imageTapped(){ 
    //write your specific code for what do you want  

    //in my case I want to show other page by specific segue 
    let sumario = self.storyboard?.instantiateViewControllerWithIdentifier("sumarioViewController") as SumarioViewController 

    self.performSegueWithIdentifier("segueSumarioViewController", sender: sumario) 
} 
0

在最近的Xcode什麼方法,這很容易。進入故事板,在對象庫中搜索「手勢」,將想要的圖像拖到圖像視圖上。然後,您可以將視圖層次結構中的手勢對象視爲點擊事件,即通過控件拖動到您的視圖控制器來連接事件處理程序。

一旦你可以設置alpha,只要你喜歡,但如果你試圖從根本上刪除圖像視圖,你應該設置imageView.hidden = true/imageView.hidden = YES,因爲這會阻止它接收事件。