0

我正在使用 Xcode 4.4,目标是 iOS 5。

如标题所示,我想为不同的图像创建多个捏合手势。我使用两种类型的图像。一种设置为背景,第二种类型的图像是粘贴在设置为背景的图像上的图章。为此,我使用 UIGestureRecognizer。一切都很好,直到我粘贴邮票类型的图像并尝试缩放该图像。我可以缩放,但是当我缩放图像时,图像也已设置为背景缩放。

使用下面的代码,我可以粘贴不同的图像(图章类型),单独点击并拖动它们,但当我尝试所有内容缩放时也不能。为什么不工作?

先感谢您

   - (void)viewDidLoad{

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"menuListType=%@ ; menuID=%@", self.category.menuListType ,self.menu.menuID);

UIImage *aImage; 
NSString *imagepath = [NSString stringWithFormat:@"%@%@",[FileUtility getPDFImageFolderPath], self.menu.imageID];
if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) {
    //        self.canvasView.image = [UIImage imageNamed:@"NotExistFile.jpg"];
    aImage = [UIImage imageNamed:@"NotExistFile.jpg"];

} else {
    //        self.canvasView.image = [UIImage imageWithContentsOfFile:imagepath];
    aImage = [UIImage imageWithContentsOfFile:imagepath];
    //        self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
}

// Choose image base and set as background
if ([self.category.menuListType isEqualToString:@"4"]) {
    //set size
    int imageW = aImage.size.width;
    int imageH = aImage.size.height;

    float scale = (imageW > imageH ? 500.0f/imageH : 500.0f/imageW);


    CGSize resizedSize = CGSizeMake(imageW * scale, imageH * scale);
    UIGraphicsBeginImageContext(resizedSize);
    [aImage drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.canvasView.contentMode = UIViewContentModeTop;
    self.canvasView.image = resizedImage;

    if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:@""]) {
    } else {
        self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
    }


} else {
    // Paste image and set as background
    UIImage *captureImage;
    NSString *capturepath = [NSString stringWithFormat:@"%@%@",[FileUtility getPDFImageFolderPath], @"capture.png"];
    if ([FileUtility fileExist:capturepath]) {
        captureImage = [UIImage imageWithContentsOfFile:capturepath];
    } else {
        captureImage = [UIImage imageNamed:@"NotExistFile3.png"];
    }
    self.canvasView.contentMode = UIViewContentModeTop;;
    self.canvasView.image = captureImage;
    self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];

    // Prepare Gestures
    iViewsTapidx = 0;
    iViewsDblTapidx = 0;
    ivMax = sizeof(iViews) / sizeof(iViews[0]);
    isTaped = NO;
    isDblTaped = NO;
    _width = 100;


    // Initialization code
    // 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];

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


    //pinch

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

    // Stamp Image
    int i = 0;
    CGPoint tapPoint = CGPointMake(self.canvasView.center.x, self.canvasView.center.y);

    int parcent = aImage.size.width / _width;   // Prepare image to paste
    // Show stamp
    *(iViews+i) = [[UIImageView alloc] initWithImage:aImage];
    (*(iViews+i)).frame =  CGRectMake(tapPoint.x - aImage.size.width/parcent/2, 
                                      tapPoint.y - aImage.size.height/parcent/2,
                                      aImage.size.width/parcent,
                                      aImage.size.height/parcent);
    (*(iViews+i)).tag = i+1;
    [self.itemView addSubview:*(iViews+i)];
    iViewsTapidx = i;
    isTaped = YES;
    self.itemView.userInteractionEnabled = YES;

}

[self showAnimation];
}

// 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;
    }
}

// remove
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) {

    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) {
        //Stamp imapge
        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;   // Set stamp image
        //Sow stamp image
        *(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;
    }

}
}

// drag
- (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];
}
}



-(void) handlePinch:(UIGestureRecognizer *) sender {

UIPinchGestureRecognizer *pinchGesture = (UIPinchGestureRecognizer *) sender;

if (pinchGesture.state == UIGestureRecognizerStateBegan || pinchGesture.state == UIGestureRecognizerStateChanged) {

    //UIView *view = pinchGesture.view;
   // UIView *view = self.itemView;
    self.itemView.transform = CGAffineTransformScale(self.itemView.transform, pinchGesture.scale, pinchGesture.scale);
    pinchGesture.scale = 1;
    NSLog(@"zoom");
}
} 
4

2 回答 2

0

另一种方法是使用 UIScrolliew 作为容器而不是 UIView 并使用- (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView { return imageView; }

这里的 imageView 是您要缩放的图像,这将缩放您正在捏它的唯一图像并且工作正常。

于 2012-11-08T09:56:34.313 回答
0

您必须在 Uiimageview 上添加手势识别器,请在此处查看:UIImageView 上的 UIGestureRecognizer

于 2012-11-08T07:29:03.773 回答