uimageview上的触摸事件作为父视图的子视图添加

时间:2012-03-19 11:28:40

标签: iphone

我在主视图中以编程方式添加了视图。在父视图中,我将以编程方式将imageview添加为子视图。现在我已经为clickview设置了触摸事件,但它不起作用。它没有在点击时检测到触摸事件。我正在使用以下代码触摸事件的imageview。

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

UITouch *touch = [touches anyObject];

if ([touch view] == originalphoto)
{
    [fullview setHidden:NO];
    [self.view addSubview:fullview];
    [setimage setImage:image];
}

}

有没有人知道如何设置以编程方式添加的imageview的触摸事件?

非常感谢。

4 个答案:

答案 0 :(得分:2)

为什么不使用手势?这很容易。您可以使用UITapGestureRecognizer让它适合您:http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITapGestureRecognizer_Class/Reference/Reference.html

答案 1 :(得分:1)

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:UIIMAGEVIEW];// uiimageview object
touchx = location.x;
touchy = location.y;
NSLog(@" x= %i ", touchx);
NSLog(@"y = %i",touchy);

答案 2 :(得分:1)

//为您的视图添加手势

UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ResignResponder:)];
[tempV addGestureRecognizer:Tap];

[Tap release];

//method will be called on tapping the view
-(void)ResignResponder:(UIGestureRecognizer*)recognizer {
        [LocSearchField resignFirstResponder];
        [self.view sendSubviewToBack:tempV];
 }

答案 3 :(得分:0)

也许你需要设置属性imageView.userInteractionEnabled = YES?