0

我在 scrollview 中添加了一些 uiimageview,现在想要在触摸 uiimageview 时执行一些操作。但是我的触摸委托方法没有被调用。以下是我的代码:

int x = 0;
for (int i = 0; i < [arr count]; i++){
    UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, self.view.frame.size.height-45)];
    myImageview.userInteractionEnabled = TRUE;
    myImageview.opaque = YES;
    //[myImageview setImage:[arr objectAtIndex:i]];
    myImageview.tag = 100+i;
    [scrollvw addSubview:myImageview];
    [myImageview setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[(NSDictionary*)[arr objectAtIndex:i] objectForKey:@"image"]]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
        //reload data here
        myImageview.image = image;


    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
        //handle errors here
    }];

    x += myImageview.frame.size.width;

}

[scrollvw setContentSize:CGSizeMake(x, self.view.frame.size.height-45)];

我添加了以下方法,但它不起作用:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];

if (touch.view.tag > 0) {
    touch.view.center = location;
}

NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

}

有人可以帮我调用触摸委托方法吗?

问候
潘卡伊

4

2 回答 2

0

为什么不像这样将 UITapGestureRecognizer 添加到您的图像上:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testMethod:)];

[myImageview addGestureRecognizer:tap];

- (void)testMethod:(id)sender {
    UIImageView *sent = (UIImageView *)sender;

    NSLog(@"Image Tag: %d", sent.tag);
}
于 2013-01-23T18:41:30.777 回答
0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

{

UITouch *touch = [touches anyObject];

touchPoint = [touch locationInView:imageView];

}

请确保您已在 .h 文件中导入 QuartzCore 框架

于 2013-01-24T05:57:31.710 回答