开发者

Touch Event is Not Working? in UImageview?

开发者 https://www.devze.com 2022-12-08 23:27 出处:网络
i have done following in viewdidload of viewcontroller.m img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

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开发者_Python百科 release];

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?


You should try setting this property:

img.userInteractionEnabled = YES;

But this is not enough, because the methods:

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

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

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.

Example:

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

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];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消