1

UITextView在主视图UIView([self view])上添加了一些子视图()来缩放/捏合并在屏幕上拖动它们。subview带有标签(mytextview1.tag = 1)的 ONE 一切正常。

但是如何告诉 UIPinchGestureRecognizer 不止一个subview呢?换句话说:如何检测当前的触摸subview并给它一个标签值?某种hittest(一根手指触摸@ subview)?

出于可用性原因,我想使用主视图。我可以将这两个手指手势附加在每个上,subview但它们可以很小以进行缩放...

这里是带有标签的 ONEsubview的代码:

UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];
[[self view] addGestureRecognizer:twoFingerPinch];


- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    NSLog(@"Pinch scale: %f", recognizer.scale);

    mytextview1.tag = 1; // please give an idea to detect current touched subview
    UITextView *myViewWithTag = (UITextView *)[self.view viewWithTag:1];

    UITextView *myViewWithTag = (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:recognizer.view];
    NSLog(@"location: %@", NSStringFromCGPoint(location));

    UIFont *font = [myViewWithTag font];
    CGFloat pointSize = [font pointSize];
    NSString *fontName = [font fontName];

    pointSize = ((recognizer.velocity > 0) ? 1.0 : -1.0) * 1 + pointSize;
    if (pointSize < 13) pointSize = 13;
    if (pointSize > 120) pointSize = 120;

    [myViewWithTag  setFont:[UIFont fontWithName:fontName size:pointSize]];

    CGRect frame = myViewWithTag.frame;
    frame.size.height = myViewWithTag.contentSize.height;
    myViewWithTag.frame = frame;
}
4

3 回答 3

1

twoFingerPinch:您可以检查手势识别器的状态

如果状态是,UIGestureRecognizerStateBegan那么您可以通过hitTest:withEvent:或自定义例程检测底层视图并将其存储在某处(比如 ivar,如 UIView* _draggingView)。

如果状态是UIGestureRecognizerStateEnded或者UIGestureRecognizerStateCancelled您忘记了存储的视图 (_draggingView = nil)。

如果状态不高于您缩放存储视图(_draggingView)。

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    switch([recognizer state])
    {
        case UIGestureRecognizerStateBegan:
        {
            CGPoint location = [recognizer locationInView:recognizer.view];
            UIView* view = [recognizer.view hitTest:location withEvent:nil];
            if(%view is fine to use%)
            {
                _draggingView = view;
            }

        break;
        }


        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateChanged:
        {
            _draggingView = nil;

        break;
        }
    }    

    if(_draggingView)
    {
        // scale _draggingView
    }
}
于 2013-07-11T09:34:30.740 回答
0

好的!

我必须清理代码,但它可以工作。一件事:由于某种原因,它并没有真正以 UIGestureRecognizerStateEnded 结尾...标签值为 0 很好...但背景颜色不是 alpha: 0.0 这是代码:

    UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];
twoFingerPinch.cancelsTouchesInView = FALSE;
twoFingerPinch.delaysTouchesEnded = TRUE; // <---- this line is essential
[[self view] addGestureRecognizer:twoFingerPinch];

// 一些开关,viewWithTag 是我的朋友 ;-)

- (void) twoFingerPinch:(UIPinchGestureRecognizer *) recognizer {
NSLog(@"Detected a pinch gesture");

CGPoint touchPoint = [recognizer locationInView:self.view];
NSLog(@"touchPoint: %@", NSStringFromCGPoint(touchPoint));
UIView *touchView = [self.view hitTest:touchPoint withEvent:nil];

switch (recognizer.state) {

    case UIGestureRecognizerStateBegan:
        NSLog(@"began");

        if([touchView isKindOfClass:[UITextView class]]) {
            touchView.tag = 1;
            NSLog(@"touchView: %ld", (long)touchView.tag);
            touchView.backgroundColor =[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5];
        }
    break;

    case UIGestureRecognizerStateChanged:
        NSLog(@"changed");
    break;

    case UIGestureRecognizerStateCancelled:
        NSLog(@"cancelled");
        touchView.tag = 0;
        touchView.backgroundColor =[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];
    break;

    case UIGestureRecognizerStateFailed:
        NSLog(@"failed");
        touchView.tag = 0;
        touchView.backgroundColor =[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];
    break;

    case UIGestureRecognizerStateEnded:
        NSLog(@"ended");
        touchView.tag = 0;
        NSLog(@"touchView: %ld", (long)touchView.tag);
        touchView.backgroundColor =[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];

    break;

    default:

        touchView.tag = 0;
        touchView.backgroundColor =[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];

    break;
}
    UITextView *myViewWithTag = (UITextView *)[self.view viewWithTag:1];
    // UITextView *myViewWithTag = (UITextView *)recognizer.view;

    UIFont *font = [myViewWithTag font];
    CGFloat pointSize = [font pointSize];
    NSString *fontName = [font fontName];

    pointSize = ((recognizer.velocity > 0) ? 1.0 : -1.0) * 1 + pointSize;
    if (pointSize < 13) pointSize = 13;
    if (pointSize > 120) pointSize = 120;

    [myViewWithTag  setFont:[UIFont fontWithName:fontName size:pointSize]];

    CGRect frame = myViewWithTag.frame;
    frame.size.height = myViewWithTag.contentSize.height;
    myViewWithTag.frame = frame;

}

于 2013-07-11T14:57:43.953 回答
0

利用

hitTest:withEvent:

返回包含指定点的视图层次结构(包括其自身)中接收器的最远后代。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

首先从手势识别器对象中获取接触点

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
   CGPoint touchPoint = [recognizer locationInView:self.view];
   UIView *touchView = [self.view hitTest:touchPoint withEvent:nil];
   if(touhView isKindOfClass:[UITextView class])
{
}
}

PS:我希望我没有写错语法。这是我在没有 Mac 的情况下写的。

于 2013-07-11T09:32:12.330 回答