放大和缩小 UIView

发布于 2024-10-17 16:55:03 字数 160 浏览 1 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-10-24 16:55:03

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

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

[[self view] addGestureRecognizer:twoFingerPinch];
}

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

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

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

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

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

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:

 -(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! :)

悲喜皆因你 2024-10-24 16:55:03

Swift 3、4+

用两根手指检测 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")
    }
}

Swift 3, 4+

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

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")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文