简单地说,我想添加一个UIImageView
的水龙头手势,当用户触摸时,调用在另一个类中实现的方法(与包含UIImageView
的类不同) 。添加一个水龙头在另一个类中调用方法的姿势
我可以这样做,如果是的话,我该怎么做?
简单地说,我想添加一个UIImageView
的水龙头手势,当用户触摸时,调用在另一个类中实现的方法(与包含UIImageView
的类不同) 。添加一个水龙头在另一个类中调用方法的姿势
我可以这样做,如果是的话,我该怎么做?
你可以这样做。但是你需要目标类(类中的方法是要执行)为代表或东西手势添加类的实例。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)];
这里的委托将是在去那个类之前你必须设置的目标类的实例。
编辑
我会解释一下。假设你有两个视图控制器类VCA和VCB。您想通过轻击手势从VCB调用VCA中的方法。
在VCB.h
@property (nonatomic,assign)VCA *delegate;
在VCB.m
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)];
在VCA.m
你将出席/推VCB上的init
VCB * viewController = [[VCB alloc]init];
viewController.delegate = self;
// push or present viewController
请你能多申报一下吗?! –
看我的编辑.... –
申报识别器选择器
UITapGestureRecognizer *recognizer =
[UITapGestureRecognizer
initWithTarget:objectOfAnotherClass
action:@selector(methodImplementedOnObjectOfAnotherClass:)];
一个简单的方法是从选择器方法tapGesture
即调用另一个类的方法。
添加UITapGestureRecogniser到ImageView的
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRecognised)];
[imageView addGestureRecognizer:tapGesture];
不要忘记启用的UIImageView的userInteraction
,因为默认情况下为ImageView的用户交互是禁用的。
你可以像下面这样做。
imageView.userInteractionEnabled = YES;
然后在tapGestureRecogniser的选择呼叫另一个类
- (void)tapRecognised
{
[next tappedOnImage];
}
这里next
的方法是另一个类的对象。您也可以使用委托来调用该方法。
希望这会帮助你。
这里self.desired的观点是要在其中添加手势的观点,你应该在以下方式
和“webViewTapped.delegate =自我”意味着你要调用同一类的功能添加手势当用户点击,你可以通过使用委托像self.delegate
UITapGestureRecognizer *viewTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
webViewTapped.numberOfTapsRequired = 1;
webViewTapped.delegate = self;
[self.desiredView addGestureRecognizer:viewTapped];
- (void)tapAction:(UITapGestureRecognizer *)sender;
{
// do your stuff here
}
''NSNotification'可以是一种方式,另一种方式可以授权更改为您所需的功能。使用适合你的任何一种。 – Amar
使用touchBegan方法并在任何地方编写用于调用函数的代码。 –