2013-07-11 56 views
0

我在主要的UIView([self view])上添加了一些子視圖(UITextView)來縮放/捏和拖動它們在屏幕上。一切工作正常的一個subview與標籤(mytextview1.tag = 1)。在主視圖中添加UIPinchGestureRecognizer並檢測子視圖上的觸摸

但是如何告訴UIPinchGestureRecognizer有多個subview? 換句話說:如何檢測當前觸摸的subview並給它一個標籤值? 某種類型的擊球(單指觸摸@subview)?

我想使用主視圖的可用性的原因。我可以附上這個兩個手指gestue每subview但他們可以是小縮放...

這裏ONE subview與標籤的代碼:

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

怎麼樣'則hitTest:withEvent:方法'? – Wain

回答

0

twoFingerPinch:您可以檢查手勢識別的state

如果狀態爲UIGestureRecognizerStateBegan,那麼你通過hitTest:withEvent:或自定義程序的基本看法檢測並存儲在某個地方(比如像伊娃UIView的* _draggingView)。

如果狀態爲UIGestureRecognizerStateEndedUIGestureRecognizerStateCancelled,則忘記存儲的視圖(_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 
    } 
} 
0

使用

hitTest:withEvent: 

返回包含指定點的視圖層次結構(包括它本身)中接收器的最遠後代。

​​

首先從手勢識別器對象獲取觸摸點

- (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寫的。

0

好吧!

我必須清理代碼,但它的工作原理。 一件事:由於某些原因,它不真的有UIGestureRecognizerStateEnded結束... 標籤值是0這很好......但的backgroundColor不阿爾法: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; 

}

相關問題