2011-10-12 104 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,因为这会阻止它接收事件。