ios 放大和缩小 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5035431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:55:19  来源:igfitidea点击:

zooming in and out of a UIView

xcodeioszoomzooming

提问by luca590

what is the best way to zoom in and out of a UIView with a simple method buttons. (e.i

使用简单的方法按钮放大和缩小 UIView 的最佳方法是什么。(ei

(IBAction)zoomin:(int)distance
{
method here
}
(IBAction)zoomout:(int)distance
{
and here
}

回答by Kritanshu_iDev

It can be done using two finger gesture recognizer: You have to just write down:-

可以使用两个手指手势识别器来完成:您必须写下:-

-(void)viewDidLoad
{
UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] 
                                           initWithTarget:self 
                                           action:@selector(twoFingerPinch:)] 
                                           autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];
}

By this you have initialized an instance which will take care of two finger sensations on the Screen (or View on which you are applying this method) Now define what to do if you have recognized the two fingers:

这样,您已经初始化了一个实例,该实例将处理屏幕(或应用此方法的视图)上的两个手指感觉。现在定义如果您识别了两个手指,该怎么做:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
    NSLog(@"Pinch scale: %f", recognizer.scale);
    CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
                                      // you can implement any int/float value in context of what scale you want to zoom in or out
    self.view.transform = transform;
}

The above defined method is called automatically not through the UIButton actions but it will solve your problem with simplicity If you strictly want to use zoom at IBAction then simply do this:

上面定义的方法不是通过 UIButton 动作自动调用的,但它会简单地解决您的问题如果您严格想在 IBAction 上使用缩放,那么只需执行以下操作:

 -(IBAction)methodCalledOnClickingUIButton:(id)sender
{
    if(sender==zoomInButton)
     {
       scaleValue++;
     }
    else if(sender==zoomOutButton)
     {
       scaleValue--;
     }
     CGAffineTransform transform = CGAffineTransformMakeScale(scaleValue,scaleValue);
     self.view.transform = transform;
}

Where scaleValue is any float value..you can set this up according to your application requirement! I hope it will work fine for you! :)

其中 scaleValue 是任何浮点值..您可以根据您的应用程序要求进行设置!我希望它对你有用!:)

回答by Tuan Huynh

Swift 3, 4+

斯威夫特 3、4+

Detect zoom in/out with two fingers for a UIView. Here is an example listening on the main view:

用两根手指检测放大/缩小UIView。这是在主视图上侦听的示例:

override func viewDidLoad() {    
     var pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchedView))
     view.isUserInteractionEnabled = true
     view.addGestureRecognizer(pinchGesture)    
}

// Listener

@objc func pinchedView(sender: UIPinchGestureRecognizer) {
    if sender.scale > 1 {
        print("Zoom out")
    } else{
        print("Zoom in")
    }
}