UIPinchGestureRecognizer缩放所有视图

时间:2012-11-13 12:07:10

标签: objective-c xcode uigesturerecognizer instance-variables uipinchgesturerecognizer

在我的项目中,我有一个UIImageVIew用户导入背景,然后在背景上放置较小的图片。

我想仅将手势应用于正在拍摄的较小图片。我正在使用的手势是点击,长按,pand和捏手势。我的问题只是捏手势。我可以单独应用长按,点击和平移,但不仅可以缩小图片,还可以拍摄背景。我知道如何解决这个问题?

在我的 - (void)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];

}

}

1 个答案:

答案 0 :(得分:0)

提供您的图片名称而不是

   [self.itemView addGesture.....