在目标c中捏放大和缩小

时间:2012-07-11 06:14:19

标签: iphone objective-c cocoa pinchzoom

如何在我们的应用程序中集成捏缩放和缩小,我在scrollView上使用imageview  我的代码是:

- (IBAction)handlePinchGesture:(UIGestureRecognizer *) recognizer {
    if(zoomEnable == TRUE)
    {

        CGFloat factor = [(UIPinchGestureRecognizer *) recognizer scale];
        CGFloat lastScaleFactor = 1;

        //if the current factor is greater 1 --> zoom in
        if (factor > 1) {
            scrollView.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1),lastScaleFactor + (factor-1));
            scrollView.scrollEnabled = YES;

        } else {

            [UIView beginAnimations:@"animation" context:nil];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:scrollView cache:NO];
            scrollView.transform = CGAffineTransformMakeScale(1,1);     
            [UIView commitAnimations];

        }
        isScrollable = TRUE;
    }

}

它从开始每次开始缩放我想要如果我缩放一些然后再次它开始我停止缩放。任何帮助都是高度赞赏的

感谢;

2 个答案:

答案 0 :(得分:1)

如果您已经使用UIGestureRecognizers,则无需使用UIScrollViewUIScrollView支持缩放缩放。

要进行缩放和平移,代理必须同时实施viewForZoomingInScrollView:scrollViewDidEndZooming:withView:atScale:;此外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同。

答案 1 :(得分:0)

-(void)zoomingImages{ 
    self.FullSizeScrollView.pagingEnabled =YES;
    NSMutableArray *_scrollArray =[[NSMutableArray alloc]init];

    // add images to scroll array

        [_scrollArray addObject:self.image1];
        [_scrollArray addObject:self.image2];

    //now call init with frame function given below to set frame for each image in scroll view
    for(int i =0 ;i<[_scrollArray count];i++){
        ZoomingImageView *_imageScrollView = [[ZoomingImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 460)];
        _imageScrollView.captureView=self;      
        [self.checkFullSizeScrollView addSubview:_imageScrollView];
        [_imageScrollView release];
        self.checkFullSizeScrollView.contentSize = CGSizeMake((i*320)+320, 460);
    }

    [_scrollArray release];
}


@implementation ZoomingImageView

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

      if (self) {

        self.maximumZoomScale = 4;
        self.minimumZoomScale = 1;     
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = YES;
        self.delegate = self;
        self.bouncesZoom = NO;
        self.currentImageView.clipsToBounds=NO;
        self.contentMode =UIViewContentModeScaleAspectFit;
        UIImageView *zoomImageView_ =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.currentImageView = zoomImageView_;
        [zoomImageView_ release];
         self.currentImageView.contentMode =UIViewContentModeScaleAspectFit;
        self.currentImageView.userInteractionEnabled = YES;
        self.currentImageView.multipleTouchEnabled = YES;
        [self addSubview:self.currentImageView];}
return self;}