2013-01-23 125 views
0

我已經在scrolview中添加了一些uiimageview,現在想要觸摸它執行一些操作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]); 

}

能有人幫我在得到被稱爲觸摸委託方法。

問候
潘卡

回答

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); 
} 
+0

是的,我用手勢識別器試過,它工作。我仍然想知道爲什麼它不適用於UITouch。 – pankaj

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

{ 

UITouch *touch = [touches anyObject]; 

touchPoint = [touch locationInView:imageView]; 

} 

,並請確保您已導入QuartzCore框架在.h文件中