如何以编程方式将UIView或UIImageView与诸如“内部触摸”之类的事件链接? [英] How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?
问题描述
更新:
在我创建UIImageView的View Controller中,我试图这样做:
SEL actionSelector = @selector(doSomethingWhenImageIsTouched :);
[self.myUIImageView addTarget:nil action:actionSelector forControlEvents:UIControlEventTouchUpInside];
编译器给了我一个警告,UIImageView可能没有回应addTarget:...我该怎么办必须这样做,它与UIImageView一起工作。我在文档中看到UIImageView不能从UIControl继承,但addTarget:是UIControl的一部分。
更新:
我最终在创建UIImageView之后创建了一个UIButton。然后我将该按钮的框架设置为UIImageView的框架,将alpha设置为0.1f。由于某种原因,如果alpha为0.0f,则不起作用。然后,我做了这个很酷的addTarget:事情...
在SDK版本3.2(可能早些时候我没有)您可以向UIView添加手势识别器。 UIGestureRecogniser是一个抽象类,允许您定义一个手势(点击,捏,滑动等)并将其添加到UIView中,并且当手势识别器识别手势时,它将触发您对欲望目标的动作。 / p>
在文档中阅读所有内容...:D
Interface Builder will only allow me to hook up such events for an button. But like in HTML, I just want to haven an blank UIImageView where - as soon as the user taps it - a method is invoked. I hope there is some cool programmatically way of doing that, which I don't know about.
UPDATE:
In my View Controller that creates the UIImageView I tried to do this:
SEL actionSelector = @selector(doSomethingWhenImageIsTouched:);
[self.myUIImageView addTarget:nil action:actionSelector forControlEvents:UIControlEventTouchUpInside];
The compiler gives me a warning, that UIImageView may not respond to addTarget:... what do I have to do so that it works with an UIImageView. I see in the docs that UIImageView does not inherit from UIControl, but addTarget: is part of UIControl.
UPDATE:
I ended up creating an UIButton after creating the UIImageView. Then I set the frame of that button to the frame of the UIImageView, and alpha to 0.1f. For some reason, it will not work if alpha is 0.0f. And then, I did that cool addTarget: thing...
In SDK version 3.2 (could be earlier i donno), you can add gesture recognizers to a UIView. UIGestureRecogniser is an abstract class that allows you to define a gesture (tap, pinch, swipe, etc) and add it to a UIView and when the gesture recognizer, recognizes the gesture, it fires up an action on a target of your desire.
read all about it in the docs... :D
这篇关于如何以编程方式将UIView或UIImageView与诸如“内部触摸”之类的事件链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!