触摸事件不起作用?在UImageview中? [英] Touch Event is Not Working? in UImageview?

查看:34
本文介绍了触摸事件不起作用?在UImageview中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在viewcontroller.m的viewdidload中完成了以下操作

i have done following in viewdidload of viewcontroller.m

    img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
img.multipleTouchEnabled = YES;
[self.view addSubview:img];
[img release];

但是当我通过断点检查时,Touchbegan,触摸Moved,一切都不起作用?与此相反,当我使用XIB文件时,我设置了MultipleTouchEnabled,但在两者中触摸事件不起作用...任何帮助?请吗?

but Touchbegan , touch Moved ,everything is not working ,when i check through Break Point? instead of this,when i use XIB file,i set multipleTouchEnabled ,but in both touch event is not working...anyHelp? please?

推荐答案

您应尝试设置此属性:

img.userInteractionEnabled = YES;

但这还不够,因为方法:

But this is not enough, because the methods:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:

来自UIResponder类(UIVIew的基类),而不是UIViewController.

are from the UIResponder class (the base class of UIVIew) and not the UIViewController.

因此,如果要调用它们,则必须定义UIView的子类(在您的情况下为UIImageView),在其中重写基方法.

So if you want them to be called, you have to define a sub class of a UIView (or UIImageView in your case) where you override the base methods.

示例:

MyImageView.h :

@interface MyImageView : UIImageView {
}

@end

MyImageView.m :

@implementation MyImageView

- (id)initWithFrame:(CGRect)aRect {
    if (self = [super initWithFrame:rect]) {
        // We set it here directly for convenience
        // As by default for a UIImageView it is set to NO
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // Do what you want here
    NSLog(@"touchesBegan!");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // Do what you want here
    NSLog(@"touchesEnded!");
}

@end

然后,您可以在示例中使用视图控制器实例化MyImageView:

Then you can instantiate a MyImageView in your example with the view controller:

img = [[MyImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:img];
[img release];

并且您应该看到触摸事件(当然还假设self.view的userInteractionEnabled设置为YES).

And you should see the touch events (also assuming self.view has userInteractionEnabled set to YES of course).

这篇关于触摸事件不起作用?在UImageview中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆