PinchGesture和最大,最小限制

时间:2015-11-05 15:28:24

标签: ios objective-c uipinchgesturerecognizer

我尝试设置PinchGesture的最大和最小限制。 但仍然存在变得非常小到几乎消失或变得非常大并且消失的问题。我的代码有什么问题?我希望比原始尺寸大两倍是最大限制,比原始尺寸小0.7倍是最小限制。

- (void)viewDidLoad {
    [super viewDidLoad];
    imageMaxscale = 2;
    imageMinscale = 0.7;
    imageNormalscale = 1;

    UIPinchGestureRecognizer *pinchRecognizerdrawChart =
        [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(handlePinchRecognizerdrawChart:)];
    [pinchRecognizerdrawChart setDelegate:self];
    [self.drawChart addGestureRecognizer:pinchRecognizerdrawChart];

}

-(void)handlePinchRecognizerdrawChart:(UIPinchGestureRecognizer*)sender {
    if(imageNormalscale * [sender scale]  > imageMinscale && imageNormalscale * [sender scale]  < imageMaxscale){
        imageNormalscale = imageNormalscale * [sender scale];

        sender.view.transform = CGAffineTransformScale(sender.view.transform, imageNormalscale, imageNormalscale);

    }
    sender.scale = 1.0;
}

1 个答案:

答案 0 :(得分:0)

我从一个链接找到了上面的解决方案,它在他的演示中起作用。但是我的实施并不起作用。所以我刚修改为

-(void)handlePinchRecognizerdrawChart:(UIPinchGestureRecognizer*)sender {
    [self.scrollView bringSubviewToFront:self.drawChart];
    if([sender scale] > previouscale && self.drawChart.frame.size.width < screenRect.size.width*1.75){
        sender.view.transform = CGAffineTransformScale(sender.view.transform, [sender scale], [sender scale]);
        previouscale = [sender scale];
    }else if([sender scale] < previouscale && self.drawChart.frame.size.width > screenRect.size.width){
        sender.view.transform = CGAffineTransformScale(sender.view.transform, [sender scale], [sender scale]);
        previouscale = [sender scale];
    }else
        previouscale = 1;

    sender.scale = 1.0;

}