0

我有一个图像视图作为背景图像。我正在寻求在图像视图触摸的某个地方启用。我从这个开始:

- (id)initWithTouchPoint:(CGRect )point
{
    self = [super init];
    if (self) {
        touchFrame = point;
        [self setAccessibilityFrame:touchFrame];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(BOOL)canResignFirstResponder{
    return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];

    if (CGRectContainsPoint(touchFrame, touchLocation)) {
        //[self setUserInteractionEnabled:NO];

    }else{
        //[self setUserInteractionEnabled:YES];
    }

    DLog(@"touchesBegan at x : %f y : %f",touchLocation.x,touchLocation.y);
}

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

}

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

}

当用户在touchFrame中触摸时,是否可以让用户触摸图像视图?

谢谢你。

4

2 回答 2

1

在 UIImageView 上添加 UITapGestureRecognizer

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handleGesture:)];
[gesture setNumberOfTapsRequired:1];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:gesture];

现在在 HandleGesture 方法中:

-(void)handleGesture:(UITapGestureRecognizer *)_gesture
{
     if (_gesture.state == UIGestureRecognizerStateEnded)
     {
         CGPoint touchedPoint = [_gesture locationInView:self.view];
     }
}

您现在可以检查 handleGesture 方法中的touchedPoint是否在指定区域,您可以相应地执行您想要的任务

于 2013-05-02T11:10:20.540 回答
0

您可以尝试将布尔变量作为类成员说 BOOL allowTouch 用值 NO 初始化:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint touchLocation = [touch locationInView:self];

     if (CGRectContainsPoint(touchFrame, touchLocation)) {
     allowTouch = YES;
     }
 }

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if(allowTouch)
     {
      //handle moves here
     }
 }

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
     allowTouch = NO;//you can put your condition too to end touches
 }

它可能会有所帮助。

于 2013-05-02T10:59:46.733 回答