IOS limits the magnification of a picture by setting the zoom ratio of the pinch gesture

Source: Internet
Author: User
<span id="Label3"></p><p><p>Pinch gesture visual can enlarge the image infinitely, but in general, we only allow the user to zoom within a certain scale range</p></p><p><p>Scenario: records the total scale with a global value, and does not allow the range to continue after it is out of range, only back to the zoom range</p></p><p><p>If you bother packing a good <span style="font-size: 16px;"><strong>assortment</strong></span> here, Three lines of Code: Https://github.com/iOSSinger/SGZoomImage</p></p><p><p>The specific code is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Maxscale 2.0<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Maximum zoom ratio</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Minscale 0.5<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Minimum zoom ratio</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">uiimageview+zoom.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">viewcontroller () @property (weak, nonatomic) iboutlet uiimageview</span></span>*<span style="color: #000000;"><span style="color: #000000;">imageView; @property (nonatomic,assign) cgfloat totalscale;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; Self.totalscale</span></span>=<span style="color: #800080;"><span style="color: #800080;">1.0</span></span><span style="color: #000000;"><span style="color: #000000;">; Uipinchgesturerecognizer</span></span>*pinch =<span style="color: #000000;"><span style="color: #000000;">[[uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (pinch:)]; [self.imageview addgesturerecognizer:pinch]; }</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Pinch: (uipinchgesturerecognizer *<span style="color: #000000;"><span style="color: #000000;">) Recognizer{cgfloat</span> scale</span>=<span style="color: #000000;"><span style="color: #000000;">recognizer.scale; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Amplification Situation</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(scale ><span style="color: #800080;"><span style="color: #800080;">1.0</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.totalscale > Maxscale)<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">reduce the situation</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(scale <<span style="color: #800080;"><span style="color: #800080;">1.0</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.totalscale < Minscale)<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } self.imageView.transform</span></span>=<span style="color: #000000;"><span style="color: #000000;">Cgaffinetransformscale (self.imageView.transform, scale, scale); Self.totalscale</span></span>*=<span style="color: #000000;">scale <span style="color: #000000;">; Recognizer.scale</span></span>=<span style="color: #800080;"><span style="color: #800080;">1.0</span></span><span style="color: #000000;"><span style="color: #000000;">; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p><p>IOS limits the magnification of a picture by setting the zoom ratio of the pinch gesture</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.