0

在我的項目中,我有一個UIImageVIew用戶導入背景,然後在該背景上放置較小的圖片。UIPinchGestureRecognizer縮放所有視圖

我想僅將手勢應用於正在點擊的較小圖片。我使用的手勢是輕拍,長按,手勢和捏手勢。我的問題只是捏手勢。我可以單獨應用長按,輕拍和平移,但可以捏合,不僅是較小的圖像縮放,而且還有背景。任何想法如何解決這個問題?在我

- (無效)viewDidLoad中我已設置:

//single tap 
    UITapGestureRecognizer *tapg = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
              action:@selector(tapAction:)]; 
    [self.itemView addGestureRecognizer:tapg]; 

    // double tap 
    UITapGestureRecognizer *doubleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
              action:@selector(doubleTapAction:)]; 
    doubleTap.numberOfTapsRequired = 2; 
    [self.itemView addGestureRecognizer:doubleTap]; 

    // longPress 
    UILongPressGestureRecognizer *longPress = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                action:@selector(longPressAction:)]; 
    [self.itemView addGestureRecognizer:longPress]; 

    // pan 
    UIPanGestureRecognizer *pan = 
    [[UIPanGestureRecognizer alloc] initWithTarget:self 
              action:@selector(panAction:)]; 
    [self.itemView addGestureRecognizer:pan]; 


    //pinch 

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self  action:@selector(pinchAction:)]; 
    [self.itemView addGestureRecognizer:pinch]; 

然後,

// tap 
- (void)tapAction:(UITapGestureRecognizer *) sender{ 
CGPoint tapPoint = [sender locationInView:self.itemView]; 
NSLog(@">>>tap x=%.2f, y=%.2f", tapPoint.x, tapPoint.y); 
int i =0; 
isTaped = NO; 
for (i = 0; i < ivMax; i++) { 
    if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) { 
     isTaped = YES; 
     iViewsTapidx = i; 
     NSLog(@"i = %d", i); 
     break; 
    } 
} 
} 

// doubleTap 
- (void)doubleTapAction:(UITapGestureRecognizer *) sender{ 
NSLog(@">>>doubleTap"); 
CGPoint tapPoint = [sender locationInView:self.itemView]; 
isTaped = NO; 
isDblTaped = NO; 

int i =0; 
for (i = 0; i < ivMax; i++) { 
    if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) { 
     isDblTaped = YES; 
     iViewsDblTapidx = i; 
     break; 
    } 
} 

// view 
if (isDblTaped) { 
    NSLog(@"remove %d", i); 
    (*(iViews+i)).tag = 0; 
    [*(iViews+i) removeFromSuperview]; 
} 
} 

// longPress 
- (void)longPressAction:(UILongPressGestureRecognizer *) sender{ 
if ([sender state] == UIGestureRecognizerStateBegan) { 

    NSLog(@">>>longPress 1"); 
}else if ([sender state] == UIGestureRecognizerStateEnded) { 
    //  NSLog(@">>>longPress 2"); 

    CGPoint tapPoint = [sender locationInView:self.itemView]; 
    NSLog(@">>>longPress 2 x=%.2f, y=%.2f", tapPoint.x, tapPoint.y); 
    int i =0; 
    for (i = 0; i < ivMax; i++) { 
     NSLog(@"i = %d", i); 
     if ((*(iViews+i)).tag == 0) { 
      break; 
     } 
    } 

    if (i < ivMax) { 
     //set smaller picture 
     UIImage *stampImage; 
     NSString *imagepath = [NSString stringWithFormat:@"%@%@",[FileUtility   getPDFImageFolderPath], self.menu.imageID]; 
     if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) { 
      stampImage = [UIImage imageNamed:@"NotExistFile.jpg"]; 

     } else { 
      stampImage = [UIImage imageWithContentsOfFile:imagepath]; 
     } 
     int parcent = stampImage.size.width/_width; 

     //show smaller picture 
     *(iViews+i) = [[UIImageView alloc] initWithImage:stampImage]; 
     (*(iViews+i)).frame = CGRectMake(tapPoint.x - stampImage.size.width/parcent/2, 
              tapPoint.y - stampImage.size.height/parcent/2, 
              stampImage.size.width/parcent, 
              stampImage.size.height/parcent); 
     (*(iViews+i)).tag = i+1; 
     [self.itemView addSubview:*(iViews+i)]; 
     iViewsTapidx = i; 
     isTaped = YES; 
    } 

} 
} 

// Pan 
- (void)panAction:(UIPanGestureRecognizer *) sender{ 
NSLog(@">>>pan"); 
if (isTaped) { 

    CGPoint p = [sender translationInView:self.itemView]; 
    CGPoint movePoint = CGPointMake((*(iViews+iViewsTapidx)).center.x + p.x, 
            (*(iViews+iViewsTapidx)).center.y + p.y); 
    (*(iViews+iViewsTapidx)).center = movePoint; 
    //  NSLog(@">>>pan x=%.2f, y=%.2f --> x=%.2f, y=%.2f", p.x, p.y, movePoint.x, movePoint.y); 
    NSLog(@">>>pan x=%.2f, y=%.2f", p.x, p.y); 
    [sender setTranslation:CGPointZero inView:self.itemView]; 
} 
} 

// scale the piece by the current scale 
// reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta  from the current scale 
- (void)pinchAction:(UIPinchGestureRecognizer *)sender 
{ 
if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) { 
    [sender view].transform = CGAffineTransformScale([[sender view] transform], [sender scale], [sender scale]); 
    [sender setScale:1]; 

} 

} 
+0

http://stackoverflow.com /問題/ 9008975 /如何到出鋼到變焦和雙抽頭對縮小與 - 的UIScrollView/9009554#9009554 –

回答

0

給你的形象的名字insted的的

[self.itemView addGesture.....