【问题标题】:UIImageView touch handlingUIImageView 触摸处理
【发布时间】:2013-05-02 10:40:57
【问题描述】:

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

- (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 中触摸时,是否可以让用户触摸图像视图?

谢谢。

【问题讨论】:

    标签: ios uiimageview uitouch


    【解决方案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是否在指定区域,您可以相应地执行您想要的任务

    【讨论】:

      【解决方案2】:

      您可以尝试将布尔变量作为类成员说 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
       }
      

      这可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        • 2020-10-07
        相关资源
        最近更新 更多